基于Struts2的全局类型转换器

 基于Struts2的全局类型转换器_第1张图片基于Struts2的全局类型转换器_第2张图片


LoginAction.java

public class LoginAction implements Action {

  //使用类型转换器将字符串请求参数直接转换成一个User实例
    private User user;
    private User customer;
    private Date birth;

  //封装Action处理结果的tip属性
    private String tip;
    public void setUser(User user) {
        this.user = user;
    }
    public void setCustomer(User customer) {
        this.customer = customer;
    }
    public void setTip(String tip) {
        this.tip = tip;
    }
    public void setBirth(Date d) {
        this.birth = d;
    }
    public User getUser() {
        return (this.user);
    }
    public User getCustomer() {
        return (this.customer);
    }
    public Date getBirth() {
        return this.birth;
    }
    public String getTip() {
        return (this.tip);
    }
    public String execute() throws Exception {
        if (getUser().getName().equals("admin")
                && getUser().getPass().equals("admin")) {
            setTip("转换成功");
            return SUCCESS;
        } else {
            setTip("转换失败");
            return ERROR;
        }
    }
}
UserConverter.java

public class UserConverter extends StrutsTypeConverter {
    public Object convertFromString(Map context, String[] values, Class toClass) {
        User user = new User();
        String[] userValues = values[0].split(",");
        user.setName(userValues[0]);
        user.setPass(userValues[1]);
        return user;
    }
    @Override
    public String convertToString(Map context, Object o) {
        User user = (User) o;
        return "<" + user.getName() + "," + user.getPass() + ">";
    }

}
User.java

public class User {
    private String name;
    private String pass;
    public void setName(String name) {
        this.name = name;
    }
    public void setPass(String pass) {
        this.pass = pass;
    }
    public String getName() {
        return (this.name);
    }
    public String getPass() {
        return (this.pass);
    }
}
struts.xml

<struts>
    <package name="jCuckoo" extends="struts-default">
         <action name="login" class="jCuckoo.LoginAction">
            <result name="success">/welcome.jsp</result>        
        </action>
    </package>
</struts>
xwork-conversion.properties

jCuckoojCuckoo.User=jCuckoo.UserConverter

web.xml

    <!-- 定义Struts2的FilterDispathcer的Filter -->
    <filter>
        <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
    </filter>

    <!-- FilterDispatcher用来初始化struts2并且处理所有的WEB请求。 -->
    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
input.jsp

<%@ page language="java" contentType="text/html; charset=GBK"%>
<html>
<head>
<title>全局类型转换器</title>
</head>
<body>
<form action="login.action" method="post">
    <table align="center" width="360">
    <caption><h3>全局类型转换器</h3></caption>
        <tr align="center">
            <td>用户信息的用户名和密码以英文逗号隔开</td>
        </tr>
        <tr>
            <td>请输入用户信息:<input type="text" name="user"/></td>
        </tr>
        <tr>
            <td>请输入客人信息:<input type="text" name="customer"/></td>
        </tr>
        <tr>
            <td>用户生日:<input type="text" name="birth"/></td>
        </tr>
        <tr align="center">
            <td><input type="submit" value="转换"/><input type="reset" value="重填"/></td>
        </tr>
    </table>
</form>
</body>
</html>

welcome.jsp

<%@ page language="java" contentType="text/html; charset=GBK"%>
<
%@taglib prefix="s" uri="/struts-tags"%>
<html>
    <head>
        <title>转换成功</title>
    </head>
    <body>
        转换成功!<br>
        用户的用户名为:<s:property value="user.name"/><br>
        用户的密码为:<s:property value="user.pass"/><br>
        用户的生日为:<s:property value="birth"/><br>
        客人的用户名为:<s:property value="customer.name"/><br>
        客人的密码为:<s:property value="customer.pass"/><br>
    </body>
</html>

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/guoquanyou/archive/2008/12/03/3439292.aspx

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/guoquanyou/archive/2008/12/03/3439292.aspx

你可能感兴趣的:(String,struts,user,Class,input,action)