MappingDispatchAction 的使用&structs-config.xml配置&spring xml文件配置

     刚开始一直使用的是一个Action类处理一个请求,单需要处理多个请求时候,就需要编写N个Action来进行处理,这样就使得要编写的类增多。于是用了下MappingDispatchAction来写,继承自MappingDispatchAction后,便可以在一个action类里编写多个方法,来分别对应处理多个请求,由于这样就不同于普通的Action类,在structs-config配置文件中需要在<action>的配置参数里添加parameter属性来为每一个MappingDispatchAction里处理方法配置一次,而当要把这个MappingDispatchAction注入到spring的时候,也就需要为每一个请求处理方法配置一个action bean。如:

 

   继承自MappingDispatchAction的UserAdminAction类:

  public class UserAdminAction extends MappingDispatchAction { public ActionForward query(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) { return null; } public ActionForward add(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) { return mapping.findForward("SUCCESS"); } public ActionForward update(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) { System.out.println("update action is working!"); return mapping.findForward("SUCCESS"); } public ActionForward remove(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) { return mapping.findForward("SUCCESS"); } }

  structs-config.xml文件中的配置:关键是parameter属性

  <action path="/query" attribute="queryForm" name="queryForm" parameter="query" type="org.springframework.web.struts.DelegatingActionProxy"> <forward name="MAIN" path="/fapiao/baseInfo/userAdmin.jsp"></forward> </action> <action path="/add" attribute="userInfoForm" name="userInfoForm" parameter="add" type="org.springframework.web.struts.DelegatingActionProxy"> <forward name="ADDMAIN" path="/fapiao/baseInfo/UserInfo.jsp"/> <forward name="SUCCESS" path="/fapiao/baseInfo/addOk.jsp"/> <forward name="ERROR" path="/fapiao/baseInfo/error.jsp"/> </action> <action path="/update" attribute="userInfoForm" name="userInfoForm" parameter="update" type="org.springframework.web.struts.DelegatingActionProxy"> <forward name="SUCCESS" path="/fapiao/baseInfo/modifyOk.jsp"/> <forward name="ERROR" path="/fapiao/baseInfo/error.jsp"/> <forward name="ERROR" path="/fapiao/baseInfo/error.jsp"/> </action> <action path="/remove" parameter="remove" type="org.springframework.web.struts.DelegatingActionProxy"> <forward name="REMOVEMAIN" path="/fapiao/baseInfo/removeUser.jsp"></forward> <forward name="SUCCESS" path="/fapiao/baseInfo/removeOk.jsp"></forward> <forward name="ERROR" path="/fapiao/baseInfo/error.jsp"></forward> </action>

 spring配置文件的配置:

 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd" default-autowire="byName"> <bean name="/query" class="fapiao.struts.action.UserAdminAction"/> <bean name="/add" class="fapiao.struts.action.UserAdminAction"/> <bean name="/update" class="fapiao.struts.action.UserAdminAction"/> <bean name="/remove" class="fapiao.struts.action.UserAdminAction"/> <bean id="modifyUserForwardAction" name="/modifyUserForward" class="fapiao.struts.action.ModifyUserForwardAction"/> </beans>

  

  

你可能感兴趣的:(spring,xml,bean,Class,action,Path)