zoukankan      html  css  js  c++  java
  • 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>
    View Code

    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>
    View Code

    这里一定要注意被注释的部分,否则运行后会报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;
        }
    }
    View Code

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

    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";
        }
    }
    View Code

    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>
    View Code

    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>
    View Code

    运行结果

     实体类转换Json

    @JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm")
        private Date updateTime;
  • 相关阅读:
    Persister使用说明
    获取一个目录下的所有文件名称
    bootstrap学习
    bootstrap.文章列表带头像及操作
    初识Lucene.net
    Lucene.net 高亮显示搜索词
    WP7.OnNavigatedTo和OnNavigatedFrom
    SL4.图片下载进度条
    SL4.基本数据验证
    SL4.数据绑定OneWay、OneTime、TwoWay
  • 原文地址:https://www.cnblogs.com/uptothesky/p/7882992.html
Copyright © 2011-2022 走看看