吴裕雄--天生自然轻量级JAVA EE企业应用开发Struts2Sping4Hibernate整合开发学习笔记:Struts2的类型转换:全局类型转换器

随声附和 提交于 2020-02-26 02:00:10
<?xml version="1.0" encoding="GBK"?>
<project name="struts" basedir="." default="">
    <property name="dist" value="classes"/>
    <property name="src" value="src"/>
    
    <path id="classpath">
        <fileset dir="lib">
            <include name="*.jar"/>
        </fileset>
        <pathelement path="${dist}"/>
    </path>

    <target name="compile" description="Compile all source code">
        <delete dir="${dist}"/>
        <mkdir dir="${dist}"/>
        <copy todir="${dist}">
            <fileset dir="${src}">
                <exclude name="**/*.java"/>
            </fileset>        
        </copy>
        <javac destdir="classes" debug="true" includeantruntime="yes"
            deprecation="false" optimize="false" failonerror="true">
            <src path="${src}"/>
            <classpath refid="classpath"/>
        </javac>
    </target>

</project>
<?xml version="1.0" encoding="GBK"?>

<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
    http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1">
    <!-- 定义Struts 2的核心Filter -->
    <filter>
        <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
    </filter>
    <!-- 让Struts 2的核心Filter拦截所有请求 -->
    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
</web-app>
<%--
网站: <a href="http://www.crazyit.org">疯狂Java联盟</a>
author  yeeku.H.lee kongyeeku@163.com
version  1.0
Copyright (C), 2001-2016, yeeku.H.Lee
This program is protected by copyright laws.
Program Name:
Date: 
--%>

<%@ page contentType="text/html; charset=GBK" language="java" errorPage="" %>
<%@taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>全局类型转换器</title>
</head>
<body>
<h3>全局类型转换器</h3>
<s:form action="login">
    <s:textfield name="user" label="用户"/>
    <tr>
        <td colspan="2"><s:submit value="转换" theme="simple"/>
        <s:reset value="重填" theme="simple"/></td>
    </tr>
</s:form>
</body>
</html>
<%--
网站: <a href="http://www.crazyit.org">疯狂Java联盟</a>
author  yeeku.H.lee kongyeeku@163.com
version  1.0
Copyright (C), 2001-2016, yeeku.H.Lee
This program is protected by copyright laws.
Program Name:
Date: 
--%>

<%@ page contentType="text/html; charset=GBK" language="java" errorPage="" %>
<%@taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>转换结果</title>
</head>
<body>
    <s:actionmessage/>
    用户名为:<s:property value="user.name"/><br/>
    密码为:<s:property value="user.pass"/><br/>
</body>
</html>
<?xml version="1.0" encoding="GBK"?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
    "http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
    <package name="lee" extends="struts-default">
        <action name="login" class="org.crazyit.app.action.LoginAction">
            <result>/WEB-INF/content/welcome.jsp</result>
            <result name="error">/WEB-INF/content/welcome.jsp</result>
        </action>
        <action name="*">
            <result>/WEB-INF/content/{1}.jsp</result>        
        </action>
    </package>
</struts>
#指定org.crazyit.app.domain.User类型需要
#使用UserConverter类来完成类型转换
org.crazyit.app.domain.User=org.crazyit.app.converter.UserConverter
package org.crazyit.app.action;

import com.opensymphony.xwork2.ActionSupport;

import org.crazyit.app.domain.*;

/**
 * Description:
 * <br/>网站: <a href="http://www.crazyit.org">疯狂Java联盟</a>
 * <br/>Copyright (C), 2001-2016, Yeeku.H.Lee
 * <br/>This program is protected by copyright laws.
 * <br/>Program Name:
 * <br/>Date:
 * @author  Yeeku.H.Lee kongyeeku@163.com
 * @version  1.0
 */
public class LoginAction extends ActionSupport
{
    private User user;

    // user的setter和getter方法
    public void setUser(User user)
    {
        this.user = user;
    }
    public User getUser()
    {
        return this.user;
    }
    public String execute() throws Exception
    {
        if (getUser().getName().equals("crazyit.org")
            && getUser().getPass().equals("leegang") )
        {
            addActionMessage("登录成功!");
            return SUCCESS;
        }
        addActionMessage("登录失败!!");
        return ERROR;
    }
}
package org.crazyit.app.converter;

import java.util.Map;
import ognl.DefaultTypeConverter;

import org.crazyit.app.domain.*;
/**
 * Description:
 * <br/>网站: <a href="http://www.crazyit.org">疯狂Java联盟</a>
 * <br/>Copyright (C), 2001-2016, Yeeku.H.Lee
 * <br/>This program is protected by copyright laws.
 * <br/>Program Name:
 * <br/>Date:
 * @author  Yeeku.H.Lee kongyeeku@163.com
 * @version  1.0
 */
public class UserConverter extends DefaultTypeConverter
{
    // 类型转换器必须重写convertValue方法,该方法需要完成双向转换
    public Object convertValue(Map context
        , Object value, Class toType)
    {
        // 当需要将字符串向User类型转换时
        if (toType == User.class )
        {
            // 系统的请求参数是一个字符串数组
            String[] params = (String[])value;
            // 创建一个User实例
            User user = new User();
            // 只处理请求参数数组第一个数组元素,
            // 并将该字符串以英文逗号分割成两个字符串
            String[] userValues = params[0].split(",");
            // 为User实例赋值
            user.setName(userValues[0]);
            user.setPass(userValues[1]);
            // 返回转换来的User实例
            return user;
        }
        else if (toType == String.class )
        {
            // 将需要转换的值强制类型转换为User实例
            User user = (User) value;
            return "<" + user.getName() + ","
                + user.getPass() + ">";
        }
        return null ;
    }
}
package org.crazyit.app.domain;

/**
 * Description:
 * <br/>网站: <a href="http://www.crazyit.org">疯狂Java联盟</a>
 * <br/>Copyright (C), 2001-2016, Yeeku.H.Lee
 * <br/>This program is protected by copyright laws.
 * <br/>Program Name:
 * <br/>Date:
 * @author  Yeeku.H.Lee kongyeeku@163.com
 * @version  1.0
 */
public class User
{
    private String name;
    private String pass;

    // name的setter和getter方法
    public void setName(String name)
    {
        this.name = name;
    }
    public String getName()
    {
        return this.name;
    }

    // pass的setter和getter方法
    public void setPass(String pass)
    {
        this.pass = pass;
    }
    public String getPass()
    {
        return this.pass;
    }

}

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title> 请求参数为字符串数组 </title>
    <meta name="author" content="Yeeku.H.Lee" />
    <meta name="website" content="http://www.crazyit.org" />
    <meta http-equiv="Content-Type" content="text/html; charset=GBK" />
</head>
<body>
<form method="post" action="test">
请输入您的姓名:<input type="text" id="name" name="name" /><br/>
选择意向课程:<select id="course" name="course" size="5" multiple="multiple">
        <option value="wjc" selected="selected">疯狂Java实训营</option>
        <option value="wpj">疯狂Java项目冲刺班</option>
        <option value="base">疯狂Java基础加强班</option>
    </select><br/>
<input type="submit" value="提交"/>
</form>
</body>
</html>

 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!