• Spring MVC-控制器(Controller)-多动作控制器(Multi Action Controller)示例(转载实践)


    以下内容翻译自:https://www.tutorialspoint.com/springmvc/springmvc_multiactioncontroller.htm

    说明:示例基于Spring MVC 4.1.6

    以下示例说明如何使用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;
       }
    }
       <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>
       <bean name="/home.htm" class="com.tutorialspoint.UserController" /> 
       <bean name="/user/*.htm" class="com.tutorialspoint.UserController" />

    例如,使用上面的配置,如果是URI

    1. /home.htm被请求,DispatcherServlet将请求转发给UserController home()方法。

    2. user/add.htm被请求,DispatcherServlet将请求转发给UserController add()方法。

    3. user/remove.htm被请求,DispatcherServlet将请求转发给UserController remove()方法。

    首先,让我们使用Eclipse IDE,并按照以下步骤使用Spring Web Framework开发基于动态窗体的Web应用程序:

    步骤描述
    1 创建一个名称的项目TestWeb包下com.tutorialspoint中所解释的Spring MVC - Hello World示例章节。
    2 在com.tutorialspoint包下创建Java类UserController。
    3 在jsp子文件夹下创建一个视图文件home.jsp,user.jsp。
    4 最后一步是创建所有源和配置文件的内容并导出应用程序,如下所述。

    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

    <beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="
       http://www.springframework.org/schema/beans     
       http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
       http://www.springframework.org/schema/context 
       http://www.springframework.org/schema/context/spring-context-3.0.xsd">
    
       <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
          <property name="prefix" value="/WEB-INF/jsp/"/>
          <property name="suffix" value=".jsp"/>
       </bean>
    
       <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>
       <bean name="/home.htm" class="com.tutorialspoint.UserController" /> 
       <bean name="/user/*.htm" class="com.tutorialspoint.UserController" />    
    </beans>

    home.jsp

    <%@ page contentType="text/html; charset=UTF-8" %>
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <title>Home</title>
    </head>
    <body>
    <body>
    <a href="user/add.htm" >Add</a> <br>
    <a href="user/remove.htm" >Remove</a>
    </body>
    </html>

    user.jsp

    <%@ page contentType="text/html; charset=UTF-8" %>
    <html>
    <head>
    <title>Hello World</title>
    </head>
    <body>
       <h2>${message}</h2>  
    </body>
    </html>

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

    现在启动您的Tomcat服务器,并确保您可以使用标准浏览器从webapps文件夹访问其他网页。现在尝试URL http://localhost:8080/TestWeb/home.htm,如果您的Spring Web应用程序的一切都很好,您应该会看到以下结果:

    尝试URL http://localhost:8080/TestWeb/user/add.htm,如果Spring Web应用程序的一切都很好,您应该会看到以下结果:

    Maven示例:

    https://github.com/easonjim/5_java_example/tree/master/springmvc/tutorialspoint/test19 

  • 相关阅读:
    Codeforces Round #502 (in memory of Leopoldo Taravilse, Div. 1 + Div. 2) E. The Supersonic Rocket
    Codeforces Round #500 (Div. 2) D
    AtCoder Grand Contest 026 D
    Codeforces Round #495 (Div. 2) Sonya and Matrix
    AtCoder Regular Contest 100 E
    1013 数素数
    1010 一元多项式求导(用while接收输入)
    1009 说反话(字符串、栈)
    L2-006 树的遍历 (后序中序求层序)
    L2-004 这是二叉搜索树吗?
  • 原文地址:https://www.cnblogs.com/EasonJim/p/7492624.html
Copyright © 2020-2023  润新知