您的位置:首页 > 其它

Action中的方法调用和自定义类型转换器

2012-06-03 20:38 274 查看
一,调用Action中的方法
public
class
Demo1Action {
。。。
public String save(){
System.out.println("我是save方法");
return
"savePage";
}

public String update(){
System.out.println("我是update方法");
return
"update";
}
}

1,动态方法调用
http://localehost:8080/工程名/namespace名/test!save.action
通过一个“!”+ 方法的名字来调用Action中的方法

对于动态方法的调用,可以通过一个常量来设置

<constant
name="struts.enable.DynamicMethodInvocation"value="false"/>

2,通配符调用(推荐使用)

<action
name="test_*Method"
class="..."
method="{1}">
<result
name="success">/show.jsp</result>
</action>

这里可以使用多个通配符:如*Test_* , 还要注意的是,{}不仅可以使用在method=””里面,还可以使用在result的names属性里面,或者是在<result></result>里面

注意:{}是以1开始,不是0


访问地址:
http://localehost:8080/工程名/namespace名/test_saveMethod.action
二,接收get/post的参数
注意:如果想struts2帮我们注入参数,或者让页面得到参数,那么一定要记得给属性生成setter和getter方法

public
class
Demo2Action {
private String
username;
private Person person;

。。。//setter getter方法以及Action的方法



·get方式的传值

简单的属性值:(Action会自动接收)

<a href=”${pageContext.request.contextPath}/hwt/demo.action?username=hwt”>get传值</a>

传入对象的属性:

<a href=”${pageContext.request.contextPath}/hwt/demo.action?person.age=18”>get传值</a>

·post传值

<form action=””method=”post” >

<input type=”text” name=”username”/>

<input type=”text” name=”person.age”/>

<input type=”submit” value=”提交” />

</form>

struts2.1.6版本中存在一个Bug,即接收到的中文请求参数为乱码(以post方式提交),原因是struts2.1.6在获取并使用了请求参数后才调用HttpServletRequest的setCharacterEncoding()方法进行编码设置,导致应用使用的就是乱码请求参数。这个bug在struts2.1.8中已经被解决,如果你使用的是struts2.1.6,要解决这个问题,你可以这样做:新建一个Filter,把这个Filter放置在Struts2的Filter之前,然后在doFilter()方法里添加以下代码

public void doFilter(...){

HttpServletRequestreq = (HttpServletRequest) request;

req.setCharacterEncoding("UTF-8");//应根据你使用的编码替换UTF-8

filterchain.doFilter(request,response);

}

三,自定义类型的转换器
1,先建立一个类继承DefaultTypeConverter,重写convertValue(Map,object,class)这个方法

2,写个配置文件:

a,action级别—在Action所在的包中,名字为ActionClassName-conversion.properties,

名字是固定的,除了ActionClassName以外,属性文件中的内容为:

待转的属性名字=转换器的全类名

b,全局—在类文件下,名字为xwork-conversion.properties,名字是固定的,属性文件的内容为:

待转类型=转换器的全类名

如:java.util.Date类型的属性可以接收格式为2009-07-20的请求参数值。但如果我们需要接收格式为20091221的请求参数,我们必须定义类型转换器,否则struts2无法自动完成类型转换。

importjava.util.Date;

publicclass person{

private Datebirthday; //能接收2012-6-3但是接收不了20120603

public Date getBirthday () {

return birthday;

}

public void setBirthday(Date birthday) {

this. birthday = birthday;

}

}

1,建立实现类

public class DateConverter extendsDefaultTypeConverter {

public Object convertValue(Map context, Object value, Class toType) {

SimpleDateFormatdateFormat = new SimpleDateFormat("yyyyMMdd");

try{

if(toType== Date.class){//当字符串向Date类型转换时

String[]params = (String[]) value;// Request.getParameterValues()

returndateFormat.parse(params[0]);

}elseif(toType == String.class){//当Date转换成字符串时

Datedate = (Date) value;

returndateFormat.format(date);

}

}catch (ParseException e) {}

returnnull;

}

}

2,建立配置文件

Action :名字:PersonAction-conversion.properties

内容为:birthday=hwt.convertor.DateConverter

全局:名字:xwork-conversion.properties

内容为:java.util.Date=hwt.convertor.DateConverter
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐