Controller实现类
AbstractController:使用Template Method模式,包括了对缓存标头的处理、检验对请求方法(POST、GET)的支持,Session的取得与同步化等。
使用方式:继承AbstractController类,覆盖handleRequestInternal()方法。
MultiActionController与ParameterMethodNameResolver
在一个Controller类中定义多个方法,并根据使用者的请求来执行当中的某个方法,相当于Struts的DispatcherAction。
使用MultiActionController需要配个一个MethodNameResolver实例,默认使用ParameterMethodNameResolver,他会根据所给的网址中,最后请求的文件名称来判断执行Controller中的哪一个方法,示例:
multiMethodController.java
1
public class MultiMethodController extends MultiActionController
{
2
private String viewPage;
3
4
public ModelAndView add(HttpServletRequest request, HttpServletResponse response)
{
5
return new ModelAndView(viewPage, "method", "add");
6
}
7
8
public ModelAndView delete(HttpServletRequest request, HttpServletResponse response)
{
9
return new ModelAndView(viewPage, "method", "delete");
10
}
11
12
public ModelAndView update(HttpServletRequest request, HttpServletResponse response)
{
13
return new ModelAndView(viewPage, "method", "update");
14
}
15
16
public void setViewPage(String viewPage)
{
17
this.viewPage = viewPage;
18
}
19
}
applicationContext.xml
1
<bean id="urlHandlerMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
2
<property name="mappings">
3
<props>
4
<prop key="/sayHello.do">sayHelloController</prop>
5
<prop key="/method.do">multiMethodController</prop>
6
<prop key="/login.do">loginController</prop>
7
<prop key="/wizard.do">wizardController</prop>
8
</props>
9
</property>
10
</bean>
11
12
<bean id="parameterMethodResolver" class="org.springframework.web.servlet.mvc.multiaction.ParameterMethodNameResolver">
13
<property name="paramName" value="action" />
14
<property name="defaultMethodName" value="add" />
15
</bean>
16
17
<bean id="multiMethodController" class="SpringMVC.MultiMethodController">
18
<property name="methodNameResolver" ref="parameterMethodResolver" />
19
<property name="viewPage" value="multiMethodTest" />
20
</bean>
PatameterizableViewController
可以直接在Bean文件中定义,设定它的viewName属性,当这个Controller执行时,会根据viewName的设定直接导向指定的网页,使用它的好处是可以用来避免直接对JSP页面请求。
AbstractCommandController
继承该类,并提供一个Command对象(如常见的User类),当请求来时,AbstractCommandController会试着对比请求参数名称与Command上的Setter方法名称,设定请求参数值。
SimpleFormController
SimpleFormController继承自AbstractFormController,对于简单的表单可以直接继承这个类,并重新定义onSubmit()或doSubmitAction()方法处理请求。以下是一个简单的登录页面示例:
LoginController.java
1
public class LoginController extends SimpleFormController
{
2
3
public LoginController()
{
4
this.setCommandClass(LoginForm.class);
5
}
6
7
protected ModelAndView onSubmit(Object command) throws Exception
{
8
LoginForm form = (LoginForm)command;
9
if("codingliyi".endsWith(form.getUsername()) &&
10
"123456".endsWith(form.getPassword()))
{
11
return new ModelAndView(getSuccessView(), "username", form.getUsername());
12
}else return new ModelAndView(getFormView());
13
}
14
}
15
applicationContext.xml
1
<bean id="loginController" class="SpringMVC.LoginController">
2
<property name="successView" value="loginSuccess" />
3
<property name="formView" value="login" />
4
</bean>
login.jsp
1
<form name="loginForm" action="login.do" method="POST">
2
用户名:<input type="text" name="username"/><br/>
3
密码:<input type="text" name="password"/><br/>
4
<input type="submit" value="登录" />
5
</form>
AbstractWizardFormController
AbstractWizardFormController用于设计多页表单。
可以继承该类,并重写processFinish()方法。一个多页表单的示例如下:
WizardController.java
1
public class WizardController extends AbstractWizardFormController
{
2
3
private String viewPage;
4
5
public WizardController()
{
6
this.setCommandClass(LoginForm.class);
7
}
8
@Override
9
protected ModelAndView processFinish(HttpServletRequest arg0,
10
HttpServletResponse arg1, Object command, BindException arg3)
11
throws Exception
{
12
LoginForm form = (LoginForm)command;
13
Map model = new HashMap();
14
model.put("username", form.getUsername());
15
model.put("password", form.getPassword());
16
return new ModelAndView(viewPage, model);
17
}
18
19
public void setViewPage(String viewPage)
{
20
this.viewPage = viewPage;
21
}
22
}
applicationContext.xml
1
<bean name="wizardController" class="SpringMVC.WizardController">
2
<property name="viewPage" value="wizardResult"/>
3
<property name="pages">
4
<list>
5
<value>question1</value>
6
<value>question2</value>
7
</list>
8
</property>
9
<property name=""></property>
10
</bean>
question2.jsp
1
<form action="wizard.do" method="POST">
2
你的密码:<input type="text" name="password" /><br/>
3
<input type="submit" value="previous" name="_target0" />
4
<input type="submit" value="finish" name="_finish" />
5
</form>
也可以在问卷中涉及一个cancel按钮,在按下该按钮后,送出"_cancel"请求参数,当请求中带有"_cancel"参数时,会执行processCancel()方法。
ThrowawayController
ThrowawayController并不在Controller的继承架构中,而是一个独立的接口定义。实现该接口的类不是以单例的方式产生并重复使用。ThrowawayController在每一次请求中都会产生新的对象,而且用了就丢,不会重复使用。
在设定Bean定义文件中,ThrowawayController必须将"singleton"属性设为false。