文章目錄
很多時候我們在view層接收到的數據類型和pojo中的類型並不匹配,此時就需要自定義一個轉換器來輔助我們完成數據類型轉換的工作。例如一個人的生日屬性,前端接收到的是String類型,但是pojo中的是Data類型,下面以此爲例,寫出一個自定義的轉換器。
(1)user_pojo
package com.gql.pojo;
import java.util.Date;
/**
* 類說明:
* User_pojo
* @guoqianliang1998.
*/
public class User {
private Integer id;
private String name;
private Integer age;
private String sex;
private Date birthday;
//省略對應的set和get方法
}
(2)handler處理器
處理器中跳轉頁面的字符串通過SpringMVC配置中的視圖解析器進行了簡化。
package com.gql.springmvc03;
import java.io.UnsupportedEncodingException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import com.gql.pojo.User;
/**
* 類說明:
* 處理器
* @guoqianliang1998.
*/
@Controller
public class UserController {
public UserController() {
super();
System.out.println("構造函數...");
}
//轉發到前端addUI.jsp
@RequestMapping("/test")
public String addUI(User user){
return "addUI";
}
@RequestMapping("/add")
public String add(User user){
return "index";
}
}
(3)前端添加頁面
一個簡單的前端添加頁面,當點擊添加用戶按鈕時跳轉到add.do頁面,由於在add.do頁面又跳轉到了index.jsp頁面,所以當點擊添加用戶按鈕最終會跳轉到index.jsp。
addUI.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<html>
<head>
<meta http-equive="content-type" content="text/html,charset=utf-8"/>
<title>用戶添加頁面</title>
</head>
<body>
<form action='<c:url value="/add.do"/>' method="post">
<table border="1">
<tr>
<td align="center">用戶名</td>
<td align="center">性別</td>
<td align="center">年齡</td>
<td align="center">生日</td>
</tr>
<tr>
<td><input type="text" name="name"/></td>
<td><input type="text" name="sex"/></td>
<td><input type="text" name="age"/></td>
<td><input type="text" name="birthday"/></td>
</tr>
<tr>
<td colspan="4" align="center">
<input type="submit" value="添加用戶" />
</td>
</tr>
</table>
</form>
</body>
</html>
(4)添加用戶後跳轉到的頁面
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<html>
<head>
<meta http-equive="content-type" content="text/html,charset=utf-8"/>
</head>
<body>
添加用戶成功
</body>
</html>
(5)StringtoDate轉換器
package com.gql.convertor;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.springframework.core.convert.converter.Converter;
/**
* 類說明:
* 轉換器
* @guoqianliang1998.
*/
public class StringtoDate implements Converter<String, Date> {
@Override
public Date convert(String source) {
try {
return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(source);
} catch (ParseException e) {
throw new RuntimeException("日期轉換異常");
}
}
}
(6)配置SpringMVC
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
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.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd ">
<!-- 處理器(手寫) -->
<context:component-scan base-package="com.gql.springmvc03"></context:component-scan>
<!-- 代替處理器映射器和處理器適配器 -->
<mvc:annotation-driven conversion-service="conversionService"/>
<!-- 自定義類型轉換器 -->
<bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
<property name="converters">
<set>
<bean class="com.gql.convertor.StringtoDate"></bean>
</set>
</property>
</bean>
<!-- 視圖解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
(7)前端控制器
*.do
,*.action
: 請求的url以.do或.action結尾,都會被SpringMVC框架所解析.
/
:所有的請求都會被pringmvc所解析,會造成靜態資源無法訪問.支持RestFul開發風格.
/*
:攔截所有請求,JSP也會被springmvc解析,造成JSP無法訪問.不要使用這種方式
<?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"
id="WebApp_ID" version="2.5">
<display-name>SpringMVC01</display-name>
<!-- 前端控制器 -->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 加載SpringMVC配置文件 -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc03.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>
(8)測試自定義轉換器
在Debug模式下運行該程序,可以看到成功將信息加入user對象中。
自定義轉換器測試成功。