Spring-MultiActionController

如果为每个请求都设计一个controller的话,随着开发的进行,工程也一定会日渐庞大。当然,Spring框架提供了避免这种情况出现的方法,那就是使你的controller继承自 org.springframework.web.servlet.mvc.multiaction.MultiActionController。就像Struts里的DispatchAction一样,MultiActionController可以处理多个类似的请求。
   在Struts里,要使用DispatchAction只需要在其action的定义中加入parameter="method"即可。在Spring中,MultiActionController要配合一个 org.springframework.web.servlet.mvc.multiaction.MethodNameResolver的实例使用(MethodNameResolver也是一个接口),但我个人认为这并不说明Spring比struts繁琐,在使用中自会体会到它的好处。
   MultiActionController的默认MethodNameResolver是 org.springframework.web.servlet.mvc.multiaction.InternalPathMethodNameResolver。这个实现类是依据请求的路径来决定执行MultiActionController的那个方法的。例如路径为:getlist.htm,则执行相应controller的getlist方法。在实际开发中,不提倡使用这个MethodNameResolver。
    一般情况下,MultiActionController搭配的MethodNameResolver是 org.springframework.web.servlet.mvc.multiaction.ParameterMethodNameResolver org.springframework.web.servlet.mvc.multiaction.PropertiesMethodNameResolver ,它们都是根据请求的参数来决定使用那个方法

UrlFileBuildAction.java
package org.dispenseModule.web;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.dispenseModule.service.IUrlsService;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;
public class UrlFileBuildAction extends MultiActionController{
 private String fail_view;
 private String success_view;
 
 private IUrlsService urlsService;
 
 public void setFail_view(String fail_view) {
  this.fail_view = fail_view;
 }
 public void setSuccess_view(String success_view) {
  this.success_view = success_view;
 }
   
 public void setUrlsService(IUrlsService urlsService) {
  this.urlsService = urlsService;
 }
 public ModelAndView htmlFile(HttpServletRequest request,
   HttpServletResponse response) throws Exception {
       String urls=urlsService.getUrls("html");
       String filePath="F:/urlFile";
       try{
    File myFilePath = new File(filePath);
        if (!myFilePath.exists()) {
          myFilePath.mkdirs();
        }
        FileWriter write=new FileWriter(filePath+"/htmlUrl.txt");
        BufferedWriter writer=new BufferedWriter(write);
         writer.write(urls);
         writer.flush();
         String success = "生成成功";
         writer.close();
         writer.close();
         return new ModelAndView(this.success_view,"success",success);
       }catch(Exception e){
        String fail = "生成失败";
        return new ModelAndView(this.fail_view, "fail", fail);
       }
   
 }
 
 public ModelAndView ubbFile(HttpServletRequest request,
   HttpServletResponse response) throws Exception {
       String urls=urlsService.getUrls("ubb");
       String filePath="F:/urlFile";
       try{
    File myFilePath = new File(filePath);
        if (!myFilePath.exists()) {
          myFilePath.mkdirs();
        }
        FileWriter write=new FileWriter(filePath+"/ubbUrl.txt");
        BufferedWriter writer=new BufferedWriter(write);
         writer.write(urls);
         writer.flush();
         String success = "生成成功";
         writer.close();
         writer.close();
         return new ModelAndView(this.success_view,"success",success);
       }catch(Exception e){
        String fail = "生成失败";
        return new ModelAndView(this.fail_view, "fail", fail);
       }
   
 }
}
一、使用ParameterMethodNameResolver
1、配置applicationContext.xml
<bean id="methodNameResolver" class="org.springframework.web.servlet.mvc.multiaction.ParameterMethodNameResolver">
 <property name="paramName">
   <value>method</value>
  </property>
  <property name="defaultMethodName">
   <value>htmlFile</value>
  </property>
我在测试过程中,如何没有设置defaultMethodName,都不能通过
2、配置applicationContext-action.xml
 <bean id="urlFileBuildAction" class="org.dispenseModule.web.UrlFileBuildAction">
       <property name="fail_view">
      <value>fail</value>
  </property>
  <property name="success_view">
   <value>success</value>
  </property>
  <property name="urlsService">
   <ref bean="urlsService" />
  </property>
  <property name="methodNameResolver">
    <ref bean="methodNameResolver"/>
  </property>
   </bean>

<bean id="handlerMapping"
  class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
  <!--  <property name="defaultHandler" ref="urlFileBuildAction"/>-->
  <property name="mappings">
   <props>
<prop key="/urlFileBuildAction.do">urlFileBuildAction</prop>
   </props>
  </property>
 </bean>
使用方法urlFileBuildAction.do?method=htmlFile

二、使用PropertiesMethodNameResolver

1、配置applicatonContext.xml

<bean id="methodNameResolver" class="org.springframework.web.servlet.mvc.multiaction.PropertiesMethodNameResolver">
 <property name="mappings">
<props>
<prop key="/urlhtmlFileBuildAction.do">htmlFile</prop>

<prop key="/urlubbFileBuildAction.do">ubbFile</prop>
</props>
</property>
</bean>

2、配置applicatonContext-action.xml

<bean id="handlerMapping"
class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="defaultHandler" ref=" sampleMultiMethodController "/>
</bean>

 <bean id="urlFileBuildAction" class="org.dispenseModule.web.UrlFileBuildAction">
       <property name="fail_view">
      <value>fail</value>
  </property>
  <property name="success_view">
   <value>success</value>
  </property>
  <property name="urlsService">
   <ref bean="urlsService" />
  </property>
  <property name="methodNameResolver">
    <ref bean="methodNameResolver"/>
  </property>
   </bean>

使用urlhtmlFileBuildAction.do

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值