欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

类型转换

程序员文章站 2024-03-18 20:49:58
...
类型转换:
步骤:
1. Input.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<h1>坐标请用逗号分隔</h1>
<form action="converterAction.action" method="post">
坐标:<input type="text" name="point" size="20"/><br>
用户名:<input type="text" name="username" size="20" /><br>
年龄:<input type="text" name="age" size="20" /><br>
生日:<input type="text" name="birthday" size="20" /><br>

<input type="submit" value="提交"/>
</form>
</body>
</html>

2. Struts.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="struts2" extends="struts-default">
<action name="helloworld" class="com.test.action.HelloWorld">
<result>/helloworld.jsp</result>
</action>

<action name="login" class="com.test.action.LoginAction">
<result>/result.jsp</result>
</action>

<action name="converterAction" class="com.test.action.PointAction">
<result name="success">/output.jsp</result>
<result name="input">/error.jsp</result>
</action>

</package>
</struts>

3. Output.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
坐标:<s:property value="point"/><br>
用户名:<s:property value="username"/><br>
年龄:<s:property value="age"/><br>
生日:<s:property value="birthday" /><br>
</body>
</html>

4. PointAction类

package com.test.action;

import java.util.Date;

import com.opensymphony.xwork2.ActionSupport;
import com.test.bean.Point;

public class PointAction extends ActionSupport{

private Point point;
private String username;
private int age;
private Date birthday;

public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
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;
}
public Point getPoint() {
return point;
}
public void setPoint(Point point) {
this.point = point;
}

public String execute() throws Exception{
return "success";
}
}

5. Point类

package com.test.bean;

public class Point {
private int x;
private int y;

public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}

}

6. 在com.test.action包下创建PointAction.conversion.properties文件

point=com.test.converter.PointCoverter

7. PointConverter类

package com.test.converter;

import java.util.Map;

import ognl.DefaultTypeConverter;

import com.test.bean.Point;

public class PointCoverter extends DefaultTypeConverter {

@SuppressWarnings("unchecked")
@Override
public Object convertValue(Map context, Object value, Class toType) {

if(Point.class==toType){
String[] str=(String[])value;
String firstValue=str[0];
String[] result=firstValue.split(",");
Point point=new Point();
point.setX(Integer.parseInt(result[0]));
point.setY(Integer.parseInt(result[1]));

return point;
}else if(String.class==toType){
Point point=(Point)value;
int x=point.getX();
int y=point.getY();

return "x:"+x+",y:"+y;
}

return null;
}
}

类型转换(继承StrutsTypeConverter):
PointCoverter类如下,其它一样

package com.test.converter;

import java.util.Map;

import org.apache.struts2.util.StrutsTypeConverter;

import com.test.bean.Point;

public class PointCoverter1 extends StrutsTypeConverter {

@Override
public Object convertFromString(Map context, String[] values, Class toClass) {
String firstValue=values[0];
String[] result=firstValue.split(",");
Point point=new Point();
point.setX(Integer.parseInt(result[0]));
point.setY(Integer.parseInt(result[1]));

return point;

}

@Override
public String convertToString(Map context, Object o) {
Point point=(Point)o;
int x=point.getX();
int y=point.getY();

return "x:"+x+",y="+y;
}

}



类型转换(批量转换):
1. Input.jsp:

坐标:<input type="text" name="point" size="20"/><br>
坐标1:<input type="text" name="point" size="20"/><br>
坐标2:<input type="text" name="point" size="20"/><br>

2. Output.jsp:

坐标:<s:property value="point"/><br>

3. 主要是PointAction类:

private List<Point> point;

4. PointAction-conversion.properties文件

#point=com.test.converter.PointCoverter1
#point1=com.test.converter.PointCoverter1
point=com.test.converter.PointCoverter2

5. PointCoverter2ç±»

package com.test.converter;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.struts2.util.StrutsTypeConverter;

import com.test.bean.Point;

public class PointCoverter2 extends StrutsTypeConverter {

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

List<Point> list=new ArrayList<Point>();

for(String str : values){
String[] result=str.split(",");
Point point=new Point();
point.setX(Integer.parseInt(result[0]));
point.setY(Integer.parseInt(result[1]));

list.add(point);
}

return list;
}

@Override
public String convertToString(Map context, Object o) {
List<Point> list=(List<Point>)o;

StringBuffer buf=new StringBuffer();

int number=0;

for(Point point : list){
number++;
int x=point.getX();
int y=point.getY();

buf.append(number).append(". x=").append(x).append(",y=").append(y).append("\n");
}

return buf.toString();
}

}

类型转换(对象属性方式):
1. input.jsp

x:<input type="text" name="point.x" size="20"/><br>
y:<input type="text" name="point.y" size="20"/><br>

2. output.jsp

坐标:<s:property value="point"/><br>

3. PointAction-conversion.properties文件

#point=com.test.converter.PointCoverter1
#point1=com.test.converter.PointCoverter1
#point=com.test.converter.PointCoverter2

4. PointActionç±»:

private Point point;

5. Point类重写toStirng

@Override
public String toString() {
String str="x="+x+",y="+y;

return str;
}

类型转换(全局):
1. 在src下建立xwork-conversion.properties(固定)文件

com.test.bean.Point=com.test.converter.PointCoverter1

2. PointAction-conversion.properties文件

#point=com.test.converter.PointCoverter1
#point1=com.test.converter.PointCoverter1
#point=com.test.converter.PointCoverter2

3. Input.jsp中坐标:

<input type="text" name="point" size="20"/><br>