当我在没有@RequestMapping
的情况下使用extends MultiActionController
时,它可以像@RequestMapping("/home")
一样正常工作。因此,当我键入http://localhost:8080/AnotationMultiactionDemo/home
时,它可以正常工作,但是当我在控制器的类名中添加extends MultiActionController
时,它将停止工作。不知道为什么。有人可以解释吗?
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;
}
}
最佳答案
实际上,您不需要RequestMapping将请求映射到某些控制器方法。因为MultiActionController的功能能够根据方法的名称映射请求,然后调用相应的方法。
例如:
如果请求是theProjectName/add1.htm
->将映射到控制器中的add1( ....)
方法
如果请求是theProjectName/add2.htm
->将被映射到控制器中的add2( ....)
方法
您当然可以使用RequestMapping注释该方法,但是这样做没有任何意义,因为多动作控制器只会搜索合适的方法名称。使用MultiActionController类的要点是,您无需为每个操作创建新的控制器类。
这是我创建的简单演示(可能对您有用),将给出有关此MultiActionController工作原理的图片。
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>