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>1</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: 2017/11/22 Time: 22:15 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: 2017/11/22 Time: 22:24 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;
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· .NET周刊【3月第1期 2025-03-02】
· [AI/GPT/综述] AI Agent的设计模式综述
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
2016-11-23 在CentOS7上安装RabbitMQ