File tree Expand file tree Collapse file tree 8 files changed +65
-5
lines changed
java/com/brianway/learning/spring/mvc
test/java/com/brianway/learning/spring/mvc/web Expand file tree Collapse file tree 8 files changed +65
-5
lines changed Original file line number Diff line number Diff line change 192
192
<version >1.9.13</version >
193
193
</dependency >
194
194
195
+ <!-- data convert -->
196
+ <dependency >
197
+ <groupId >joda-time</groupId >
198
+ <artifactId >joda-time</artifactId >
199
+ <version >2.9.4</version >
200
+ </dependency >
201
+
195
202
<!-- apache common-->
196
203
<dependency >
197
204
<groupId >org.apache.commons</groupId >
Original file line number Diff line number Diff line change 36
36
<artifactId >jackson-databind</artifactId >
37
37
</dependency >
38
38
39
+ <!-- joda time -->
40
+ <dependency >
41
+ <groupId >joda-time</groupId >
42
+ <artifactId >joda-time</artifactId >
43
+ </dependency >
44
+
39
45
<!-- JSP tag -->
40
46
<dependency >
41
47
<groupId >javax.servlet</groupId >
Original file line number Diff line number Diff line change 1
1
package com .brianway .learning .spring .mvc .domain ;
2
2
3
+ import org .springframework .format .annotation .DateTimeFormat ;
4
+ import org .springframework .format .annotation .NumberFormat ;
5
+
6
+ import java .util .Date ;
7
+
3
8
/**
4
9
* Created by brian on 16/8/23.
5
10
*/
@@ -8,6 +13,10 @@ public class User {
8
13
private String userName ;
9
14
private String password ;
10
15
private String realName ;
16
+ @ DateTimeFormat (pattern = "yyyy-MM-dd" )
17
+ private Date birthday ;
18
+ @ NumberFormat (pattern = "#,###.###" )
19
+ private long salary ;
11
20
12
21
public String getUserId () {
13
22
return userId ;
@@ -40,4 +49,20 @@ public String getRealName() {
40
49
public void setRealName (String realName ) {
41
50
this .realName = realName ;
42
51
}
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
+ }
43
68
}
Original file line number Diff line number Diff line change @@ -147,4 +147,8 @@ public void initBinder(WebDataBinder binder) {
147
147
binder .registerCustomEditor (User .class , new UserEditor ());
148
148
}
149
149
150
+ @ RequestMapping (value = "/format" )
151
+ public String format (User user ) {
152
+ return "/user/showUser" ;
153
+ }
150
154
}
Original file line number Diff line number Diff line change 44
44
</util : list >
45
45
46
46
<!-- 数据转换 -->
47
- <bean id =" conversionService" class =" org.springframework.context.support.ConversionServiceFactoryBean" >
47
+ <bean id =" conversionService"
48
+ class =" org.springframework.format.support.FormattingConversionServiceFactoryBean" >
48
49
<property name =" converters" >
49
50
<set >
50
51
<bean class =" com.brianway.learning.spring.mvc.domain.StringToUserConverter" />
Original file line number Diff line number Diff line change 1
1
<%@ 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" %>
3
3
<%@ taglib prefix =" form" uri =" http://www.springframework.org/tags/form" %>
4
4
<html >
5
5
<head >
Original file line number Diff line number Diff line change 1
1
<%@ 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
3
<html >
3
4
<head >
4
5
<title >用户信息</title >
5
6
</head >
6
7
<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 }
10
11
</c:if >
11
12
</body >
12
13
</html >
Original file line number Diff line number Diff line change 1
1
package com .brianway .learning .spring .mvc .web ;
2
2
3
+ import org .junit .Assert ;
3
4
import org .junit .Test ;
4
5
import org .springframework .core .io .FileSystemResource ;
5
6
import org .springframework .core .io .Resource ;
@@ -33,4 +34,19 @@ public void testShowImage() throws IOException {
33
34
Resource outFile = new FileSystemResource ("/Users/brian/todo/tmp/test.jpg" );
34
35
FileCopyUtils .copy (reponse , outFile .getFile ());
35
36
}
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
+ }
36
52
}
You can’t perform that action at this time.
0 commit comments