web.xml

<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/services.xml</param-value>
</init-param>
<load-on-startup></load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>

services.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" xmlns:p="http://www.springframework.org/schema/p"
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/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <context:component-scan base-package="org.mythsky.springmvcdemo"></context:component-scan>
<!--<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"></bean>-->
<!--<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"></bean>-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
<mvc:annotation-driven></mvc:annotation-driven>
</beans>

这里一定要注意被注释的部分,否则运行后会报400错误

Product.java

package org.mythsky.springmvcdemo.model;

import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.format.annotation.NumberFormat; import java.io.Serializable;
import java.util.Date; public class Product implements Serializable {
@DateTimeFormat(pattern = "yyyy-MM-dd")
private Date createDate;
@NumberFormat(style = NumberFormat.Style.NUMBER,pattern = "#,###")
private int total;
@NumberFormat(style = NumberFormat.Style.PERCENT)
private double discount;
@NumberFormat(style = NumberFormat.Style.CURRENCY)
private double money; public Product() {
// super();
} public Date getCreateDate() {
return createDate;
} public void setCreateDate(Date createDate) {
this.createDate = createDate;
} 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;
}
}

大部分书里写这里必须要有无参构造函数,如果没有其他构造函数的话不写也行

ProductController.java

package org.mythsky.springmvcdemo.controller;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.mythsky.springmvcdemo.model.Product;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping; @Controller
public class ProductController {
private static final Log logger= LogFactory.getLog(ProductController.class);
@RequestMapping(value = "/product")
public String test(@ModelAttribute Product product, Model model){
logger.info(product);
model.addAttribute("product",product);
return "showproduct";
}
}

testForm.jsp

<%--
Created by IntelliJ IDEA.
User: mythsky
Date: //
Time: :
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>FormmaterTest</title>
</head>
<body>
<h3>测试表单数据格式化</h3>
<form action="product" method="post">
<table>
<tr>
<td><label>日期类型:</label></td>
<td><input type="text" name="createDate"> </td>
</tr>
<tr>
<td><label>整数类型:</label></td>
<td><input type="text" name="total"> </td>
</tr>
<tr>
<td><label>百分数类型:</label></td>
<td><input type="text" name="discount"> </td>
</tr>
<tr>
<td><label>货币类型:</label></td>
<td><input type="text" name="money"> </td>
</tr>
<tr>
<td><input type="submit" value="提交"> </td>
</tr>
</table>
</form>
</body>
</html>

showproduct.jsp

<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%--
Created by IntelliJ IDEA.
User: mythsky
Date: //
Time: :
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>测试表单数据格式化</title>
</head>
<body>
<form:form modelAttribute="product" method="post" action="">
<table>
<tr>
<td>日期类型:</td>
<td><form:input path="createDate"></form:input></td>
</tr>
<tr>
<td><label>整数类型:</label></td>
<td><form:input path="total"></form:input> </td>
</tr>
<tr>
<td><label>百分数类型:</label></td>
<td><form:input path="discount"></form:input> </td>
</tr>
<tr>
<td><label>货币类型:</label></td>
<td><form:input path="money"></form:input> </td>
</tr>
</table>
</form:form>
</body>
</html>

运行结果

实体类转换Json

@JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm")
private Date updateTime;

spring mvc 数据格式化的更多相关文章

  1. Spring mvc数据转换 格式化 校验(转载)

    原文地址:http://www.cnblogs.com/linyueshan/p/5908490.html 数据绑定流程 1. Spring MVC 主框架将 ServletRequest 对象及目标 ...

  2. Hibernate Validation,Spring mvc 数据验证框架注解

    1.@NotNull:不能为 Null,但是可以为Empty:用在基本数据类型上. @NotNull(message="{state.notnull.valid}", groups ...

  3. spring mvc 数据校验(bean实体注解实现)

    spring mvc 数据校验 1.添加个jar (jar与一版本会冲突) <dependency> <groupId>com.fasterxml</groupId> ...

  4. Spring MVC 数据校验@Valid

    先看看几个关键词 @Valid @Pattern @NotNull @NotBlank @Size BindingResult 这些就是Spring MVC的数据校验的几个注解. 那怎么用呢?往下看 ...

  5. JSR教程2——Spring MVC数据校验与国际化

    SpringMVC数据校验采用JSR-303校验. • Spring4.0拥有自己独立的数据校验框架,同时支持JSR303标准的校验框架. • Spring在进行数据绑定时,可同时调用校验框架完成数据 ...

  6. spring mvc 数据回显

    1.spring mvc自动将传入的pojo数据存入request域 request中的key是该pojo类名,首字母小写. JSP controller 第一次访问user.jsp 填写表单 点击提 ...

  7. Spring MVC 数据验证——validate编码方式

    1.导入jar包 validation-api-1.0.0.GA.jar这是比較关键的一个jar包,主要用于解析注解@Valid. hibernate-validator-4.3.2.Final.ja ...

  8. 【代码总结】Spring MVC数据校验

    1.实验介绍 --------------------------------------------------------------------------------------------- ...

  9. Spring MVC 数据验证——validate注解方式

    1.说明 学习注解方式之前,应该先学习一下编码方式的spring注入.这样便于理解验证框架的工作原理.在出错的时候,也能更好的解决这个问题.所以本次博客教程也是基于编码方式.仅仅是在原来的基础加上注解 ...

随机推荐

  1. 学以致用十六-----Centos7.2编译安装mysql5.6.22

    一.系统环境 二.卸载系统自带的mariadb rpm -qa | grep db rpm -e --nodeps mariadb-libs-5.5.60 rpm -e --nodeps mariad ...

  2. Java实现FTP批量大文件上传下载篇1

    本文介绍了在Java中,如何使用Java现有的可用的库来编写FTP客户端代码,并开发成Applet控件,做成基于Web的批量.大文件的上传下载控件.文章在比较了一系列FTP客户库的基础上,就其中一个比 ...

  3. 正则表达式Regular expressions

    根据某种匹配模式来寻找strings中的某些单词 举例:如果我们想要找到字符串The dog chased the cat中单词 the,我们可以使用下面的正则表达式: /the/gi 我们可以把这个 ...

  4. android-基础编程之开篇

    先唠叨两句,机缘巧合现在来做android开发了,之前做后台c的,对这块不是很了解,要慢慢学习,对于framework层的学习感觉需要app开发经验更好点,在完成工作之余积累一些基础知识.既来之则安之 ...

  5. codeforces820B Mister B and Angle in Polygon 2017-06-28 09:42 123人阅读 评论(0) 收藏

    B. Mister B and Angle in Polygon time limit per test 2 seconds memory limit per test 256 megabytes i ...

  6. QOpenGLFunctions的相关的使用(1)

    QOpenGLFunctions的使用 1.  QOpenGLFunctions  说明  QOpenGLFunctions 类提供了跨平台的OpenGl ES2.0 API版本. OpenGL 2. ...

  7. chrome常用小插件

    1.广告终结者                    (去广告) 2.adsafe2.0.1                  (去广告) 3.Infinity New Tab           ( ...

  8. Java框架知识点总结

    一.Struts1的运行原理 在启动时通过前端总控制器ActionServlet加载struts-config.xml并进行解析,当用户在jsp页面发送请求被struts1的核心控制器ActionSe ...

  9. 加密算法(DES,AES,RSA,MD5,SHA1,Base64)比较和项目应用(转载)

    加密技术通常分为两大类:"对称式"和"非对称式". 对称性加密算法:对称式加密就是加密和解密使用同一个密钥.信息接收双方都需事先知道密匙和加解密算法且其密匙是相 ...

  10. 安装使用Entity Framework Power Tool Bate4 (Code First)从已建好的数据自动生成项目中的对应Model(新手贴,望各位大侠给予指点)

    从开始学习使用MVC以后,同时也开始接触EF,很多原理都不是太懂,只知道安装了EF以后,点击哪里可以生成数据库对应的Model,不用再自己手写Model.这里记录的就是如何从已建立好的数据库生成项目代 ...