📜  Spring MVC-属性方法名称解析器示例

📅  最后修改于: 2020-11-11 06:30:43             🧑  作者: Mango


以下示例显示了如何使用Spring Web MVC框架使用Multi Action Controller的Properties Method Name Resolver方法。 MultiActionController类有助于分别在单个控制器中映射多个URL及其方法。

package com.tutorialspoint;

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 home(HttpServletRequest request,
      HttpServletResponse response) throws Exception {
      ModelAndView model = new ModelAndView("user");
      model.addObject("message", "Home");
      return model;
   }

   public ModelAndView add(HttpServletRequest request,
      HttpServletResponse response) throws Exception {
      ModelAndView model = new ModelAndView("user");
      model.addObject("message", "Add");
      return model;
   }

   public ModelAndView remove(HttpServletRequest request,
      HttpServletResponse response) throws Exception {
      ModelAndView model = new ModelAndView("user");
      model.addObject("message", "Remove");
      return model;
   }
}

   
      
         
            
               home
               add
               update      
            
         
      
   

例如,使用以上配置,如果URI-

  • /user/home.htm被请求,DispatcherServlet会将请求转发到UserController home()方法。

  • /user/add.htm被请求,DispatcherServlet会将请求转发到UserController的add()方法。

  • /user/remove.htm被请求时,DispatcherServlet会将请求转发到UserController的remove()方法。

首先,让我们拥有一个运行良好的Eclipse IDE,并考虑以下步骤,以使用Spring Web Framework开发基于动态表单的Web应用程序。

Step Description
1 Create a project with a name TestWeb under a package com.tutorialspoint as explained in the Spring MVC – Hello World chapter.
2 Create Java class UserController under the com.tutorialspoint package.
3 Create a view file user.jsp under the jsp sub-folder.
4 The final step is to create the content of the source and configuration files and export the application as explained below.

UserController.java

package com.tutorialspoint;

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 home(HttpServletRequest request,
      HttpServletResponse response) throws Exception {
      ModelAndView model = new ModelAndView("user");
      model.addObject("message", "Home");
      return model;
   }

   public ModelAndView add(HttpServletRequest request,
      HttpServletResponse response) throws Exception {
      ModelAndView model = new ModelAndView("user");
      model.addObject("message", "Add");
      return model;
   }

   public ModelAndView remove(HttpServletRequest request,
      HttpServletResponse response) throws Exception {
      ModelAndView model = new ModelAndView("user");
      model.addObject("message", "Remove");
      return model;
   }
}

TestWeb-servlet.xml



   
      
      
   

    
      
   
   
      
         
            
               
                  home
                  add
                  update      
               
            
         
      
     

user.jsp

Hello World
   
   
      

${message}

完成创建源文件和配置文件后,导出应用程序。右键单击您的应用程序,使用“导出”→“ WAR文件”选项,并将TestWeb.war文件保存在Tomcat的webapps文件夹中。

现在,启动Tomcat服务器,并确保您能够使用标准浏览器从webapps文件夹访问其他网页。现在,尝试使用URL- http:// localhost:8080 / TestWeb / user / add.htm ,如果Spring Web Application一切正常,我们将看到以下屏幕。

弹簧多动作控制器