后端控制器之CommandController、 FormController(三)

另一种表单封装的参数的Controller是AbstractCommandController,在SpringMVC学习笔记(二) --- 后端控制器之CommondController、 FormController(一) 也说到了使用这种方式不是太方便,但是作为学习,还是值得看看。

先看下springmvc-servlet.xml

Xml代码 复制代码 收藏代码
  1. <?xml version="1.0" encoding="UTF-8" ?> 
  2. <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"> 
  3. <beans> 
  4.      <!-- 定义Bean --> 
  5.      <bean id="registrationCommandController" class="com.wy.controller.RegistrationCommandController"> 
  6.         <property name="commandClass" value="com.wy.pojo.User" /> 
  7.      </bean> 
  8.      <bean name="/simpleImplementsController.do" class="com.wy.controller.SimpleImplementsController"/> 
  9.          
  10.      <!-- 映射处理器 --> 
  11.      <bean id="simpleUrlHandlerMapping" 
  12.         class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"> 
  13.         <property name="mappings"> 
  14.             <props> 
  15.                 <prop key="/register.do">registrationCommandController</prop> 
  16.             </props> 
  17.         </property> 
  18.         <property name="order" value="1"></property> 
  19.     </bean> 
  20.     <bean id="beanNameUrlHandlerMapping" 
  21.         class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"> 
  22.         <property name="order" value="0"></property> 
  23.     </bean>    
  24.      
  25.     <!-- 视图解析器策略和视图解析器 --> 
  26.     <bean id="internalResourceViewResolver" 
  27.         class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
  28.         <property name="viewClass" 
  29.             value="org.springframework.web.servlet.view.InternalResourceView" /> 
  30.         <property name="prefix" value="/WEB-INF/page/" /> 
  31.         <property name="suffix" value=".jsp" /> 
  32.     </bean> 
  33. </beans> 

前面的文章也提到了使用AbstractCommandController来封装表单参数是需要维护两个Controller。

SimpleImplementsController.java

Java代码 复制代码 收藏代码
  1. package com.wy.controller; 
  2.  
  3. import javax.servlet.http.HttpServletRequest; 
  4. import javax.servlet.http.HttpServletResponse; 
  5.  
  6. import org.springframework.web.servlet.ModelAndView; 
  7. import org.springframework.web.servlet.mvc.Controller; 
  8.  
  9. /**
  10. * 实现Controller和继承AbstractContoller功能是一样的
  11. *
  12. * @author Administrator
  13. * @version Sep 17, 2011
  14. */ 
  15. public class SimpleImplementsController implements Controller { 
  16.      
  17.     @Override 
  18.     public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception { 
  19.  
  20.         ModelAndView mav = new ModelAndView("login");//逻辑视图名称 
  21.         mav.addObject("msg", "Hello World! Implements Controller"); 
  22.         return mav; 
  23.     } 
  24.  

RegistrationCommandController.java

Java代码 复制代码 收藏代码
  1. package com.wy.controller; 
  2.  
  3. import javax.servlet.http.HttpServletRequest; 
  4. import javax.servlet.http.HttpServletResponse; 
  5.  
  6. import org.springframework.validation.BindException; 
  7. import org.springframework.web.servlet.ModelAndView; 
  8. import org.springframework.web.servlet.mvc.AbstractCommandController; 
  9.  
  10. import com.wy.pojo.User; 
  11.  
  12. /**
  13. *
  14. *
  15. * @author Administrator
  16. * @version 2011-10-7
  17. */ 
  18. @SuppressWarnings("deprecation"
  19. public class RegistrationCommandController extends AbstractCommandController{ 
  20.      
  21.     /**
  22.      * 构造器
  23.      */ 
  24.     public RegistrationCommandController(){ 
  25.         //也可以在spmvc-servlet.xml中定义 
  26.         //<property name="commandClass" value="com.wy.pojo.User" /> 
  27.         setCommandClass(User.class);//为控制器类指定命名类对象 
  28.     } 
  29.  
  30.     @Override 
  31.     protected ModelAndView handle(HttpServletRequest request, HttpServletResponse response, Object command, 
  32.             BindException exception) throws Exception 
  33.     { 
  34.         ModelAndView mav = new ModelAndView("edit"); 
  35.         User user = (User)command; 
  36.         System.out.println(user); 
  37.         return mav; 
  38.     } 
  39.  

你可能感兴趣的:(后端控制器之CommandController、 FormController(三))