`

SpringMVC学习笔记(二) --- 后端控制器之CommandController、 FormController(三)

 
阅读更多

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

 

先看下springmvc-servlet.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
     <!-- 定义Bean -->
     <bean id="registrationCommandController" class="com.wy.controller.RegistrationCommandController">
        <property name="commandClass" value="com.wy.pojo.User" />
     </bean>
	 <bean name="/simpleImplementsController.do" class="com.wy.controller.SimpleImplementsController"/>
	 	
     <!-- 映射处理器 -->
     <bean id="simpleUrlHandlerMapping"
		class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
		<property name="mappings">
			<props>
				<prop key="/register.do">registrationCommandController</prop>
			</props>
		</property>
		<property name="order" value="1"></property>
	</bean>
	<bean id="beanNameUrlHandlerMapping"
		class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping">
		<property name="order" value="0"></property>
	</bean>	
	
	<!-- 视图解析器策略和视图解析器 -->
	<bean id="internalResourceViewResolver"
		class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="viewClass"
			value="org.springframework.web.servlet.view.InternalResourceView" />
		<property name="prefix" value="/WEB-INF/page/" />
		<property name="suffix" value=".jsp" />
	</bean>
</beans>

 

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

 

SimpleImplementsController.java

package com.wy.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

/**
 * 实现Controller和继承AbstractContoller功能是一样的
 *
 * @author Administrator
 * @version Sep 17, 2011
 */
public class SimpleImplementsController implements Controller {
	
	@Override
	public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {

		ModelAndView mav = new ModelAndView("login");//逻辑视图名称
		mav.addObject("msg", "Hello World! Implements Controller");
		return mav;
	}

}

 

RegistrationCommandController.java

 

package com.wy.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractCommandController;

import com.wy.pojo.User;

/**
 * 
 *
 * @author Administrator
 * @version 2011-10-7
 */
@SuppressWarnings("deprecation")
public class RegistrationCommandController extends AbstractCommandController{
    
	/**
     * 构造器
     */
    public RegistrationCommandController(){
    	//也可以在spmvc-servlet.xml中定义
    	//<property name="commandClass" value="com.wy.pojo.User" />
	    setCommandClass(User.class);//为控制器类指定命名类对象
    }

	@Override
	protected ModelAndView handle(HttpServletRequest request, HttpServletResponse response, Object command,
	        BindException exception) throws Exception
	{
		ModelAndView mav = new ModelAndView("edit");
		User user = (User)command;
		System.out.println(user);
		return mav;
	}

}

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics