public class MyController extends MultiActionController { // 新增 方法修饰符要是public public ModelAndView add(HttpServletRequest request, HttpServletResponse response) throws Exception { return new ModelAndView("/WEB-INF/jsp/success.jsp", "result", "add()"); } // 修改 public ModelAndView update(HttpServletRequest request, HttpServletResponse response) throws Exception { return new ModelAndView("/WEB-INF/jsp/success.jsp", "result", "update()"); } }
创建对应的controller
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:mvc="http://www.springframework.org/schema/mvc" 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/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"> <!-- 配置一个处理器映射器 --> <bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"> <property name="urlMap"> <map> <!-- 因为我们的controller继承了MultiActionController, 在MultiActionController有个 /** Delegate that knows how to determine method names from incoming requests */ private MethodNameResolver methodNameResolver = new InternalPathMethodNameResolver(); 根据我们的请求中参数找到对应方法 的解析器 *:代表的就是我们的请求参数 也是对应的方法名 --> <entry key="/user/*" value="myController"/> </map> </property> </bean> <!-- 处理器 --> <bean id="myController" class="cn.bdqn.controller.MyController"> <property name="methodNameResolver" ref="myResolver"></property> </bean> </beans>
mvc核心xml文件的配置
使用我们自身设置的解析器 来执行,不需要改变controller中的代码!只需要更改mvc核心xml文件
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:mvc="http://www.springframework.org/schema/mvc" 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/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"> <!-- 配置一个处理器映射器 --> <bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"> <property name="urlMap"> <map> <!-- 因为我们的controller继承了MultiActionController, 在MultiActionController有个 /** Delegate that knows how to determine method names from incoming requests */ private MethodNameResolver methodNameResolver = new InternalPathMethodNameResolver(); 根据我们的请求中参数找到对应方法 的解析器 *:代表的就是我们的请求参数 也是对应的方法名 --> <entry key="/user/*" value="myController"/> </map> </property> </bean> <!-- 设置解析器 --> <bean id="myResolver" class="org.springframework.web.servlet.mvc.multiaction.PropertiesMethodNameResolver"> <property name="mappings"> <props> <!-- 底层代码 Set explicit URL to method name mappings through a Properties object. @param mappings Properties with URL as key and method name as value public void setMappings(Properties mappings) { this.mappings = mappings; } --> <prop key="/user/adds">add</prop> <prop key="/user/updates">update</prop> </props> </property> </bean> <!-- 处理器 --> <bean id="myController" class="cn.bdqn.controller.MyController"> <!--需要更改父类的解析器引用 变成我们自已定义的解析器 --> <property name="methodNameResolver" ref="myResolver"/> </bean> </beans>
mvc核心xml文件的设置
一旦我们设置了解析器,那么默认的
会执行我们设置的解析器
PropertiesMethodNameResolver
在浏览器中输入对应的url即可看到效果!
时间: 2024-10-05 05:49:03