簡體   English   中英

當我在控制器類名稱中添加“ extendeds MultiActionController”時,無法使用@RequestMapping

[英]Unable to use @RequestMapping when I add 'extends MultiActionController' to controllers class name

當我使用@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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM