您的位置:首页 > 编程语言 > Java开发

Struts2--类型转换

2016-04-15 16:24 501 查看
struts.xml:

<?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.1//EN"
"http://struts.apache.org/dtds/struts-2.1.dtd">

<struts>

<constant name="struts.devMode" value="true"></constant>
<package name="test" namespace="/" extends="struts-default">

<action name="test" class="com.bjsxt.action.TestAction">
<result>/test.jsp</result>
</action>

</package>

</struts>


TestAction.java:

package com.bjsxt.action;

import java.awt.Point;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.opensymphony.xwork2.ActionSupport;

public class TestAction extends ActionSupport {

private String name;
private int age;
private Date d;
Set<String> interests;
Map<String, String> users;
Point p;

List<Point> ps;
Map<String, Point> points;

@Override
public String execute() throws Exception {

return super.execute();
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public int getAge() {
return age;
}

public void setAge(int age) {
this.age = age;
}

public Date getD() {
return d;
}

public void setD(Date d) {
this.d = d;
}

public Set<String> getInterests() {
return interests;
}

public void setInterests(Set<String> interests) {
this.interests = interests;
}

public Map<String, String> getUsers() {
return users;
}

public void setUsers(Map<String, String> users) {
this.users = users;
}

public Point getP() {
return p;
}

public void setP(Point p) {
this.p = p;
}

public List<Point> getPs() {
return ps;
}

public void setPs(List<Point> ps) {
this.ps = ps;
}

public Map<String, Point> getPoints() {
return points;
}

public void setPoints(Map<String, Point> points) {
this.points = points;
}

}


  

全局注册转换 src下新建属性文件: /xwork-conversion.properties

java.awt.Point=com.bjsxt.converter.MyPointConverter


如果局部: 放在action包下: /TestAction-conversion.properties:

p=com.bjsxt.converter.MyPointConverter


  

转换java:

package com.bjsxt.converter;

import java.awt.Point;
import java.util.Map;

import org.apache.struts2.util.StrutsTypeConverter;

public class MyPointConverter extends StrutsTypeConverter{

@Override
public Object convertFromString(Map context, String[] values, Class toClass) {

Point p = new Point();
String[] strs = (String[])values;
String[] xy = strs[0].split(",");
p.x = Integer.parseInt(xy[0]);
p.y = Integer.parseInt(xy[1]);
return p;
}

@Override
public String convertToString(Map context, Object o) {
// TODO Auto-generated method stub
return o.toString();
}

}


  

如下访问:

http://localhost:8080/struts2_3700_type_conversion/test?name=a&age=20 http://localhost:8080/struts2_3700_type_conversion/test?&d=1988-08-08 12:39:56 http://localhost:8080/struts2_3700_type_conversion/test?&interests=math&interests=english http://localhost:8080/struts2_3700_type_conversion/test?&users['a']=usera&users['b']=userb http://localhost:8080/struts2_3700_type_conversion/test?p=2,3


test.jsp:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%@taglib uri="/struts-tags" prefix="s" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title>Type Conversion</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>
name:<s:property value="name"/><br/>
age:<s:property value="age"/><br/>
date:<s:property value="d"/><br/>
<s:date name="d" format="yyyy/MM/dd HH:mm:ss"/><br/>
<s:property value="interests"/><br/>
<s:property value="users"/><br/>
<s:property value="p"/><br/>
<s:property value="ps"/><br/>
points:<s:property value="points"/><br/>

</body>
</html>


  

测试结果:

http://localhost:8080/struts2_3700_type_conversion/test?d=1988-08-08 12:39:56
 http://localhost:8080/struts2_3700_type_conversion/test?interests=math&interests=english [math, english]
 http://localhost:8080/struts2_3700_type_conversion/test?name=a&age=20 结果:
name:a
age:20
 http://localhost:8080/struts2_3700_type_conversion/test?d=1988-08-08 12:39:56
结果: 1988/08/08 12:39:56
 http://localhost:8080/struts2_3700_type_conversion/test?interests=math&interests=english 结果: [math, english]
 http://localhost:8080/struts2_3700_type_conversion/test?users['a']=usera&users['b']=userb 结果: {b=userb, a=usera}
 http://localhost:8080/struts2_3700_type_conversion/test?p=2,3 java.awt.Point[x=2,y=3]
 http://localhost:8080/struts2_3700_type_conversion/test?ps=2,3&ps=9,0 结果:[java.awt.Point[x=2,y=3], java.awt.Point[x=9,y=0]]
 http://localhost:8080/struts2_3700_type_conversion/test?points['a']=8,9&points['b']=1,0 结果: points:{b=java.awt.Point[x=1,y=0], a=java.awt.Point[x=8,y=9]}


  

  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: