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. weat!!团队

    摘要: 团队名称:weat!! 团队成员:刘波 崔和杰 简介: 刘波:性别男,爱好:动漫,徒步旅行.在组内负责程序编写这一部分. 优点:认真负责,不懂就会去问. 崔和杰:性别男,爱好:篮球.在组内负责 ...

  2. 2018-01-06自定义view时遇到的问题

    1.设置蒙版的简单写法: 2.

  3. Java理论学时第二节。课后作业。

    枚举不属于原始数据类型,它的每个具体值都引用一个特定的对象,相同的值则引用同一个对象. 可以使用“==”和equals()方法直接比对枚举变量的值,换句话说,对于枚举类型的变量,“==”和equals ...

  4. noip第6课作业

    1.    数据统计 [问题描述] 输入N个整数,求出它们的最小值.最大值和平均值(保留3位小数).输入保证这些数都是不超过1000的整数.(1<=N<=1000) [样例输入] 8 2 ...

  5. OC语言-runtime

    参考博客 IOS高级开发-Runtime(一) http://blog.csdn.net/lizhongfu2013/article/details/9496705 apple官方参考 Object- ...

  6. 杭电2133What day is it

    给你个日期 问是星期几        知道1 1 1是周1就行了 #include <iostream>#include <cstdio>using namespace std ...

  7. lca tarjin

    这个算法  我个人认为是  遍历每一个点把它当成一些询问的最近祖先 1 2 3 4 5 6 low是并差集,vis是是否访问过,访问过为true,没有为false: 假设询问是(4,4),(4,5), ...

  8. php apache

    php与Apache配置基础知识 1.在 AllowOverride 设置为 None 时,.htaccess 文件将被完全忽略.当此指令设置为All时,所有具有“.htaccess” 作用域的指令都 ...

  9. UniGUI的 TUniPageControl控件动态拖动tabsheet的实现方法

    https://blog.csdn.net/shuiying/article/details/54932518 实现可以用鼠标动态拖动tabsheet,共三个步骤: 1.在ServerModule中, ...

  10. Ubuntu下mount命令的好用处

    mount,也就是挂载.如果是让电脑自己挂载Windows的分区,也就是你直接在文件管理器里点击那些Windows的盘符,系统就会帮助你自动挂载,不过其挂载后的名称太长太复杂,不方便终端操作.所以还是 ...