ç±»å转æ¢
程序员文章站
2024-03-18 20:49:58
...
ç±»å转æ¢:
æ¥éª¤ï¼
1ï¼ Input.jsp
2ï¼ Struts.xml
3ï¼ Output.jsp
4ï¼ PointActionç±»
5ï¼ Pointç±»
6ï¼ å¨com.test.actionå ä¸å建PointAction.conversion.propertiesæ件
7ï¼ PointConverterç±»
ç±»å转æ¢(继æ¿StrutsTypeConverter):
PointCoverterç±»å¦ä¸ï¼å ¶å®ä¸æ ·
ç±»å转æ¢(æ¹é转æ¢):
1. Input.jspï¼
2. Output.jspï¼
3. 主è¦æ¯PointActionç±»:
4. PointAction-conversion.propertiesæ件
5. PointCoverter2ç±»
ç±»å转æ¢(对象å±æ§æ¹å¼):
1. input.jsp
2. output.jsp
3. PointAction-conversion.propertiesæ件
4. PointActionç±»:
5. Pointç±»éåtoStirng
ç±»å转æ¢(å ¨å±):
1. å¨srcä¸å»ºç«xwork-conversion.properties(åºå®)æ件
2. PointAction-conversion.propertiesæ件
3. Input.jspä¸åæ ï¼
æ¥éª¤ï¼
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>