吴裕雄--天生自然轻量级JAVA EE企业应用开发Struts2Sping4Hibernate整合开发学习笔记:Struts2的类型转换:类型转换中的错误处理(2)

<?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>
xwork.default.invalid.fieldvalue={0}字段无效
xwork.default.invalid.fieldvalue={0}\u5b57\u6bb5\u65e0\u6548
<?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>
    <constant name="struts.custom.i18n.resources" value="mess"/>
    <constant name="struts.i18n.encoding" value="GBK"/>
    <package name="lee" extends="struts-default">

        <!-- 定义处理用户请求的Action -->
        <action name="login" class="org.crazyit.app.action.LoginAction">
            <!-- 配置名为input的逻辑视图,当转换失败后转入该逻辑视图 -->
            <result name="input">/WEB-INF/content/input.jsp</result>
            <!-- 配置名为success的逻辑视图 -->
            <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>
<%--
网站: <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>
    <s:head/>
</head>
<body>
<s:fielderror />
<h2>类型转换的错误处理</h2>
<p><b>请输入用户信息</b></p>
<s:form action="login">
    <s:textfield name="user.name" label="用户名"/>
    <s:textfield name="user.age" label="年龄"/>
    <s:textfield name="user.birth" 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:property value="user.name"/><br/>
    年龄:<s:property value="user.age"/><br/>
    生日:<s:property value="user.birth"/><br/>
</body>
</html>
xwork.default.invalid.fieldvalue={0}字段无效
xwork.default.invalid.fieldvalue={0}\u5b57\u6bb5\u65e0\u6548
<?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>
    <constant name="struts.custom.i18n.resources" value="mess"/>
    <constant name="struts.i18n.encoding" value="GBK"/>
    <package name="lee" extends="struts-default">

        <!-- 定义处理用户请求的Action -->
        <action name="login" class="org.crazyit.app.action.LoginAction">
            <!-- 配置名为input的逻辑视图,当转换失败后转入该逻辑视图 -->
            <result name="input">/WEB-INF/content/input.jsp</result>
            <!-- 配置名为success的逻辑视图 -->
            <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>
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 [email protected]
 * @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 user;
    }
    // 没有execute方法,直接使用ActionSupport的execute方法
}
invalid.fieldvalue.user.birth=生日信息必须满足yyyy-MM-dd格式
invalid.fieldvalue.user.birth=\u751f\u65e5\u4fe1\u606f\u5fc5\u987b\u6ee1\u8db3yyyy-MM-dd\u683c\u5f0f
package org.crazyit.app.domain;

import java.util.Date;
/**
 * 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 [email protected]
 * @version  1.0
 */
public class User
{
    private String name;
    private Integer age;
    private Date birth;
    // name的setter和getter方法
    public void setName(String name)
    {
        this.name = name;
    }
    public String getName()
    {
        return this.name;
    }

    // age的setter和getter方法
    public void setAge(Integer age)
    {
        this.age = age;
    }
    public Integer getAge()
    {
        return this.age;
    }

    // birth的setter和getter方法
    public void setBirth(Date birth)
    {
        this.birth = birth;
    }
    public Date getBirth()
    {
        return this.birth;
    }

}

猜你喜欢

转载自www.cnblogs.com/tszr/p/12364859.html
今日推荐