局部類型轉換:
?
?
在對應的Action的同級目錄下新建Action名-conversion.properties(一定要與Action類名對應)
其內容為: 目標轉換對象=轉換器類(包名+類名)
?
1、頁面發送請求
?
?
<form action="convert">
日期類型:<input type="text" name="birthday">yyyy-mm-dd<br>
整數類型:<input type="text" name="age"><br>
<input type="submit" value="提交">
</form>
2、找到對應的action,讓屬性有get和set方法
?
?
package hb.convert;
import java.util.Date;
import com.opensymphony.xwork2.ActionSupport;
public class ConvertAction extends ActionSupport{
Date birthday;
int age;
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
@Override
public String execute() throws Exception {
System.out.println(this.birthday);
System.out.println(this.age);
return super.execute();
}
}
?
3、寫一個類型轉換類(繼承StrutsTypeConverter類)
?
package hb.convert;
import java.util.Date;
import java.util.Map;
import org.apache.struts2.util.StrutsTypeConverter;
public class ParamTypeConvert extends StrutsTypeConverter {
//由字符串轉為對象執行
convertFromString方法
@Override
public Object convertFromString(Map map, String[] str, Class clazz) {
//前端傳遞過來的字符串
String datestr = str[0];
System.out.println(datestr);
//action中定義的參數類型,在配置文件中定義
System.out.println(clazz.toString());
System.out.println(map);
return new Date();
}
//由對象轉為字符串執行
convertToString方法
@Override
public String convertToString(Map map, Object obj) {
Date date = (Date)obj;
long l = date.getTime();
String result = new Long(l).toString();
return result;
}
}
?
4、在action同目錄下面創建與action同名的properties文件 ConvertAction-conversion.properties
? ? 指明action對應的屬性由哪個類來轉換
?
?
birthday=hb.convert.ParamTypeConvert
?
?
5、配置struts.xml文件,查看內容
?
<!-- 測試類型轉換 -->
<action name="convert" class="hb.convert.ConvertAction">
<result name="success">/resource/convert/sucess.jsp</result>
</action>
?
6、顯示屬性展示到前端的值
?
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'sucess.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<s:property value="birthday"/><br>
</body>
</html>
?
?
?
?
總結:
1、如果是默認類型,struts2會幫我們自動轉換,即例子中的String轉為int類型,這個不需要我們自己操作
2、類型轉換的配置文件名要跟action類同名,并且在同一個目錄下面
3、action屬性名=包+轉換類
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061
微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元

