如果为每个请求都设计一个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