以下内容翻译自: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
- /user/home.htm被请求,DispatcherServlet将请求转发到UserController home()方法。
- /user/add.htm被请求,DispatcherServlet将请求转发给UserController add()方法。
- /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
时间: 2024-10-14 10:42:23