• 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>
  • 相关阅读:
    在MVC中更新ModelFirst Entity Framework POCO实体外键的方法
    美国行照片集十四:洛杉矶拉斯维加斯之旅
    深入ASP.NET MVC之三:Controller的激活
    美国行照片集之十三:感恩节之旅
    深入ASP.NET MVC 之一:IIS到路由表
    博客样式改版
    关于如何用string保存二进制数据的问题
    XSI IPC机制的优缺点
    C,C++,java,python对比
    grunt requireJS 的基础配置
  • 原文地址:https://www.cnblogs.com/hujiapeng/p/4671375.html
Copyright © 2020-2023  润新知