對 於BaseCommandController及其子類別來說,它的Command物件並不一定要接受基本型態或是String型態,您可以撰寫一個實作 java.beans.PropertyEditor的類別,在當中進行轉換,例如將接收到的字串轉換為User類別的實例。
舉個實例來說,假設您的Command物件如下設計:
- SomeForm.java
package onlyfun.caterpillar;
public class SomeForm {
private User user;
public void setUser(User user) {
this.user = user;
}
public User getUser() {
return user;
}
}
SomeForm為網頁表單的物件代表,而其中出現的User類別如下設計:
- User.java
package onlyfun.caterpillar;
public class User {
private String firstName;
private String lastName;
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getFirstName() {
return firstName;
}
public String getLastName() {
return lastName;
}
}
您的Command物件將接收一個自訂型態的User實例,然而從HTTP接收到的參數值是String型態,您可以撰寫一個 UserPropertyEditor類別做轉換,通常直接繼承java.beans.PropertyEditorSupport並重新定義它的 getAsText()及setAsText()方法,例如:
- UserPropertyEditor.java
package onlyfun.caterpillar;
import java.beans.PropertyEditorSupport;
public class UserPropertyEditor extends PropertyEditorSupport {
public String getAsText() {
Object o = this.getValue();
if(o == null || !(o instanceof User)) {
return null;
}
User user = (User) o;
String name = user.getFirstName()
+ "," + user.getLastName();
return name;
}
public void setAsText(String text) {
String[] tokens = text.split(",");
User user = new User();
user.setFirstName(tokens[0]);
user.setLastName(tokens[1]);
setValue(user);
}
}
當必須從指定的物件轉換為字串時,會呼叫getAsText()方法,而接收到參數要將之轉換為指定的物件時,會呼叫setAsText()方法,接著來撰寫一個測試的Controller:
- SomeFormController.java
package onlyfun.caterpillar;
import org.springframework.web.servlet.
mvc.SimpleFormController;
import org.springframework.web.servlet.*;
import java.util.*;
import javax.servlet.http.HttpServletRequest;
import org.springframework.web.
bind.ServletRequestDataBinder;
public class SomeFormController extends SimpleFormController {
public SomeFormController() {
setCommandClass(SomeForm.class);
}
protected ModelAndView onSubmit(
Object command) throws Exception {
SomeForm form = (SomeForm) command;
Map model = new HashMap();
model.put("firstName",
form.getUser().getFirstName());
model.put("lastName",
form.getUser().getLastName());
return new ModelAndView(this.getSuccessView(), model);
}
protected void initBinder(HttpServletRequest request,
ServletRequestDataBinder binder)
throws Exception {
super.initBinder(request, binder);
binder.registerCustomEditor(
User.class, new UserPropertyEditor());
}
}
注意到這邊重新定義了initBinder()方法,並在當中使用ServletRequestDataBinder的registerCustomEditor()方法註冊了自訂的PropertyEditor,來看看定義檔的內容:
- mvc-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING/DTD BEAN/EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="urlMapping"
class="org.springframework.web.servlet.
→ handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="/index.do">
indexController
</prop>
<prop key="/someForm.do">
someFormController
</prop>
</props>
</property>
</bean>
<bean id="viewResolver"
class="org.springframework.web.servlet.
→ view.InternalResourceViewResolver">
<property name="prefix">
<value>/WEB-INF/jsp/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
<bean id="indexController"
class="org.springframework.web.servlet.
→ mvc.ParameterizableViewController">
<property name="viewName">
<value>index</value>
</property>
</bean>
<bean id="someFormController"
class="onlyfun.caterpillar.SomeFormController">
<property name="successView">
<value>hello</value>
</property>
</bean>
</beans>
現在假設撰寫有一個測試網頁:
- index.jsp
<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>
<html>
<head>
<meta http-equiv="Content-Type"
content="text/html; charset=UTF-8">
<title>Form</title>
</head>
<body>
<form name="someForm"
action="/PropertyEditorDemo/someForm.do" method="POST">
問題一 <input type="text" name="user"/><br>
<input type="submit" value="Submit"/>
</form>
</body>
</html>
在輸入欄位中,指定由user屬性來接受輸入,當資料送出後,會經由PropertyEditor的轉換,假設呈現處理結果的hello.jsp網頁是如下撰寫的:
- hello.jsp
<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>
<html>
<head>
<meta http-equiv="Content-Type"
content="text/html; charset=UTF-8">
<title>User Info</title>
</head>
<body>
<h1>\${firstName} - \${lastName}</h1>
</body>
</html>
如果您在index.jsp填入"Justin,Lin",則結果會顯示"Justin-Lin"。