重要特性總結
-
Struts2 的類型轉換,對於 8 個原生數據類型以及 Date,String 等常見類型,Struts2 可以使內建的類型轉換器實現自動的轉換;但對於自定義的對象類型來說,需要我們自己指定類型轉換的方式,即自定義類型轉換器。
-
對於自定義類型的轉換器來說,需要提供三個信息:Action 的名字、Action 中待轉換的屬性名以及該屬性對應的類型轉換器。
其中 Action 的名字是通過屬性文件的
文件名
來獲得的、 Action 中待轉換的屬性名是通過屬性文件中的key
來獲得的,該屬性對應的類型轉換器是通過該key
對應的value
來獲得的。
StrutsTypeConverter
Base class for type converters used in Struts. This class provides two abstract methods that are used to convert both to and from strings -- the critical functionality that is core to Struts's type coversion system.
Type converters do not have to use this class. It is merely a helper base class, although it is recommended that you use this class as it provides the common type conversion contract required for all web-based type conversion.
There's a hook (fall back method) called performFallbackConversion
** of which could be used to perform some fallback conversion if** convertValue
method of this failed. By default it just ask its super class (Ognl's DefaultTypeConverter) to do the conversion.
To allow the framework to recognize that a conversion error has occurred, throw an XWorkException or preferable a TypeConversionException.
使用 StrutsTypeConverter 進行類型轉換實例
新建一個 test.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Struts2.3 converter</title>
</head>
<body>
<form action="userAction.action">
user info: <input type="text" name="user"/><br/>
<input type="submit" value="submit"/>
</form>
</body>
</html>
流程處理 Struts.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<package name="struts2" extends="struts-default">
<action name="userAction" class="space.terwer.struts23.UserAction">
<result name="success">/output.jsp</result>
</action>
</package>
</struts>
Action 處理類 UserAction
package space.terwer.struts23;
import com.opensymphony.xwork2.ActionSupport;
import space.terwer.bean.User;
public class UserAction extends ActionSupport {
private User user;
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
@Override
public String execute() throws Exception {
System.out.println("username:" + this.getUser().getUsername());
System.out.println("password:" + this.getUser().getPassword());
return SUCCESS;
}
}
類型轉換器 UserConverter2.java
package space.terwer.converter;
import java.util.Map;
import org.apache.struts2.util.StrutsTypeConverter;
import space.terwer.bean.User;
public class UserConverter2 extends StrutsTypeConverter {
@Override
public Object convertFromString(Map context, String[] values, Class toClass) {
String firstValue = values[0];
String[] st = firstValue.split(";");
String uername = st[0];
String password = st[1];
User user = new User();
user.setUsername(uername);
user.setPassword(password);
return user;
}
@Override
public String convertToString(Map context, Object o) {
User user = (User) o;
String username = user.getUsername();
String password = user.getPassword();
String userInfo = "Use StrutsTypeConverter converted from UserConverter2=>username: " + username
+ ", password: " + password;
return userInfo;
}
}
類型轉換配置 UserAction-conversion.properties
user=space.terwer.converter.UserConverter2
結果展示 output.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Struts2.3 converter result</title>
</head>
<body>
<s:property value="user"/>
</body>
</html>
實際效果
總結
可以使用 Struts2
框架自帶的類型轉換器 StrutsTypeConverter
簡化類型轉換代碼的編寫,StrutsTypeConverter
類繼承於 DefaultTypeConverter
父類,並且提供了兩個抽象方法: convertFromString
和 convertToString
,分別表示 從頁面額字符串轉換爲後臺對象 以及 從後臺對象轉換爲頁面字符串 ,我們 只需要實現這兩個抽象方法即可實現類型轉換 。