TUTIORALS FROM MADHAV:
SPRING-AOP-ANNOTATIONS SPRING -DAO SPRIN-MVC SPRING-SECUTITY
SPRING MVC:
Example program1:
// this program will
describes the mvc work flow
Index.jsp
<%@ page language="java" import="java.util.*"
pageEncoding="ISO-8859-1"%>
<html>
<head>
</head>
<body>
This is my JSP page. <br>
<a
href="hai.spring">click hear to call haicontroller</a>
</body>
</html>
Hai.jsp
<%@ page language="java" import="java.util.*"
pageEncoding="ISO-8859-1"%>
<html>
<head>
</head>
<body>
hai welcome to hugo
tc <br>
</body>
</html>
HaiController.java
package madhav;
import
org.springframework.web.servlet.*;
import
org.springframework.web.servlet.mvc.*;
import org.springframework.validation.*;
mport javax.servlet.http.*;
public class HaiController implements Controller{
public ModelAndView handleRequest(HttpServletRequest
req,HttpServletResponse res)throws Exception
{
return new ModelAndView("/Hai.jsp");
}
}
dispatchr-servlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<bean id="mad"
class="madhav.HaiController">
</bean>
<bean id="myurlmapping" class="
org.springframework.web.servlet.handler.
SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="/hai.spring">mad</prop>
</props>
</property>
</bean>
</beans>
Web.xml
<?xml version="1.0"
encoding="UTF-8"?>
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" id="WebApp_ID">
<display-name>speingmvcacc</display-name>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>*.spring</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
OUT PUT SORTS:
·
BeanNameUrlHandlerMapping
·
SimpleUrlHandlerMapping
· ControllerClassNameHandlerMapping
BeanNameUrlHandlerMapping:
class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping
">
<bean id="/hai.spring"
class="madhav.HaiController">
</bean>
</bean>
SimpleUrlHandlerMapping:
Ex-code:
<bean id="mad" class="madhav.HaiController">
</bean>
<bean id="myurlmapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="/hai.spring">mad</prop>
</props>
</property>
</bean>
ControllerClassNameHandlerMapping:
<bean id="myurlmapping" class="org.springframework.web.servlet.handler.ControllerClassNameHandlerMapping">
</bean>
<bean id="mad" class="madhav.HaiController">
</bean>
rlpatther:
URL: <Contextpath>/Hai
Tepes
of controllers:
·
Controller
AbstractCommandController
AbstractCommandController
·
SimpleFormController
·
WizardFormController
·
MultiActionController
Example
program 2:
//this program will
describes using the “AbstractCommandController”;
Login.html
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Softech
computer education</title>
<head>
<body>
<form action="login.spring"><pre>
user
name:<input
type="text" name="uname"/>
password
:<input
type="password" name="pass"/>
<input type ="submit" value="login"/>
</pre>
</form>
</body>
</html>
Hugo.jsp
<%@ page language="java"
import="java.util.*" pageEncoding="ISO-8859-1"%>
<html>
<body>
wel come to the softech home
page
user name:<%=request.getParameter("uname") %>
</body>
</html>
UserHome.jsp
<%@ page language="java"
import="java.util.*" pageEncoding="ISO-8859-1"%>
<html>
<body>
wel come to the non softech home page
user name:<%=request.getParameter("uname") %>
</body>
</html>
UserDetails.java
package
madhav;
public class
UserDetails {
private
String uname,pass;
public
String getUname() {
return uname;
}
public void
setUname(String uname) {
this.uname =
uname;
}
public
String getPass() {
return pass;
}
public void
setPass(String pass) {
this.pass =
pass;
}
}
LoginController.java
package
madhav;
import
org.springframework.web.servlet.*;
import
org.springframework.web.servlet.mvc.*;
import org.springframework.validation.*;
import
javax.servlet.http.*;
public class
LoginController extends AbstractCommandController
{
public
ModelAndView handle(HttpServletRequest
req,HttpServletResponse res,Object command,BindException errors)throws
Exception
{
String type;
UserDetails
user=((UserDetails)command);
type=user.getUname();
if(type==null)
return new
ModelAndView("/Login.html");
else if(type.equals("hugo"))
return new
ModelAndView("/hugo.jsp");
else
return new ModelAndView("/UserHome.jsp");
}
}
Dispatcher-servlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<bean id="mad"
class="madhav.LoginController">
<property name="commandClass">
<value type="java.lang.Class">
madhav.UserDetails
</value>
</property>
<property name="commandName">
<value>UserDetails</value>
</property>
</bean>
<bean id="myurlmapping"
class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="/login.spring">mad</prop>
</props>
</property>
</bean>
</beans>
Web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" id="WebApp_ID">
<display-name>speingmvcacc</display-name>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>*.spring</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
</welcome-file-list>
</web-app>
OUT PUT
SHORTS:
Example
program 3:
//this
program will describes working validator.
Login.html
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Softech
computer education</title>
<head>
<body>
<form action="login.spring"><pre>
user
name:<input
type="text" name="uname"/>
password
:<input
type="password" name="pass"/>
<input type ="submit" value="login"/>
</pre>
</form>
</body>
</html>
Hugo.jsp
<%@ page language="java"
import="java.util.*" pageEncoding="ISO-8859-1"%>
<html>
<body>
wel come to the softech home
page
user name:<%=request.getParameter("uname") %>
</body>
</html>
UserHome.jsp
<%@ page language="java"
import="java.util.*" pageEncoding="ISO-8859-1"%>
<html>
<body>
wel come to the non softech home page
user name:<%=request.getParameter("uname") %>
</body>
</html>
UserDetails.java
package
madhav;
public class
UserDetails {
private
String uname,pass;
public
String getUname() {
return uname;
}
public void
setUname(String uname) {
this.uname =
uname;
}
public
String getPass() {
return pass;
}
public void
setPass(String pass) {
this.pass =
pass;
}
}
LoginController.java
package
madhav;
import
org.springframework.web.servlet.*;
import
org.springframework.web.servlet.mvc.*;
import org.springframework.validation.*;
import
javax.servlet.http.*;
public class
LoginController extends AbstractCommandController
{
public
ModelAndView handle(HttpServletRequest
req,HttpServletResponse res,Object command,BindException errors)throws
Exception
{
if(errors.hasErrors())
{
PrintWriter
out=res.getWriter();
out.println("errors
has been found");
java.util.List<FieldError>
errorslist=errors.getFieldErrors("uname");
for(FieldError
error:errorslist)
{
out.println(error.getDefaultMessage());
}
errorslist=errors.getFieldErrors("pass");
for(FieldError
error:errorslist)
{
out.println(error.getDefaultMessage());
}
return null;
}
String type;
UserDetails
user=((UserDetails)command);
type=user.getUname();
if(type==null)
return new
ModelAndView("/Login.html");
else if(type.equals("hugo"))
return new
ModelAndView("/hugo.jsp");
else
return new
ModelAndView("/UserHome.jsp");
}
}
Dispatcher-servlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<bean id="mad"
class="madhav.LoginController">
<property name="commandClass">
<value type="java.lang.Class">
madhav.UserDetails
</value>
</property>
<property name="commandName">
<value>UserDetails</value>
</property>
<property name="validatior">
<bean
clss=”madhav.UserValidator”>
</property>
</bean>
<bean id="myurlmapping"
class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="/login.spring">mad</prop>
</props>
</property>
</bean>
</beans>
Web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" id="WebApp_ID">
<display-name>speingmvcacc</display-name>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>*.spring</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
</welcome-file-list>
</web-app>
OUT PUT SHORTS:
Example program 4:
//this program will describes using “simpleformcontroler”
index.html:
<!DOCTYPE html>
<html>
<head>
<title>indexl.html</title>
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="this is my page">
<meta http-equiv="content-type" content="text/html; charset=ISO-8859-1">
<!--<link rel="stylesheet"
type="text/css" href="./styles.css">-->
</head>
<body>
This is my HTML page. <br>
<a href="">click hear for SimpleFsormController</a>
</body>
</html>
FormDetials.jsp
<%@ page language="java"
contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://www.springframework.org/tags/form"
prefix="form"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Registration
Page</title>
</head>
<body bgcolor="pink">
<form:form method="POST" commandName="user">
<table>
<tr>
<td>Enter
Ur Name :</td>
<td><form:input path="name" /></td>
</tr>
<tr>
<td>Enter
Address</td>
<td><form:input path="address" /></td>
</tr>
<tr>
<td>Enter
Mail ID</td>
<td><form:input path="mailId" /></td>
</tr>
<tr>
<td colspan="2"><input type="submit"
value="submit"></td>
</tr>
</table>
</form:form>
</body>
</html>
SuccessPage.jsp
<%@ page language="java"
contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Success
Page</title>
</head>
<body bgcolor="gray">
Your
Details
<hr>
User
Name : ${user.name} <br/>
Address : ${user.address} <br/>
Mail
Id : ${user.mailId} <br/>
</body>
</html>
Stuednt.java
package
madhav;
public class
Student {
private
String name;
private
String address;
private
String mailId;
public
String getName() {
return name;
}
public void
setName(String name) {
this.name =
name;
}
public void setAddress(String
address) {
this.address =
address;
}
public
String getAddress() {
return address;
}
public void
setMailId(String mailId) {
this.mailId =
mailId;
}
public
String getMailId() {
return mailId;
}
}
Controler.java
package madhav;
import
org.springframework.web.servlet.ModelAndView;
import
org.springframework.web.servlet.mvc.SimpleFormController;
@SuppressWarnings("deprecation")
public class
Controller extends SimpleFormController {
public
Controller() {
setCommandClass(Student.class);
System.out.println("Student
controller");
setCommandName("user");
System.out.println("user");
}
protected
ModelAndView onSubmit(Object command) throws
Exception {
Student user =
(Student) command;
//userService.add(user);
System.out.println("model
and view");
return new
ModelAndView("SuccessPage","user",user);
}
}
Dispatcher-servlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/WEB-INF/jspfiles/"
p:suffix=".jsp" />
<bean name="/userRegistration.htm" class="madhav.Controller"
p:formView="FormDetails" p:successView="SuccessPage"/>
</beans>
Web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>SpringExample6</display-name>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>*.htm</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>redirect.jsp</welcome-file>
</welcome-file-list>
</web-app>
OUT PUT SHORTS:
Ex 5:
// this example program willdescribes-
MultiActionController
Index.jsp
<%@ page language="java"
import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
String
path = request.getContextPath();
String
basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My
JSP 'index.jsp' starting page</title>
<link rel="stylesheet"
type="text/css" href="styles.css">
-->
</head>
<body bgcolor="wheat">
This is my JSP page. <br>
<a href="sayHai.spring">click hea to say
hai</a><br>
<a href="sayBye.spring">click hear to say bye</a><br>
<a href="login.spring">click hear for login
into softech </a><br>
</body>
</html>
Hai.jsp
<%@ page language="java"
import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
String
path = request.getContextPath();
String
basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My
JSP 'Hai.jsp' starting page</title>
<link rel="stylesheet"
type="text/css" href="styles.css">
-->
</head>
<body bgcolor="wheat">
hai this is madhav <br>welcome
to softech<br>
</body>
</html>
Login.jsp
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Softech
computer education</title>
<head>
<body bgcolor="wheat">
<form action="login.spring"><pre>
user
name:<input
type="text" name="uname"/>
password
:<input
type="password" name="pass"/>
<input type ="submit" value="login"/>
</pre>
</form>
</body>
</html>
Bte.jsp
<%@ page language="java"
import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
String
path = request.getContextPath();
String
basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My
JSP 'Bye.jsp' starting page</title>
<link rel="stylesheet"
type="text/css" href="styles.css">
-->
</head>
<body bgcolor="wheat">
Bye...............<br>
</body>
</html>
StudentController.java
package
madhav;
import
org.springframework.web.servlet.*;
import
org.springframework.web.servlet.mvc.*;
import
org.springframework.web.servlet.mvc.multiaction.MultiActionController;
import
org.springframework.web.servlet.mvc.multiaction.ParameterMethodNameResolver;
import
org.springframework.validation.*;
import
javax.servlet.http.*;
public
class StudentController extends MultiActionController{
public ModelAndView
sayHai(HttpServletRequest req,HttpServletResponse res)throws Exception
{
return
new ModelAndView("/Hai");
}
public ModelAndView
sayBye(HttpServletRequest req,HttpServletResponse res)throws Exception
{
return
new ModelAndView("/Bye");
}
public ModelAndView login(HttpServletRequest
req,HttpServletResponse res)throws Exception
{
String
type=(String)req.getParameter("name");
if(type==null)
return
new ModelAndView("/Login");
else
if(type.equals("hugo"))
return
new ModelAndView("/hugo");
else
return
new ModelAndView("/UserHome");
}
}
Servlet-dispatcher.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/WEB-INF/jspfiles/" p:suffix=".jsp" />
<bean name="/*.spring" class="madhav.StudentController">
</bean>
</beans>
Web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" id="WebApp_ID">
<display-name>speingmvcacc</display-name>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>*.spring</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
Output screens:
click hear to download the above example program
TUTIORALS FROM MADHAV:
No comments:
Post a Comment