SpringMVC的控制器MultiActionController

朱华皓
2023-12-01

在SpringMVC中通过控制器继承MultiActionController,来实现单个控制器中分别映射多个URL到对应的方法。

 

  最关键的一点:在实际中使用不多,了解即可

 

 (一)通过参数方法名称解析器

<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">

<!--    <context:component-scan base-package="com.sdga" /> -->

   <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 id="userController" class="com.sdga.controller.UserController">  
      <property name="methodNameResolver" >
      <bean class="org.springframework.web.servlet.mvc.multiaction.ParameterMethodNameResolver">
          <property name="paramName" value="action"/>
     
 </bean>
      </property>
     </bean>       
</beans>

(二)通过属性方法名称解析器。 

 

<bean class="com.yiibai.springmvc.UserController">
   <property name="methodNameResolver">
      <bean class="org.springframework.web.servlet.mvc.multiaction.PropertiesMethodNameResolver">
         <property name="mappings">
            <props>
               <prop key="/user/home.html">home</prop>
               <prop key="/user/add.html">add</prop>
               <prop key="/user/update.html">update</prop>      
            </props>
         </property>
      </bean>
   </property>
</bean>bean class="com.yiibai.springmvc.UserController">
   <property name="methodNameResolver">
      <bean class="org.springframework.web.servlet.mvc.multiaction.PropertiesMethodNameResolver">
         <property name="mappings">
            <props>
               <prop key="/user/home.html">home</prop>
               <prop key="/user/add.html">add</prop>
               <prop key="/user/update.html">update</prop>      
            </props>
         </property>
      </bean>
   </property>
</bean>
(三)通过可参数化的视图控制器

 

 

<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
   <property name="mappings">
      <value>
         index.html=userController
      </value>
   </property>
</bean>
<bean id="userController" class="org.springframework.web.servlet.mvc.ParameterizableViewController">
   <property name="viewName" value="index"/>
</bean>

 

 类似资料: