- acf
- 等级: 初级会员
- 文章: 9
- 积分: 30
|
在利用Spring MVC 时,或许会想到把MultiActionController和CommandController结全起来全用,使你的Controller功能更加强大。现在就有这样一个例子,能使你的一个Controller既可以处理多个URL请求,又可以接受form表单
Controller源码如下:
java 代码
java 代码
- package com.betop.probe.controller.study;
-
- import java.util.HashMap;
- import java.util.Map;
-
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
-
- import org.springframework.validation.BindException;
- import org.springframework.web.bind.ServletRequestDataBinder;
- import org.springframework.web.servlet.ModelAndView;
- import org.springframework.web.servlet.mvc.AbstractCommandController;
-
- import com.betop.probe.domain.MasterUser;
-
- public class UserMultiActionController3 extends AbstractCommandController{
-
- public UserMultiActionController3(){
- this.setCommandClass(MasterUser.class);
- }
-
- public ModelAndView demo1(HttpServletRequest req, HttpServletResponse resp)throws Exception {
-
- Map<String, Object> model = new HashMap<String, Object>();
- model.put("message", "这是MultiActioncontroller的第三种配置!");
- return new ModelAndView("study/MultiActionControllerView3.vm", model);
- }
-
- public ModelAndView demo2(HttpServletRequest req, HttpServletResponse resp)throws Exception{
-
-
- MasterUser user = getCommand(req, resp);
-
- Map<String, Object> model = new HashMap<String, Object>();
-
- model.put("user", user);
-
- return new ModelAndView("study/result.vm", model);
-
- }
-
- private MasterUser getCommand(HttpServletRequest req, HttpServletResponse resp) throws Exception{
-
- MasterUser command = (MasterUser)getCommand(req);
- ServletRequestDataBinder binder = bindAndValidate(req, command);
- BindException errors = new BindException(binder.getBindingResult());
-
- return command;
- }
-
- @Override
- protected ModelAndView handle(HttpServletRequest request,
- HttpServletResponse response, Object command, BindException errors)
- throws Exception {
-
- return null;
- }
-
- }
配置如下:
xml 代码
- <bean id="testMulti3" class="com.betop.probe.controller.study.UserMultiActionController3"/>
-
- <bean id="proxyController" class="org.springframework.web.servlet.mvc.multiaction.MultiActionController">
- <property name="methodNameResolver">
- <ref bean="paraMethodResolver"/>
- </property>
-
- <property name="delegate">
- <ref bean="testMulti3"/>
- </property>
- </bean>
-
- <bean id="SimpleUrlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
- <property name="mappings">
- <props>
- <prop key="/test/*.htm">proxyController</prop>
- </props>
- </property>
- </bean>
这样你就可以通过URL:http://host/project/test/demo2.htm来提交你的表单
URL:http://host/project/test/demo1.htm来访问另外一个方法。
上面的做法是通过 getCommand方法,借用了父类里的一些功能把请求参数转换为你想要的对象。
当然,你也可以不去写getCommand方法,对于方法demo2也可以写成如下:
java 代码
- public ModelAndView demo2(HttpServletRequest req, HttpServletResponse resp)throws Exception{
- return super.handleRequest(req, resp);
- }
这样你就要把demo2中功能复制到 handle方法中。
声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
|
返回顶楼 |
|
|
- acf
- 等级: 初级会员
- 文章: 9
- 积分: 30
|
配置中少了一部分,这里加上: <bean id="paraMethodResolver" class="org.springframework.web.servlet.mvc.multiaction.ParameterMethodNameResolver"> <property name="paramName"> <value>method</value> </property> </bean>
|
返回顶楼 |
|
|
- 稻香麦甜
- 等级: 初级会员
- 性别:
- 文章: 105
- 积分: 0
- 来自: 北京
|
干嘛不和SimpleFormController 结合?
|
返回顶楼 |
|
|