zoukankan      html  css  js  c++  java
  • Spring MVC-控制器(Controller)-属性方法名称解析器(Properties Method Name Resolver )示例(转载实践)

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

    说明:示例基于Spring MVC 4.1.6

    以下示例显示如何使用Spring Web MVC框架使用多操作控制器的属性方法名称解析方法。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;
       }
    }
    <bean class="com.tutorialspoint.UserController">
       <property name="methodNameResolver">
          <bean class="org.springframework.web.servlet.mvc.multiaction.PropertiesMethodNameResolver">
             <property name="mappings">
                <props>
                   <prop key="/user/home.htm">home</prop>
                   <prop key="/user/add.htm">add</prop>
                   <prop key="/user/remove.htm">update</prop>      
                </props>
             </property>
          </bean>
       </property>
    </bean>

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

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

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

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

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

    步骤描述
    1 创建一个名称的项目TestWeb包下com.tutorialspoint中所解释的Spring MVC - Hello World示例章节。
    2 在com.tutorialspoint包下创建Java类UserController。
    3 在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("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

    <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.mvc.support.ControllerClassNameHandlerMapping"> 
          <property name="caseSensitive" value="true" />
       </bean>
       <bean class="com.tutorialspoint.UserController">
          <property name="methodNameResolver">
          <bean class="org.springframework.web.servlet.mvc.multiaction.PropertiesMethodNameResolver">
             <property name="mappings">
                <props>
                   <prop key="/user/home.htm">home</prop>
                   <prop key="/user/add.htm">add</prop>
                   <prop key="/user/remove.htm">update</prop>      
                </props>
             </property>
          </bean>
          </property>
       </bean>  
    </beans>

    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/user/add.htm,如果您的Spring Web应用程序的一切都很好,您应该会看到以下结果:

    Maven示例:

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

  • 相关阅读:
    CGO入门和OCR文字识别(非第三方API,有源码,效果好)实战
    Golang中如何正确的使用sarama包操作Kafka?
    音量强度转分贝db
    ShowDialog()弹出的窗体,关闭后,主窗体会闪烁的BUG
    小鱼提问3 static方法中可以访问某个类的私有变量吗(不通过反射的其他非正常手段)?什么情况下可以?
    “-="的陷阱
    c++ 从一个BYTE[] *filePtr 追加二进制文件
    Android解决程序切换后台被干掉,恢复状态问题
    Cookie
    dede后台搜索标签
  • 原文地址:https://www.cnblogs.com/EasonJim/p/7492667.html
Copyright © 2011-2022 走看看