反射应用三步曲之二----使用mvc模式获取表单数据--为避免项目太复杂,所以未牵涉数据库
反射工具: apache的BeanUtils 和 Spring 的beanUtils
传统servlet获取表单数据可参考:传统Servlet获取表单数据
反射应用三步曲之一:使用反射封装自己的工具类MyBeanUtils
反射应用三步曲之三: 链接地址:
使用Maven构建一 war工程
pom文件中导入依赖:
导依赖时注意:
1> 若未使用Apache的beanUtils,则不必导 commons-beanutils:1.9.3
2>jsp页面顶部顶部报异常时:
导 jsp-api 和 javax.servlet-api ---->原因:点击此处
Employee:
注意我在 private Date hredate; 上面加了一个注解@DateTimeFormat(pattern="yyyy-MM-dd") 以指定格式化的日期格式
package com.qx.pojo;
import java.util.Arrays;
import java.util.Date;
import org.springframework.format.annotation.DateTimeFormat;
public class Employee {
private String name; //姓名
private String password; //密码
private int age; //年龄
private String[] hobby; //爱好
@DateTimeFormat(pattern="yyyy-MM-dd")
private Date hiredate; //入职日期
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String[] getHobby() {
return hobby;
}
public void setHobby(String[] hobby) {
this.hobby = hobby;
}
public Date getHiredate() {
return hiredate;
}
public void setHiredate(Date hiredate) {
this.hiredate = hiredate;
}
@Override
public String toString() {
return "Employee [name=" + name + ", password=" + password + ", age=" + age + ", hobby="
+ Arrays.toString(hobby) + ", hiredate=" + hiredate + "]";
}
}
EmployeeMulateMulate: --- 变种
package com.qx.common;
import java.util.Arrays;
public class EmployeeMutate {
private String name; //姓名
private String password; //密码
/*由int类型变异为String类型*/
private String age; //年龄
private String[] hobby; //爱好
/*由Date类型变异为String类型*/
private String hiredate; //入职日期
//getter and setter
@Override
public String toString() {
return "EmployeeMutate [name=" + name + ", password=" + password + ", age=" + age + ", hobby="
+ Arrays.toString(hobby) + ", hiredate=" + hiredate + "]";
}
}
applicationContext.xml
<?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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">
<context:component-scan base-package="com.qx"></context:component-scan>
<bean id="employeeEx" class="com.qx.common.EmployeeMutate"></bean>
</beans>
spring-MVC.xml
<?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:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd">
<!-- 虽在applicationContext.xml中配置过了,最好还是在此处再配一次,防止扫不到 -->
<context:component-scan base-package="com.qx"></context:component-scan>
<!-- 不需要配置id,因为用不到 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- <property name="prefix" value="/"></property> -->
<property name="suffix" value=".jsp"></property>
</bean>
<mvc:annotation-driven></mvc:annotation-driven>
</beans>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
<display-name>MyWebPro</display-name>
<!-- needed for ContextLoaderListener -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/applicationContext.xml</param-value>
</context-param>
<!-- Bootstraps the root web application context before servlet initialization -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- The front controller of this Spring Web application, responsible for handling all application requests -->
<servlet>
<servlet-name>SpringMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/spring-MVC.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- Map all requests to the DispatcherServlet for handling -->
<servlet-mapping>
<servlet-name>SpringMVC</servlet-name>
<url-pattern>*.action</url-pattern>
</servlet-mapping>
<!-- 解决乱码 -->
<filter>
<filter-name>CharacterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>utf-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
index.jsp
<body>
<form action="employee/getEmployeeInfo.action" method="post">
姓 名:<input type="text" name="name"/><br/>
密 码:<input type="password" name="password"><br/>
年 龄:<input type="text" name="age"><br/>
爱好:<input type="checkbox" name="hobby" value="smoke">抽烟
<input type="checkbox" name="hobby" value="drink">喝酒
<input type="checkbox" name="hobby" value="head">烫头<br/>
入职日期:<input type="text" name="hiredate"><br/>
<input type="submit" value="提交"/>
</form>
</body>
success.jsp:
<body>
Success!
</body>
重点: EmployeeController
package com.qx.controller;
import java.util.Arrays;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import com.qx.common.EmployeeMutate;
import com.qx.pojo.Employee;
@Controller
@RequestMapping("/employee")
public class EmployeeController {
@Autowired
private EmployeeMutate employeeMutate;
@RequestMapping("/getEmployeeInfo")
public String getEmployeeInfo(Employee employee) throws Exception{
System.out.println(employee);
System.out.println(Arrays.toString(employee.getHobby()));
/*使用日期转换器工具类 spring的 BeanUtils*/
/*忽略掉password字段*/
/**
* 结果为:
* EmployeeMutate [name=网易按, password=null, age=null, hobby=[drink, head], hiredate=null]
* 说明被忽略掉的字段和属性名相同但类型不匹配的字段都不会复制到目标类对象中.---不会报异常
*
* 若使用Apache的BeanUtils去复制属性时, 无法忽略部分字段,若目标类对象和源对象属性名相同类型不同时复制属性会报异常
*/
BeanUtils.copyProperties(employee, employeeMutate,new String[] {"password"});
System.out.println(employeeMutate);
System.out.println("---------------------------");
/*Apache的BeanUtils.copyProperties(employee, employeeMutate2)会出异常*/
// EmployeeMutate employeeMutate2=new EmployeeMutate();
// // 复制属性
// org.apache.commons.beanutils.BeanUtils.copyProperties(employee, employeeMutate2);
//
// System.out.println(employeeMutate2);
return "/success";
}
}
进入index.jsp页面
点击提交后,浏览器端和控制台结果为: