没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
试读
16页
我们知道,Struts2完成参数传递处理工作的基础是OGNL和ValueStack。而在这个 过程中,我也把Struts2所要做的工作大致归纳为两个方面: 1. 对OGNL操作进行封装,完成OGNL表达式所表示的值到Java对象的值传递机制 2. 在参数传递的过程中,做恰当的类型转化,保证页面上的字符串能够转化成各 式各样的Java对象 接下来,通过四个不同的角度,来具体讲述Struts2在这两个方面的工作。
资源推荐
资源详情
资源评论
Struts2中的参数传递
我们知道,Struts2完成参数传递处理工作的基础是OGNL和ValueStack。而在这个
过程中,我也把Struts2所要做的工作大致归纳为两个方面:
1. 对OGNL操作进行封装,完成OGNL表达式所表示的值到Java对象的值传递机制
2. 在参数传递的过程中,做恰当的类型转化,保证页面上的字符串能够转化成各
式各样的Java对象
接下来,通过四个不同的角度,来具体讲述Struts2在这两个方面的工作。
目 录 [ - ]
最简单的参数传递
Array、List、Map等容器类型的参数传递
文件上传
自定义的类型转化实现
最简单的参数传递
public class EnumTypeConverter extends DefaultTypeConverter
{
/**
* Converts the given object to a given type. How this is
to be done is implemented in toClass. The OGNL context, o
* and toClass are given. This method should be able to ha
ndle conversion in general without any context or object
* specified.
*
* @param context - OGNL context under which the conversio
n is being done
* @param o - the object to be converted
* @param toClass - the class that contains the code to co
nvert to enumeration
* @return Converted value of type declared in toClass or
TypeConverter.NoConversionPossible to indicate that the
* conversion was not possible.
P 1 / 16
Struts2中的参数传递
*/
public Object convertValue(Map context, Object o, Class to
Class) {
if (o instanceof String[]) {
return convertFromString(((String[]) o)[0], toClass);
} else if (o instanceof String) {
return convertFromString((String) o, toClass);
}
return super.convertValue(context, o, toClass);
}
/**
* Converts one or more String values to the specified cla
ss.
* @param value - the String values to be converted, such
as those submitted from an HTML form
* @param toClass - the class to convert to
* @return the converted object
*/
public java.lang.Enum convertFromString(String value, Clas
s toClass) {
return Enum.valueOf(toClass, value);
}
}
有了这个类,我们就可以比较轻松的对枚举类型进行数据赋值了。
Java代码
public enum Gender {
MALE, FEMALE
}
Html代码
<form method="post" action="/struts-example/enum-conversion.
P 2 / 16
Struts2中的参数传递
action">
<input type="text" name="user.name" value="downpour" />
<select name="user.gender">
<option value="MALE">?</option>
<option value="FEMALE">?</option>
</select>
<input type="submit" value="submit" />
</form>
Java代码
public class EnumConversionAction extends ActionSupport {
private static final Log logger = LogFactory.getLog(Policy
.class);
private User user;
/* (non-Javadoc)
* @see com.opensymphony.xwork2.ActionSupport#execute()
*/
@Override
public String execute() throws Exception {
logger.info("user's gender:" + user.getGender());
return super.execute();
}
// setters and getters
}
通过上面的代码,就完成了对枚举类型的赋值。不过这里有一点需要特别指出:那
就是XWork在XWork-2.1.X的版本之前,枚举类型不被默认支持。如果你需要获得
枚举类型的自动赋值,还需要增加一个配置文件xwork-conversion.properties到cl
asspath下:
Java代码
P 3 / 16
Struts2中的参数传递
java.lang.Enum=com.opensymphony.xwork2.util.EnumTypeConverte
r
对于使用新的版本的XWork的朋友,则不需要增加这个配置文件。
Date类型
XWork默认是支持Date类型的转化的。不过从源码上来看,貌似我们很难用上它默
认的类型转化。
Java代码
private Object doConvertToDate(Map context, Object value, Cl
ass toType) {
Date result = null;
if (value instanceof String && value != null && ((String
) value).length() > 0) {
String sa = (String) value;
Locale locale = getLocale(context);
DateFormat df = null;
if (java.sql.Time.class == toType) {
df = DateFormat.getTimeInstance(DateFormat.MEDIUM, l
ocale);
} else if (java.sql.Timestamp.class == toType) {
Date check = null;
SimpleDateFormat dtfmt = (SimpleDateFormat) DateForm
at.getDateTimeInstance(DateFormat.SHORT,
DateFormat.MEDIUM,
locale);
SimpleDateFormat fullfmt = new SimpleDateFormat(dtfm
t.toPattern() + MILLISECOND_FORMAT,
locale);
SimpleDateFormat dfmt = (SimpleDateFormat) DateForma
t.getDateInstance(DateFormat.SHORT,
locale);
SimpleDateFormat[] fmts = {fullfmt, dtfmt, dfmt};
for (int i = 0; i < fmts.length; i++) {
P 4 / 16
剩余15页未读,继续阅读
资源评论
- zhhwyybwzl2014-03-01真心不咋地没有将评出·清楚
leeway66
- 粉丝: 0
- 资源: 10
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功