当前位置: 首页 > 工具软件 > sparrow-mvc > 使用案例 >

spring 3 mvc中的controller小结

丁淇
2023-12-01
  本文小结下spring 3 MVC中常见的几个controller相关的

1  MultiActionController
  比如在一个controller中,可以设置增删改的操作,都可以放这里:
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 CustomerController extends MultiActionController{

public ModelAndView add(HttpServletRequest request,
HttpServletResponse response) throws Exception {

return new ModelAndView("CustomerPage", "msg","add() method");

}

public ModelAndView delete(HttpServletRequest request,
HttpServletResponse response) throws Exception {

return new ModelAndView("CustomerPage", "msg","delete() method");

}

public ModelAndView update(HttpServletRequest request,
HttpServletResponse response) throws Exception {

return new ModelAndView("CustomerPage", "msg","update() method");

}

public ModelAndView list(HttpServletRequest request,
HttpServletResponse response) throws Exception {

return new ModelAndView("CustomerPage", "msg","list() method");

}

}

<beans ...>

<bean
  class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping" />

  <bean class="com.mkyong.common.controller.CustomerController" />

</beans>

下面的形成匹配:
/customer/add.htm –> add()
/customer/delete.htm –> delete()
/customer/update.htm –> update()
/customer/list.htm –> list()
而InternalPathMethodNameResolver是MultiActionController的默认实现,
<beans ...>
<bean
  class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping" />

  <bean class="com.mkyong.common.controller.CustomerController">
     <property name="methodNameResolver">
<bean class="org.springframework.web.servlet.mvc.multiaction.InternalPathMethodNameResolver">
   <property name="prefix" value="test" />
   <property name="suffix" value="Customer" />
</bean>
     </property>
   </bean>
</beans>
增加了前缀和后缀,比如:
/customer/add.htm –> testaddCustomer()
/customer/delete.htm –> testdeleteCustomer()
/customer/update.htm –> testupdateCustomer()
/customer/list.htm –> testlistCustomer()
如果用annotation则更方便了,上面的变为:
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class CustomerController{

@RequestMapping("/customer/add.htm")
public ModelAndView add(HttpServletRequest request,
HttpServletResponse response) throws Exception {

return new ModelAndView("CustomerAddView");

}
2 PropertiesMethodNameResolver
  这个可以配合multication去自定义匹配:
如:
<beans ...>

<bean
  class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping" />

<bean class="com.mkyong.common.controller.CustomerController">
   <property name="methodNameResolver">
    <bean class="org.springframework.web.servlet.mvc.multiaction.PropertiesMethodNameResolver">
      <property name="mappings">
<props>
   <prop key="/customer/a.htm">add</prop>
   <prop key="/customer/b.htm">update</prop>
   <prop key="/customer/c.htm">delete</prop>
   <prop key="/customer/d.htm">list</prop>
   <prop key="/customer/whatever.htm">add</prop>
</props>
       </property>
     </bean>
    </property>
  </bean>

</beans>

3 ParameterMethodNameResolver
  这个实际上是将新形如XXXX.XXX?ACTION=ADD后的action=add部分映射到
mulitication的又一个方法,很简单,比如:
<beans ...>

<bean
  class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping" />

  <bean class="com.mkyong.common.controller.CustomerController">
     <property name="methodNameResolver">
<bean class="org.springframework.web.servlet.mvc.multiaction.ParameterMethodNameResolver">
   <property name="paramName" value="action"/>
</bean>
     </property>
   </bean>

</beans>
/customer/*.htm?action=add –> add() method
/customer/whatever.htm?action=add –> add() method
/customer/*.htm?action=update –> update() method
/customer/*.htm?action=delete –> delete() method
/customer/*.htm?action=list –> list() method


4 ParameterizableViewController
一般要经过CONTROLLER才能返回一个VIEW,比如:
public class WelcomeController extends AbstractController{

@Override
protected ModelAndView handleRequestInternal(HttpServletRequest request,
HttpServletResponse response) throws Exception {

ModelAndView model = new ModelAndView("WelcomePage");
return model;

}

}
但现在实际不用了:
<beans xmlns="http://www.springframework.org/schema/beans"
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-2.5.xsd">

    <bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
        <property name="mappings">
            <props>
                <prop key="/welcome.htm">welcomeController</prop>
            </props>
        </property>
    </bean>

   <bean name="welcomeController"
            class="org.springframework.web.servlet.mvc.ParameterizableViewController">
    <property name="viewName" value="WelcomePage" />
   </bean>

当访问http://localhost/welcome.htm后,直接跳转到WelcomePage.jsp页面了
 类似资料: