欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Spring MVC 使用AnnotationFormatterFactory格式化数据

程序员文章站 2022-06-11 22:41:41
...

示例【Spring MVC 使用AnnotationFormatterFactory格式化数据】

创建index.jsp

<body>
	<h4>测试表单数据格式化</h4>
	<form action="register" method="post">
		<table>
			<tr>
				<td>日期类型:</td>
				<td><input type="text" name="birthday"></td>
			</tr>
			<tr>
				<td>整数类型:</td>
				<td><input type="text" name="total"></td>
			</tr>
			<tr>
				<td>百分数类型:</td>
				<td><input type="text" name="discount"></td>
			</tr>
			<tr>
				<td>货币类型:</td>
				<td><input type="text" name="money"></td>
			</tr>
			<tr>
				<td></td>
				<td><input type="submit" value="提交"></td>
			</tr>
		</table>
	</form>
</body>

创建User

package com.po;
import java.util.Date;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.format.annotation.NumberFormat;
import org.springframework.format.annotation.NumberFormat.Style;
public class User {
	//日期类型
	@DateTimeFormat(pattern="yyyy-MM-dd")
	private Date birthday;
	//正常数字类型
	@NumberFormat(style=Style.NUMBER,pattern="#,###")
	private int total;
	//百分数类型
	@NumberFormat(style=Style.PERCENT)
	private double discount;
	//货币类型
	@NumberFormat(style=Style.CURRENCY)
	private double money;
	public Date getBirthday() {
		return birthday;
	}
	public void setBirthday(Date birthday) {
		this.birthday = birthday;
	}
	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;
	}
	@Override
	public String toString() {
		return "User [birthday=" + birthday + ", total=" + total + ", discount=" + discount + ", money=" + money + "]";
	}
}

创建UserController

package com.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import com.po.User;
@Controller
public class UserController {
	@RequestMapping("/register")
	public String register(@ModelAttribute User user,Model model) {
		System.out.println(user);
		model.addAttribute("user", user);
		return "success";
	}
}

配置springmvc-config.xml

<!-- 装配自定义格式化转换器-->
<mvc:annotation-driven/>
<!-- 定义扫描的包 -->
<context:component-scan base-package="com.*" />
<!-- 视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
	<property name="prefix" value=""></property>
	<property name="suffix" value=".jsp" />
</bean>

创建success.jsp

<body>
	<form:form modelAttribute="user" action="">
	<table>
		<tr>
			<td>日期类型:</td>
			<td><form:input path="birthday"/></td>
		</tr>
		<tr>
			<td>整数类型:</td>
			<td><form:input path="total"/></td>
		</tr>
		<tr>
			<td>百分数类型:</td>
			<td><form:input path="discount"/></td>
		</tr>
		<tr>
			<td>货币类型:</td>
			<td><form:input path="money"/></td>
		</tr>
	</table>
	</form:form>
</body>

启动Tomcat并访问index.jsp

Spring MVC 使用AnnotationFormatterFactory格式化数据Spring MVC 使用AnnotationFormatterFactory格式化数据

Spring MVC 使用AnnotationFormatterFactory格式化数据