Springmvc 注解驱动格式化实例

public class User {
    private String name;
    //可将形如1980-09-01的字符串转换为Date类型的birthday属性中
    @DateTimeFormat(pattern = "yyyy-MM-dd")
    private Date birthday;
    //可将形如4,500.00的字符串转换到long类型的salary属性中
    @NumberFormat(pattern = "#,###.##")
    private long salary;

    public long getSalary() {
        return salary;
    }

    public void setSalary(long salary) {
        this.salary = salary;
    }

    public String getName() {

        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Date getBirthday() {

        return birthday;
    }

    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }
}
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
    <title>首页</title>
</head>
<body>
    <form action="<c:url value="/showuser.html" />">
        <table>
            <tr>
                    <td>
                        用户名:
                    </td>
                    <td>
                        <input type="text" name="name"/>
                    </td>
            </tr>
            <tr>
                    <td>
                        生日:
                    </td>
                    <td>
                        <input type="text" name="birthday"/>
                    </td>
            </tr>
            <tr>
                    <td>
                        工资:
                    </td>
                    <td>
                        <input type="text" name="salary">
                    </td>
            </tr>
            <tr>
                    <td colspan="2">
                        <input type="submit" value="提交">
                    </td>
            </tr>
        </table>
    </form>
</body>
</html>


@RequestMapping("showuser")
public String showuser(User user){
    System.out.println(user.getName());
    System.out.println(user.getBirthday());
    System.out.println(user.getSalary());
    return "register";
}

<context:component-scan base-package="com.smart"/>
<!--替换原来默认的,FormattingConversionServiceFactory实现了格式化功能-->
<mvc:annotation-driven conversion-service="myconversionService"/>
<bean id="myconversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean"/>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"
      p:prefix="/WEB-INF/views/"
      p:suffix=".jsp"/>


占旭鹏
Thu Sep 06 00:00:00 GMT+08:00 2012
1123

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值