Skip to content

Commit cfab689

Browse files
committed
[add] add format exmple of spring MVC
1 parent 8540771 commit cfab689

File tree

8 files changed

+65
-5
lines changed

8 files changed

+65
-5
lines changed

pom.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,13 @@
192192
<version>1.9.13</version>
193193
</dependency>
194194

195+
<!-- data convert -->
196+
<dependency>
197+
<groupId>joda-time</groupId>
198+
<artifactId>joda-time</artifactId>
199+
<version>2.9.4</version>
200+
</dependency>
201+
195202
<!-- apache common-->
196203
<dependency>
197204
<groupId>org.apache.commons</groupId>

spring-mvc/pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,12 @@
3636
<artifactId>jackson-databind</artifactId>
3737
</dependency>
3838

39+
<!-- joda time -->
40+
<dependency>
41+
<groupId>joda-time</groupId>
42+
<artifactId>joda-time</artifactId>
43+
</dependency>
44+
3945
<!-- JSP tag -->
4046
<dependency>
4147
<groupId>javax.servlet</groupId>

spring-mvc/src/main/java/com/brianway/learning/spring/mvc/domain/User.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
package com.brianway.learning.spring.mvc.domain;
22

3+
import org.springframework.format.annotation.DateTimeFormat;
4+
import org.springframework.format.annotation.NumberFormat;
5+
6+
import java.util.Date;
7+
38
/**
49
* Created by brian on 16/8/23.
510
*/
@@ -8,6 +13,10 @@ public class User {
813
private String userName;
914
private String password;
1015
private String realName;
16+
@DateTimeFormat(pattern = "yyyy-MM-dd")
17+
private Date birthday;
18+
@NumberFormat(pattern = "#,###.###")
19+
private long salary;
1120

1221
public String getUserId() {
1322
return userId;
@@ -40,4 +49,20 @@ public String getRealName() {
4049
public void setRealName(String realName) {
4150
this.realName = realName;
4251
}
52+
53+
public Date getBirthday() {
54+
return birthday;
55+
}
56+
57+
public void setBirthday(Date birthday) {
58+
this.birthday = birthday;
59+
}
60+
61+
public long getSalary() {
62+
return salary;
63+
}
64+
65+
public void setSalary(long salary) {
66+
this.salary = salary;
67+
}
4368
}

spring-mvc/src/main/java/com/brianway/learning/spring/mvc/web/UserController.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,4 +147,8 @@ public void initBinder(WebDataBinder binder) {
147147
binder.registerCustomEditor(User.class, new UserEditor());
148148
}
149149

150+
@RequestMapping(value = "/format")
151+
public String format(User user) {
152+
return "/user/showUser";
153+
}
150154
}

spring-mvc/src/main/webapp/WEB-INF/mvc-servlet.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@
4444
</util:list>
4545

4646
<!-- 数据转换 -->
47-
<bean id="conversionService" class="org.springframework.context.support.ConversionServiceFactoryBean">
47+
<bean id="conversionService"
48+
class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
4849
<property name="converters">
4950
<set>
5051
<bean class="com.brianway.learning.spring.mvc.domain.StringToUserConverter"/>

spring-mvc/src/main/webapp/WEB-INF/views/user/register.jsp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
2-
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
2+
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
33
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
44
<html>
55
<head>
Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
2+
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
23
<html>
34
<head>
45
<title>用户信息</title>
56
</head>
67
<body>
7-
当前用户是${user.userName},userId为${user.userId}
8-
<c:if test="userSession != null">
9-
<br> session用户是${userSession.userName},userId为${userSession.userId}
8+
当前用户是 ${user.userName},userId 为 ${user.userId}
9+
<c:if test="${userSession != null}">
10+
<br> session 用户是 ${userSession.userName},userId 为 ${userSession.userId}
1011
</c:if>
1112
</body>
1213
</html>

spring-mvc/src/test/java/com/brianway/learning/spring/mvc/web/UserControllerTest.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.brianway.learning.spring.mvc.web;
22

3+
import org.junit.Assert;
34
import org.junit.Test;
45
import org.springframework.core.io.FileSystemResource;
56
import org.springframework.core.io.Resource;
@@ -33,4 +34,19 @@ public void testShowImage() throws IOException {
3334
Resource outFile = new FileSystemResource("/Users/brian/todo/tmp/test.jpg");
3435
FileCopyUtils.copy(reponse, outFile.getFile());
3536
}
37+
38+
@Test
39+
public void testFormat() {
40+
RestTemplate restTemplate = new RestTemplate();
41+
MultiValueMap<String, String> form = new LinkedMultiValueMap<String, String>();
42+
form.add("userId","0001");
43+
form.add("userName", "Tom");
44+
form.add("password", "123");
45+
form.add("birthday", "1992-11-25");
46+
form.add("salary", "4,500.00");
47+
String html = restTemplate.postForObject(URL_PREFIX + "format", form, String.class);
48+
Assert.assertNotNull(html);
49+
Assert.assertTrue(html.contains("Tom"));
50+
System.out.println(html);
51+
}
3652
}

0 commit comments

Comments
 (0)