一、什么是注解驱动的属性格式化?

--在bean的属性中设置,SpringMVC处理 方法参数绑定数据、模型数据输出时自动通过注解应用格式化的功能。

二、注解类型

1.DateTimeFormat

@DateTimeFormat注解可以对java.util.Date、java.util.calenda等时间类型的属性进行标注。

DateTimeFormat最常用的属性就是 pattern,类型为String,使用自定义的时间格式化字符串,如yyyy-MM-dd

2.NumberFormat

@NumberFormat可以对类似数字类型的属性进行标注,它拥有两个互斥的属性。

*pattern。类型为String,使用自定义的数字格式化串,如#,###

*style。类型是NumberFormat.Style,它有几个常用的可选值:

CURRENCY:货币类型

NUMBER:正常数字类型

PERCENT:百分数类型

实例:

1.导入所需jar包

2.在web.xml配置前端控制器

<!-- 配置springmvc的前端控制器 -->
<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:springmvc-config.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>

3.编写一个实体类

package com.dj.pojo;

import java.io.Serializable;
import java.util.Date; import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.format.annotation.NumberFormat;
import org.springframework.format.annotation.NumberFormat.Style; public class User implements Serializable{ //日期类型
@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;
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public int getTotal() {
return total;
}
public void setTotal(int total) {
this.total = total;
}
public double getDiscount() {
return discount;
}
public void setDiscount(double discount) {
this.discount = discount;
}
public double getMoney() {
return money;
}
public void setMoney(double money) {
this.money = money;
}
}

4.编写一个controller

package com.dj.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod; import com.dj.pojo.User; @Controller
public class UserController { /**
* 动态跳转页面
* @param pagename
* @return
*/
@RequestMapping(value="/{pagename}")
public String index(@PathVariable String pagename){
return pagename;
}
/**
* 跳转到显示格式化后数据的页面
* @param user
* @param model
* @return
*/
@RequestMapping(value="/test",method=RequestMethod.POST)
public String test(@ModelAttribute User user,Model model){
model.addAttribute("user",user);
return "success"; }
}

5.编写一个测试的表单页面

<%@ 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>Insert title here</title>
</head>
<body>
<form action="test" method="post">
日期类型:<input type="text" id="birthday" name="birthday"/><br>
整数类型:<input type="text" id="total" name="total"/><br>
百分数类型:<input type="text" id="discount" name="discount"/><br>
货币类型:<input type="text" id="money" name="money"/><br>
<input type="submit" value="提交"/>
</form>
</body>
</html>

6.编写一个测试显示格式化后的数据的页面。如果希望在视图页面中将模型数据以格式化的方式进行渲染,则需要使用spring的页面标签。

 <%@ taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<form:form modelAttribute="user" method="post" action="">
日期类型:<form:input path="birthday"/><br>
整数类型:<form:input path="total"/><br>
百分数类型:<form:input path="discount"/><br>
货币类型:<form:input path="money"/><br>
</form:form>

7.最后编写配置文件

<?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:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd"> <!-- spring可以自动去扫描base-package下面的包或者子包下面的java类 如果扫描到有spring相关注解的类,则吧这个类注册为spring的bean -->
<context:component-scan base-package="com.dj.controller" />
<!-- 默认装配 -->
<mvc:annotation-driven></mvc:annotation-driven>
<!-- 视图解析器 -->
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 前缀 -->
<property name="prefix">
<value>/</value>
</property>
<!-- 后缀 -->
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
</beans>

8.最后进行测试

测试成功!

SpringMVC的数据格式化-注解驱动的属性格式化的更多相关文章

  1. SpringMVC 数据转换 & 数据格式化 & 数据校验

    数据绑定流程 1. Spring MVC 主框架将 ServletRequest 对象及目标方法的入参实例传递给 WebDataBinderFactory 实例,以创建 DataBinder 实例对象 ...

  2. 【Spring注解驱动开发】如何使用@Value注解为bean的属性赋值,我们一起吊打面试官!

    写在前面 在之前的文章中,我们探讨了如何向Spring的IOC容器中注册bean组件,讲解了有关bean组件的生命周期的知识.今天,我们就来一起聊聊@Value注解的用法. 项目工程源码已经提交到Gi ...

  3. springmvc注解驱动

    <?xml version="1.0" encoding="UTF-8"?> <beans xsi:schemaLocation=" ...

  4. SpringMVC入门和常用注解

    SpringMVC的基本概念 关于 三层架构和 和 MVC 三层架构 我们的开发架构一般都是基于两种形式,一种是 C/S 架构,也就是客户端/服务器,另一种是 B/S 架构,也就 是浏览器服务器.在 ...

  5. SpringMVC配置版到注解版

    什么是springmvc? 1.1.什么是MVC MVC是模型(Model).视图(View).控制器(Controller)的简写,是一种软件设计规范. 是将业务逻辑.数据.显示分离的方法来组织代码 ...

  6. springmvc入门基础之注解和参数传递

    一.SpringMVC注解入门 1. 创建web项目2. 在springmvc的配置文件中指定注解驱动,配置扫描器 <!-- mvc的注解驱动 --> <mvc:annotation ...

  7. 15.SpringMVC核心技术-数据验证

    在 Web 应用程序中,为了防止客户端传来的数据引发程序的异常,常常需要对数据进行验证. 输入验证分为客户端验证与服务器端验证.客户端验证主要通过 JavaScript 脚本进 行, 而服务器端验证则 ...

  8. 【Spring注解驱动开发】使用@Scope注解设置组件的作用域

    写在前面 Spring容器中的组件默认是单例的,在Spring启动时就会实例化并初始化这些对象,将其放到Spring容器中,之后,每次获取对象时,直接从Spring容器中获取,而不再创建对象.如果每次 ...

  9. Spring(六)SpringMVC的数据响应

    SpringMVC的请求和响应 SpringMVC的数据响应 01-SpringMVC的数据响应-数据响应方式(理解) 1)    页面跳转 直接返回字符串 通过ModelAndView对象返回 2) ...

随机推荐

  1. 一起学ASP.NET Core 2.0学习笔记(一): CentOS下 .net core2 sdk nginx、supervisor、mysql环境搭建

    作为.neter,看到.net core 2.0的正式发布,心里是有点小激动的,迫不及待的体验了一把,发现速度确实是快了很多,其中也遇到一些小问题,所以整理了一些学习笔记: 阅读目录 环境说明 安装C ...

  2. selenium+BeautifulSoup实现强大的爬虫功能

    sublime下运行 1 下载并安装必要的插件 BeautifulSoup selenium phantomjs 采用方式可以下载后安装,本文采用pip pip install BeautifulSo ...

  3. 8.23.2 IO-输入输出规范代码

    文件路径4种写法: 相对路径,相对当前而言,在当前路径下找: String filePath = "temp01";  绝对路径: String filePath = " ...

  4. jenkins 设置 gitlab web hooks

    背景 接口自动化期望代码push后触发实现持续集成,代码push后,自动化执行jenkins的job. 步骤 准备工作 工具:jenkins,gitlab jenkins需要安装插件:git plug ...

  5. jquery 的 each 方法中 return 的坑

    jquery 的 each 方法中 return 的坑 Chapter 0 在项目中使用 jquery 的 each 方法时想在 each 的循环中返回一个布尔类型的值于是掉进一个坑中... Chap ...

  6. MySQL存储引擎中的MyISAM和InnoDB区别

    MyISAM是MySQL的默认数据库引擎(5.5版之前),由早期的ISAM(Indexed Sequential Access Method:有索引的顺序访问方法)所改良.虽然性能极佳,但却有一个缺点 ...

  7. chrome开发工具指南(十一)

    检查资源 使用 Application 面板的 Frames 窗格可以按框架组织资源. 您也可以在 Sources 面板中停用 Group by folder 选项,按框架查看资源. 要按网域和文件夹 ...

  8. angularJS+Ionic移动端图片上传的解决办法

    前端开发中经常会碰到图片上传的问题,网上的解决办法很多,可是有些图片上传的插件会有一些附属的插件,因此因为一个图片上传的问题可能额需要引入其他插件到项目中,久而久之项目会不伦不类,有时候插件之间也会有 ...

  9. IOS学习【前言】

    2016-1-14 16年开始时导师安排任务,开始IOS学习之旅  经过几天的学习,感觉还是需要作比较多的学习笔记,因此开始用博客记录整个过程,方便以后查看学习与分享. 主要记录一些关键的问题处理方法 ...

  10. 全平台轻量级 Verilog 编译器 & 仿真环境

    一直苦于 modelsim 没有Mac版本,且其体量过大,在学习verilog 时不方便使用. 终于找到一组轻量级且全平台 ( Linux+Windows+macOS ) 的编译仿真工具组. Icar ...