gpt4 book ai didi

java - 当我将 'extends MultiActionController' 添加到 Controller 类名时无法使用 @RequestMapping

转载 作者:行者123 更新时间:2023-12-01 10:34:30 25 4
gpt4 key购买 nike

当我使用@RequestMapping而不使用extends MultiActionController时,它可以像@RequestMapping("/home")一样正常工作。因此,当我输入 http://localhost:8080/AnotationMultiactionDemo/home 时,它工作正常,但是当我将 extends MultiActionController 添加到我的 Controller 类名时,它停止工作。不知道为什么。谁能解释一下这一点吗?

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>

ma​​inController.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将请求映射到特定的 Controller 方法。因为MultiActionController的功能是能够根据方法的名称映射请求,然后调用相应的方法。例如:

  • 如果请求是theProjectName/add1.htm --> 将映射到 Controller 中的add1( ....) 方法
  • 如果请求是theProjectName/add2.htm --> 将映射到 Controller 中的add2( ....) 方法

您当然可以使用 RequestMapping 注释该方法,但这样做没有意义,因为多操作 Controller 只会搜索合适的方法名称。使用 MultiActionController 类的要点是您不需要为每个操作创建新的 Controller 类。

这是我创建的简单演示(因为它可能对您有用),它将提供有关此 MultiActionController 工作原理的图片。

enter image description here

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");
}

}

调度程序-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>

重定向.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<a href="add1.htm" >Add1</a> <br>
<a href="add2.htm" >Add2</a>

关于java - 当我将 'extends MultiActionController' 添加到 Controller 类名时无法使用 @RequestMapping,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34860220/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com