structs2标签简单实用,及自定义转换器示例代码

一、在structs.xml中配置

<structs>
<package name="tagp" namespace="/test" extends="struts-default">
<action name="TagDemo" class="cn.hjp.test.TagDemo">
<result name="success" type="dispatcher">/tag/Index2.jsp</result>
</action>
</package>
</structs>

二、首页Index.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>
<span style="color:red">生日日期格式:yyyy/MM/dd</span>
<s:form action="TagDemo" namespace="/test"> <s:textfield name="name" label="姓名"></s:textfield> <s:textfield name="sex" label="性别"></s:textfield> <s:textfield name="age" label="年龄"></s:textfield> <s:textfield name="birthday" label="生日"></s:textfield> <s:submit value="提交"></s:submit> </s:form> </body> </html>

三、Action代码(注意要引用对包):

如果想实现下面的自动装配,必须这样生成属性setter和getter

public class TagDemo extends ActionSupport {

    private String name;
    private char sex;
    private int age;
    private Date birthday;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public char getSex() {
        return sex;
    }

    public void setSex(char sex) {
        this.sex = sex;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public Date getBirthday() {
        return birthday;
    }

    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }

    @Override
    public String execute() throws Exception {
        // TODO Auto-generated method stub
        System.out.println(this.toString());
        return SUCCESS;
    }

    @Override
    public String toString() {
        return "TagDemo [name=" + name + ", sex=" + sex + ", age=" + age + ", birthday=" + birthday + "]";
    }

}

四、自定义的转换器类(注意引用对包):

public class MyDateConverter extends DefaultTypeConverter {

    @Override
    public Object convertValue(Object value, Class toType) {
        // TODO Auto-generated method stub
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy/MM/dd");
        if (toType == Date.class) {
            String[] params = (String[]) value;
            try {
                return simpleDateFormat.parse(params[0]);
            } catch (ParseException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        if (toType == String.class) {
            Date date = (Date) value;
            return simpleDateFormat.format(date);
        }
        throw new RuntimeException("错误");
    }
}

五、下面就是配置如何使用自定义的转换器类,分两种:

1、如果只想对某个包下Action里的字段进行格式化,就要在这个包下(和action类同级)新建properties文件,依据上面示例则要建文件TagDemo-conversion.properties

代码:birthday=cn.hjp.test.MyDateConverter

2、如果要对所有时间类型格式化,则要在src根下新建文件xwork-conversion.properties

代码:java.util.Date=cn.hjp.test.MyDateConverter

六、action返回的页面,如果实现页面值的自动加载,也要使用structs标签,Index2.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:textfield name="name" label="姓名"></s:textfield>
<s:textfield name="sex" label="性别"></s:textfield>
<s:textfield name="age" label="年龄"></s:textfield>
<s:textfield name="birthday" label="生日"></s:textfield>
</body>
</html>

 

posted @ 2015-07-23 18:45  jiapeng  阅读(278)  评论(0编辑  收藏  举报