TUTIORALS FROM MADHAV:
Spring MVC "MultiActrionController"
Spring MVC:
Tepes of controllers:
- · Controller
- AbstractCommandController
- · SimpleFormController
- · WizardFormController
- · MultiActionController
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:
Related topics:
5. Spring MVC WizardFormController
TUTIORALS FROM MADHAV: