📜  Spring MVC-多动作控制器示例

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


以下示例显示了如何通过Spring Web MVC框架使用Multi Action Controller。 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("home");
      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;
   }
}

 
 

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

  • /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 a Java class UserController under the com.tutorialspoint package.
3 Create view files home.jsp and 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("home");
      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.jsp

Home
   
   
   
      Add 
Remove

user.jsp

Hello World
   
   
      

${message}

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

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

弹簧多功能控制器1

尝试使用URL http:// localhost:8080 / TestWeb / user / add.htm ,如果Spring Web Application一切正常,我们将看到以下屏幕。

弹簧多动作控制器2