SpringMVC注解驱动的属性对象格式化功能

说明:    Spring为开发者提供了注解驱动的属性对象格式化功能:在bean属性中设置、SpringMVC处理方法参数绑定数据、模型数据输出时自动通过注解应用格式化的功能。。

在org.springframework.format.annotation包下定义了两个格式化的注解类型:

1. DateTimeFormat


2.NumberFormat


=======================================================

编码实现部分:

1.项目结构


2.环境搭建:

首先是pom.xml依赖配置

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.lin</groupId>
  <artifactId>AnnotationFormatterFactory</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>war</packaging>
  
  <dependencies>
  	<!-- spring begin -->
	<!--spring-core-->  
	<dependency>
	    <groupId>org.springframework</groupId>
	    <artifactId>spring-core</artifactId>
	    <version>4.2.0.RELEASE</version>
	</dependency>
	<!-- spring-web-mvc -->
	<dependency>
		<groupId>org.springframework</groupId>
		<artifactId>spring-webmvc</artifactId>
		<version>4.2.0.RELEASE</version>
	</dependency>
	<!-- spring end -->
	
  </dependencies>
  
  
  <!-- 添加项目jdk编译插件 -->
  <build>
  	<!-- 设置编译版本为1.8 -->
  	<plugins>
  		<plugin>
  			<groupId>org.apache.maven.plugins</groupId>
  			<artifactId>maven-compiler-plugin</artifactId>
  			<configuration>
  				<source>1.8</source>
  				<target>1.8</target>
  				<encoding>UTF-8</encoding>
  			</configuration>
  		</plugin>
  		<!-- 配置tomcat7的插件,如果使用maven的命令方式运行,则命令为:tomcat7:run ,
  			 而不能是tomcat:run(如果使用该命令,还是会使用maven默认的tomcat来编译运行项目)。
  			
  			可以直接使用eclipse中的tomcat来运行项目(就是原先没有使用maven时的那样运行项目就可以了,那样的话也不需要配这个插件了)。	 
  		 -->
  		<plugin>  
            <groupId>org.apache.tomcat.maven</groupId>  
            <artifactId>tomcat7-maven-plugin</artifactId>  
            <version>2.1</version>  
        </plugin>
  	</plugins>
  </build>
</project>

然后是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>AnnotationFormatterFactory</display-name>
  
   <!-- 定义Spring MVC的前端控制器 -->
  <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/springmvc-config.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  
  <!-- 让Spring MVC的前端控制器拦截所有请求 -->
  <servlet-mapping>
    <servlet-name>springmvc</servlet-name>
    <url-pattern>/</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>

接着是springmvc-config.mxl

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:mybatis="http://mybatis.org/schema/mybatis-spring"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
                              http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
                              http://www.springframework.org/schema/mvc
                              http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
                              http://www.springframework.org/schema/context
                              http://www.springframework.org/schema/context/spring-context-4.2.xsd
                              http://www.springframework.org/schema/tx
                              http://www.springframework.org/schema/tx/spring-tx-4.2.xsd
                              http://mybatis.org/schema/mybatis-spring
                              http://mybatis.org/schema/mybatis-spring.xsd">

    <!-- 自动扫描该包,SpringMVC会将包下用@Controller注解的类注册为Spring的controller -->
    <context:component-scan base-package="org.fkit"/>
    
    <!-- 设置默认配置方案 -->
    <mvc:annotation-driven/>
    
	<!-- 使用默认的Servlet来响应静态文件 -->
	<mvc:default-servlet-handler/>
	
    <!-- 视图解析器 -->
    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <!-- 前缀 -->
        <property name="prefix">
            <value>/WEB-INF/content/</value>
        </property>
        <!-- 后缀 -->
        <property name="suffix">
            <value>.jsp</value>
        </property>
    </bean>

</beans>

-----------------------------------------------------------------------

2.提供用于接收参数的实体类

public class User implements Serializable{
	private static final long serialVersionUID = 1L;
	
	//日期类型
	@DateTimeFormat(pattern="yyyy-MM-dd")
	private Date birthday;
	
	//正常数字类型
	@NumberFormat(style=Style.NUMBER,pattern="#,###")
	private int total;
	
	//百分数类型
	@NumberFormat(style=Style.PERCENT)
	private double discount;
	
	//货币类型
	@NumberFormat(style=Style.CURRENCY)
	private double money;
	
	//省略set和get方法
}

-----------------------------------------------

3.controller类

@Controller
public class FormatterController {

	//动态跳转页面
	@RequestMapping(value="/{formName}")
	public String loginForm(@PathVariable String formName){
		return formName;
	}
	
	@RequestMapping(value="/test",method=RequestMethod.POST)
	public String test(@ModelAttribute User user, Model model){
		model.addAttribute("user", user);
		return "success";
	}
	
}
---------------------------------------------------------

4.页面代码

testForm.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>测试表单数据格式化</title>
</head>
<body>
	<h3>测试表单数据格式化</h3>
	<form action="test" method="post">
		<table>
			<tr>
				<td><label>日期类型:</label></td>
				<td><input type="text" id="birthday" name="birthday"/></td>
			</tr>
			<tr>
				<td><label>整数类型:</label></td>
				<td><input type="text" id="total" name="total"/></td>
			</tr>
			<tr>
				<td><label>百分数类型:</label></td>
				<td><input type="text" id="discount" name="discount"/></td>
			</tr>
			<tr>
				<td><label>货币类型:</label></td>
				<td><input type="text" id="money" name="money"/></td>
			</tr>
			<tr>
				<td><input type="submit" id="submit" value="提交"/></td>
			</tr>
		</table>
	</form>
</body>
</html>

success.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>测试表单数据格式化-成功页</title>
</head>
<body>
	<h3>测试表单数据格式化</h3>
	<form:form modelAttribute="user">
		<table>
			<tr>
				<td>日期类型:</td>
				<td><form:input path="birthday"/></td>
			</tr>
			<tr>
				<td>整数类型:</td>
				<td><form:input path="total"/></td>
			</tr>
			<tr>
				<td>百分数类型:</td>
				<td><form:input path="discount"/></td>
			</tr><tr>
				<td>货币类型:</td>
				<td><form:input path="money"/></td>
			</tr>
		</table>
	</form:form>
</body>
</html>

小结:

可以看到的是,在springmvc中没有做什么特别的配置,那是因为<mvc:annotation-driven/>这个标签帮我们加载了很多东西,到了后面学习有必要好好研究下<mvc:annotation-driven/>中为我们加载了哪些东东。。然后就是,注解开发相对于使用配置的方式实现的话那肯定是便利了很多。。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值