Reputation: 5283
When I use @RequestMapping
without extends MultiActionController
it is properly working like @RequestMapping("/home")
. So, when I type in http://localhost:8080/AnotationMultiactionDemo/home
it is working properly but when I add extends MultiActionController
to my controllers class name it stops working. Don't know why. Can any one explain this?
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"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<context:component-scan base-package="MylController"/>
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/WEB-INF/jsp/"
p:suffix=".jsp" />
</beans>
mainController.java
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package MylController;
import Utilities.SqlServices;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;
/**
*
* @author Juned Ansari
*/
@Controller
public class mainController extends MultiActionController{
@RequestMapping("/home")
public ModelAndView showIndex() {
ModelAndView mav = new ModelAndView();
mav.setViewName("index");
mav.addObject("msg", "Hello Index");
return mav;
}
@RequestMapping(value = "/calculate", method = RequestMethod.POST)
public ModelAndView calc(HttpServletRequest req, HttpServletResponse res) {
ModelAndView mav = new ModelAndView();
mav.setViewName("index");
int c= Integer.parseInt(req.getParameter("txtno1"))+ Integer.parseInt(req.getParameter("txtno2"));
mav.addObject("res", c);
return mav;
}
@RequestMapping(value = "/getstudlist", method = RequestMethod.GET)
public ModelAndView getStudlst(HttpServletRequest req, HttpServletResponse res){
ModelAndView mav = new ModelAndView("stud");
try
{
SqlServices objSqlServices=new SqlServices(getServletContext().getRealPath("WEB-INF/applicationContext.xml"));
mav.addObject("data",objSqlServices.getLists("select * from stud"));
}
catch(Exception ex)
{
System.out.println(ex.toString());
}
return mav;
}
}
Upvotes: 0
Views: 392
Reputation: 3099
Actually you don't need RequestMapping to map the request to certain controller method. Because the functionality of the MultiActionController is capable of mapping the requests based on method's names and then invoking the corresponding method. ex:
theProjectName/add1.htm
--> will be mapped to add1( ....)
method in controllertheProjectName/add2.htm
--> will be mapped to add2( ....)
method in controllerYou can of course annotate the method with RequestMapping but then no point of doing that, because the multiaction controller will just search for the suitable method name. The main point of using MultiActionController class is that you do not need to create new controller class for each action.
Here is simple demo I created (as it maybe useful for you )that will give a picture about how this MultiActionController works.
UserController.java
package com;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;
public class UserController extends MultiActionController {
public ModelAndView add1(HttpServletRequest request,
HttpServletResponse response) {
return new ModelAndView("page1", "message", "hi this is page1");
}
public ModelAndView add2(HttpServletRequest request,
HttpServletResponse response) {
return new ModelAndView("page2", "message", "hi this is page2");
}
}
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/jsp/" p:suffix=".jsp" />
<bean name="/*.htm" class="com.UserController" />
</beans>
page1.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>
${message}
</body>
</html>
page2.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>
${message}
</body>
</html>
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>SpringExample10</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>
redirect.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<a href="add1.htm" >Add1</a> <br>
<a href="add2.htm" >Add2</a>
Upvotes: 1