Struts2的自定义类型转换器实例

CircleConverter.java

package com.liuxi.typeconverter; import java.util.Map; import org.apache.struts2.util.StrutsTypeConverter; import com.liuxi.bean.Circle; import com.liuxi.bean.Point; /** * * @author liuxi * *Aug 18, 2010 5:21:17 PM *圆的转换器 */ public class CircleConverter extends StrutsTypeConverter{ @Override public Object convertFromString(Map context, String[] values, Class toClass) { Circle circle=new Circle(); Point center=new Point(); String centerValues[]=values[0].split(","); double x=Double.parseDouble(centerValues[0]); double y=Double.parseDouble(centerValues[1]); center.setX(x); center.setY(y); circle.setRadius(Double.parseDouble(centerValues[2])); circle.setCenter(center); return circle; } @Override public String convertToString(Map context, Object value) { Circle circle=(Circle)value; return circle.toString(); } }

 

CircleAction.java

 

package com.liuxi.typeconverter; import com.liuxi.bean.Circle; import com.opensymphony.xwork2.Action; import com.opensymphony.xwork2.ActionContext; public class CircleAction implements Action { private Circle circle; public Circle getCircle() { return circle; } public void setCircle(Circle circle) { this.circle = circle; } public String execute(){ System.out.println("在CircleAction中输出圆的信息"); System.out.println(this.getCircle()); ActionContext.getContext().put("circle", circle); return SUCCESS; } }

 

 

在包com.liuxi.typeconverter下创建文件CircleAction-conversion.properties:

circle=com.liuxi.typeconverter.CircleConverter

 

 

jsp页面:

createCircle.jsp页面:

 

 

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib prefix="s" uri="/struts-tags" %> <!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>输入一个圆</title> </head> <body> <TABLE bgcolor="orange" width="100%"> <tr ><td align="center">自定义类型转换器测试</td></tr> </TABLE> <s:form action="createCircle"> <table align="center" border="1" > <tr ><td colspan="2"><font color="#3737DE">按要求输入数据就能创建一个圆哦</font></td></tr> <tr> <td> <input type="text" name="circle"/> </td> <td> <font color="red">x,y,radius</font> </td> </tr> <tr><td colspan="3" align="center"><s:submit value="submit"/></td></tr> </table> </s:form> </body> </html>

 

 

 

circleMessage.jsp页面:

 

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib prefix="s" uri="/struts-tags" %> <!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="circle"/> </body> </html>

 

在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="converter" extends="struts-default" > <action name="createCircle" class="com.liuxi.typeconverter.CircleAction"> <result>/circleMessage.jsp</result> </action> </package> </struts>

 

 

运行效果:

 

数据输入页面:

 

Struts2的自定义类型转换器实例_第1张图片

 

结果显示:

 

 

Struts2的自定义类型转换器实例_第2张图片

 

 

 

 

 

 

 

 

你可能感兴趣的:(html,struts,String,object,Class,action)