struts2简单类型参数转换器(拦截器自动转换)

这边测试类型int,string,date,list(set),map,下面贴代码

struts.xml文件代码

        <!-- 类型转换 -->
        <action name="ConvertAction_*" class="convert.CovertAction" method="{1}">
            <result name="success" type="dispatcher">/convert.jsp</result>
        </action>

定义action类

public class CovertAction extends ActionSupport{
    
    private int age;
    private Date date;
    private List<String> interests;
    private String name;
    private Map<String, String> map;
    
    public Map<String, String> getMap() {
        return map;
    }
    public void setMap(Map<String, String> map) {
        this.map = map;
    }
    public int getAge() {
        return age;
    }
    public Date getDate() {
        return date;
    }
    public List<String> getInterests() {
        return interests;
    }
    public String getName() {
        return name;
    }
    public String hello(){
           
           return SUCCESS;
       }
    public void setAge(int age) {
        this.age = age;
    }
    public void setDate(Date date) {
        this.date = date;
    }
    public void setInterests(List<String> interests) {
        this.interests = interests;
    }
    
   public void setName(String name) {
    this.name = name;
}
    
    
}

前端测试页面

    age:<s:property value="age"/><br/>
    name:<s:property value="name"/><br/>
    date:<s:property value="date"/><br/>
    <s:date name="date" format="yyyy-MM-dd"/><br/>
    interests:<s:property value="interests"/><br/>
    map:<s:property value="map"/>
    <s:debug></s:debug>

测试url

http://localhost:8081/struts05/hello/ConvertAction_hello.action?name=xxxx&age=11&date=1999-01-01&interests=math&interests=english&map['a']=aaa&map['b']=bbb

result:

猜你喜欢

转载自www.cnblogs.com/Danial7777777/p/9153501.html
今日推荐