1:Struts2的类型转换默认的框架默默的帮助用户完成转换功能,主要用户输入的数据合理地转换为需要的属性的类型,Struts2框架就会在execute方法执行之前,替用户将参数转化为合理的类型
2:Struts2内建类型转换器
int/Integer : 整数型
short/Short: 短整数型
long/Long: 长整型
float: Float: 浮点型
double/Double: 双精度型
boolean/Boolean: 布尔型
byte/Byte: 字节型
char/Character: 字符型
BigInteger: 大整数型
Date: 日期类型
3:枚举类型
定义一个枚举类型:
package com.capinfotech.model; public enum ColorEnum { red, blue, green; }
定义相应的Action类:
package com.capinfotech.action; import com.capinfotech.model.ColorEnum; import com.opensymphony.xwork2.ActionSupport; public class ConverterAction extends ActionSupport { private ColorEnum color; public ColorEnum getColor() { return color; } public void setColor(ColorEnum color) { this.color = color; } public String execute() throws Exception { System.out.println("color=" + color); System.out.println("您输入的颜色是红色吗?答案是:" + (color == color.red)); return SUCCESS; } }
<action name="convert" class="com.capinfotech.action.ConverterAction"> <result name="success">/converter/success.jsp</result> </action>
页面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> 用户输入的颜色是:<s:property value="color"/> </body> </html>
在浏览器中输入信息,得到的信息如下图:
*数组和List
Action类里面有两个属性
private int heights[];
private List<Integer> weights;
在页面中需要添加时:
<s:textfield name="heights" />
<s:textfiled name="heights" />
<s:textfield name="weights[0]" />
<s:textfield name="weights[1]" />
Action里面有个属性:
private Rectangle[] rectangles;
在页面中需要添加:
<s:textfield name="rectangles[0].height" />
<s:textfield name="rectangles[0].weight" />
<s:textfield name="rectangles[1].height" />
<s:textfield name="rectangles[1].weight" />
在Action里面有个Map属性
Map与数组及其类似,同样是用来存放多个单体数据,只不过数组用索引来区分不同的单体,而Map用Key来区分所有的单体。可以用"Map名['Key值']"这样的OGNL来
引用Map的值
private Map<String, Rectangle> map;
在页面上可以这样写:
<s:textfield name="map['first'].height" />
<s:textfield name="map['first'].weight" />
<s:textfield name="map['socond'].height" />
<s:textfield name="map['second'].weight" />
4:自定义类型转换器
需要继承类StrutsTypeConverter类,在该抽象类中需要用户实现两个方法:
public abstract Object convertFromString(Map context, String[] values, Class toClass);*context: 转换上下文,可以在里面引用各种对象,比如:可以通过context.get(ValueStack.VALUE_STACK)来引用值栈。
*values:用户输入的字符串
* 需要被转换为的数据类型
public abstract String convertToString(Map context, Object o);* o:需要转换的对象
自定义的类型转换器为:
package com.capinfotech.util; import java.util.Map; import org.apache.struts2.util.StrutsTypeConverter; import com.capinfotech.model.Rectangle; import com.opensymphony.xwork2.conversion.TypeConversionException; public class RectangleConverter extends StrutsTypeConverter { @Override public Object convertFromString(Map context, String[] values, Class toClass) { //长方形向字符串转换 String userInput = values[0]; String[] arr = userInput.split(":"); //在真正的格式转换前,先把所有的用户输入可能的错误拦截住 if(arr != null && arr.length != 2) { throw new TypeConversionException("请输入正确的长方形格式如:width:height"); } try { Rectangle rectangle = new Rectangle(); int width = Integer.parseInt(arr[0]); int height = Integer.parseInt(arr[1]); rectangle.setHeight(height); rectangle.setWidth(width); return rectangle; } catch(RuntimeException e) { throw new TypeConversionException("请输入正确的长方形格式如:width:height", e); } } @Override public String convertToString(Map context, Object o) { Rectangle rectangle = (Rectangle)o; return "长方形:宽:" + rectangle.getWidth() + ", 高:" + rectangle.getHeight(); } }
Action的内容为:
package com.capinfotech.action; import com.capinfotech.model.ColorEnum; import com.capinfotech.model.Rectangle; import com.opensymphony.xwork2.ActionSupport; public class ConverterAction extends ActionSupport { private Rectangle rectangle; public Rectangle getRectangle() { return rectangle; } public void setRectangle(Rectangle rectangle) { this.rectangle = rectangle; } public String execute() throws Exception { return SUCCESS; } }
提交页面为:
<%@ 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> <s:form action="/convert" method="post"> <s:textfield name="rectangle" label="输入长和高" /> <s:submit value="提交" /> </s:form> </body> </html>
成功页面为;
<%@ 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> <s:property value="rectangle"/> </body> </html>
输出为: