spring mvc 数据格式化
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 数据格式化的更多相关文章
- Spring mvc数据转换 格式化 校验(转载)
原文地址:http://www.cnblogs.com/linyueshan/p/5908490.html 数据绑定流程 1. Spring MVC 主框架将 ServletRequest 对象及目标 ...
- Hibernate Validation,Spring mvc 数据验证框架注解
1.@NotNull:不能为 Null,但是可以为Empty:用在基本数据类型上. @NotNull(message="{state.notnull.valid}", groups ...
- spring mvc 数据校验(bean实体注解实现)
spring mvc 数据校验 1.添加个jar (jar与一版本会冲突) <dependency> <groupId>com.fasterxml</groupId> ...
- Spring MVC 数据校验@Valid
先看看几个关键词 @Valid @Pattern @NotNull @NotBlank @Size BindingResult 这些就是Spring MVC的数据校验的几个注解. 那怎么用呢?往下看 ...
- JSR教程2——Spring MVC数据校验与国际化
SpringMVC数据校验采用JSR-303校验. • Spring4.0拥有自己独立的数据校验框架,同时支持JSR303标准的校验框架. • Spring在进行数据绑定时,可同时调用校验框架完成数据 ...
- spring mvc 数据回显
1.spring mvc自动将传入的pojo数据存入request域 request中的key是该pojo类名,首字母小写. JSP controller 第一次访问user.jsp 填写表单 点击提 ...
- Spring MVC 数据验证——validate编码方式
1.导入jar包 validation-api-1.0.0.GA.jar这是比較关键的一个jar包,主要用于解析注解@Valid. hibernate-validator-4.3.2.Final.ja ...
- 【代码总结】Spring MVC数据校验
1.实验介绍 --------------------------------------------------------------------------------------------- ...
- Spring MVC 数据验证——validate注解方式
1.说明 学习注解方式之前,应该先学习一下编码方式的spring注入.这样便于理解验证框架的工作原理.在出错的时候,也能更好的解决这个问题.所以本次博客教程也是基于编码方式.仅仅是在原来的基础加上注解 ...
随机推荐
- .net Json JavaScriptSerializer JsonHelper类
结合.net 的JavaScriptSerializer 类实现Json数据处理 调用1: Model.Users m = BLL.UsersBLL.GetUserById(Convert.ToInt ...
- wget批量下载http文件
eg:http://hgdownload.soe.ucsc.edu/goldenPath/hg19/encodeDCC/wgEncodeAwgDnaseUniform/ 下载该路径下的所有文件 wge ...
- 1.8.3suspend与resume方法的缺点--不同步
package com.cky.bean; /** * Created by edison on 2017/12/3. */ public class MyObject { private Strin ...
- 刚部署的程序加载不出来css,js以及图片
刚部署的程序加载不出来css,js以及图片,解决方式 需要在配置中加入静态资源 方法一: controller.xml中加入 <mvc:annotation-driven/> <mv ...
- Installing Apache Hadoop Single Node
转载请注明出处:http://www.cnblogs.com/wubdut/p/4681286.html platform: Ubuntu 14.04 LTS hadoop 1.2.1 1. inst ...
- Java学习介绍
Java版本介绍 JavaME:微型版,用于开发小型设备.智能卡.移动终端应用(使用率较低) JavaSE:标准版,用于创建桌面应用(企业用JavaSE创建桌面应用较少) JavaEE:企业版,用于创 ...
- 17、文件IO详解及实例
上篇文章已经讲过了文件系统的一些基本的概念,这里首先对文件IO进行详细的学习,文件IO也称为系统调用IO,是操作系统为"用户态"运行的进程和硬件交互提供的一组接口,即操作系统内核留 ...
- bootstrap4.2 导航搜索框
<!DOCTYPE html><html> <head> <meta charset="UTF-8"> <title>& ...
- Http TCP/IP协议和socket之间的区别和联系
总结,TCP/IP是传输层协议,主要解决数据如何在网路中传输,socket是TCP/IP协议的具体实现,是对TCP/IP协议的封装和应用,属于程序员层面,HTTP是应用层协议,应用层协议很多,类似的像 ...
- AFNetworking 3.0 AFHTTPSessionManager文件下载
#import "ViewController.h" #import <AFNetworking.h> @interface ViewController () - ( ...