Spring中AbstractCommandController控制器
定义继承AbstractCommandController的控制器,实现数据的自动封装
Controller
[java]
package com.hsit.controller;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.config.CustomEditorConfigurer;
import org.springframework.beans.propertyeditors.CustomDateEditor;
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.hsit.pojo.UserInfo;
public class UserInfoController extends AbstractCommandController {
@Override
protected ModelAndView handle(HttpServletRequest request,
HttpServletResponse response, Object command, BindException errors)
throws Exception {
// TODO Auto-generated method stub
UserInfo user = (UserInfo) command;
System.out.println(user);
return null;
}
}
JavaBean
[java]
package com.hsit.pojo;
import java.util.Date;
public class UserInfo {
private String username;
private String password;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String toString() {
return "username=" + username + ",password=" + password;
}
}
jsp 视图
[plain]
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title></title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<!--
<script type="text/javascript" src="js/*.js"></script>
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<center>
<form action="Login.do" method="post">
用户名:<input type="text" name="username" size="20"/><br>
账 号:<input type="password" name="password" size="20"><br>
<!--name名字一定要和UserInfo的属性名一样,才能正确封装-->
<input type="submit" value="提交">
</form>
</center>
</body>
</html>
配置如下
[html]
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<!-- 配置处理器映射采用SimpleUrlHandlerMapping -->
<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<!-- props底下的prop标签内填写的key都是符合转发条件的url值,之后跟的是bean的ID,
如果输入的url值与key值中的url匹配,则执行对应beanID绑定的Controller -->
<props>
<prop key="go.do">go</prop>
<prop key="test.do">test</prop>
<prop key="goLogin.do">goLogin</prop>
<prop key="Login.do">Login</prop>
</props>
</property>
</bean>
<!-- 配置视图解释器 -->
<bean class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass" val
补充:软件开发 , Java ,