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. AngularJS实战之路由ui-view传参

    angular路由传参 首页 <!DOCTYPE html> <html ng-app="app"> <head> <title>路 ...

  2. leetcode - [6]Binary Tree Postorder Traversal

    Given a binary tree, return the postorder traversal of its nodes' values. For example:Given binary t ...

  3. day18(javaEE三大组件之一servlet(简介(一)))

    Servlet servlet是小型服务器语言,使用它可以处理前台传递来的信息,servlet进行处理后在响应给前台,其中servlet起到了关键性的作用.前端输入的信息可以持久化的存储在数据库中,并 ...

  4. USTC《现代软件工程》春季学期——第一次个人作业:词频统计

    截止日期 2018年3月29日23:59 要求 1. 对源文件(*.txt,*.cpp,*.h,*.cs,*.html,*.js,*.java,*.py,*.php等,文件夹内的所有文件)统计字符数. ...

  5. Codeforces Round #264 (Div. 2) D. Gargari and Permutations 多序列LIS+dp好题

    http://codeforces.com/contest/463/problem/D 求k个序列的最长公共子序列. k<=5 肯定 不能直接LCS 网上题解全是图论解法...我就来个dp的解法 ...

  6. [javascript-snippet]使用javascript+html5实现图片的灰度处理

    // 源码出自:潇湘夜雨<!DOCTYPE> <html> <head> <meta charset="utf-8"/> </ ...

  7. Android-Java-死锁

    死锁:程序不往下执行了,程序又没有结束,就一直卡在哪里: 在使用synchronized的时候要避免死锁,synchronized嵌套就可能会引发死锁,需要严格的检查代码,排除死锁发生的可能: 特意演 ...

  8. PostgresSQL使用Copy命令能大大提高数据导入速度

    最近在做会员系统,其中会员系统有一份企业信息初始化的数据,需要从SQL Server数据库导入到PostgreSQL,单表的数据近30万.最开始的方案是在SQL Server上生成insert int ...

  9. 【C#进阶】拥抱Lambda(二)

    语言的设计,真的是挺有意思的.第一次看这个代码[1]时,旁人随口了一句"哇,好多实心句号". 当时马上一个想法是--怎么实现的?返回了对象,然后再调用方法?然后就放下了,后来发现, ...

  10. dot net core 使用 IPC 进程通信

    本文告诉大家如何使用dot net core 和其他进程进行通信 一般都是使用 WCF 或 remoting 做远程通信,但是 dot net core 不支持 WCF 所以暂时我就只能使用 管道通信 ...