Wu Yuxiong - natural born flyweight JAVA EE enterprise application development Struts2Sping4Hibernate integrated development study notes: Struts2 type conversion: Set collection process

<?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">
    <! - define the core of Filter Struts 2 ->
    <filter>
        <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
    </filter>
    <! - let Struts core Filter 2 intercept all requests ->
    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
</web-app>
<?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>
<% - 
Web site: <A href = "http://www.crazyit.org"> crazy league Java </a> 
author yeeku.H.lee kongyeeku @ 163 .com
which version of the   1.0 for 
Amsterdam Copyright (the C), 20012012 , yeekuHLee
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> attribute type conversion Set </ title>
</head>
<body>
<H3> attribute type conversion Set </ h3>
<s:form action="login">
    <S: textfield name = "users" label = "first user message" />
    <S: textfield name = "users" label = "second user information" />
    <S: textfield name = "birth" label = "User Birthday" />
    <tr>
        <td colspan="2"><s:submit value="转换" theme="simple"/>
        <s:reset value="重填" theme="simple"/></td>
    </tr>
</s:form>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=GBK"%>
<%@taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
    <Title> conversions </ title>
</head>
<body>
    <s:actionmessage/>
    <s:property value="users"/>
<! - users access attribute set in the attribute name index attribute value crazyit.org element -> 
user crazyit.org user name: <S: Property value = "users ( 'crazyit.org') name. "/> <br/>
<! - set of users access pass indexed attribute value in the Attribute element crazyit.org -> 
user's password crazyit.org: <S: Property value = "users ( 'crazyit.org') pass." /> <br/>
<! - set of users access attribute name attribute element index in the attribute value fkit -> 
user fkit user name: <S: Property value = "users ( 'fkit') name." /> <Br / >
<! - set of users access pass indexed attribute value in the Attribute element fkit -> 
user's password fkit: <:. "Users ( 'fkit') pass" Property value S = /> a 
birthday: <S: Property value = "birth" /> a
</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>
package org.crazyit.app.action;

import com.opensymphony.xwork2.*;
import java.util.Date;
import java.util.Set;

/**
 * Description:
 * <br/> website: <a href=" http://www.crazyit.org "> crazy Java league </a>
 * <br/>Copyright (C), 2001-2012, Yeeku.H.Lee
 * <br/>This program is protected by copyright laws.
 * <br/>Program Name:
 * <br/>Date:
 * Author   YeekuHLee kongyeeku163com
 * @version  1.0
 */
public class LoginAction extends ActionSupport
{
    private Set users;
    private Date birth;
    // users的setter和getter方法
    public void setUsers(Set users)
    {
        this.users = users;
    }
    public Set getUsers()
    {
        return this.users;
    }
    // Birth of setter and getter methods 
    public  void setBirth (a Date Birth)
    {
        this.birth = birth;
    }
    public Date getBirth()
    {
        return this.birth;
    }
    // do not provide execute () method,
     // is used directly execute ActionSupport () method 
}
# Users specified attribute type converter is UserConverter
users= org.crazyit.app.converter.UserConverter
# Specified index attribute users a collection of attributes in the collection element is the name
KeyProperty_users=name
package org.crazyit.app.converter;

import java.util.*;
import org.apache.struts2.util.StrutsTypeConverter;

import org.crazyit.app.domain. * ;
/ **
 * Description:
 * <br/> website: <a href=" http://www.crazyit.org "> crazy Java league </a>
 * <br/>Copyright (C), 2001-2016, Yeeku.H.Lee
 * <br/>This program is protected by copyright laws.
 * <br/>Program Name:
 * <br/>Date:
 * Author YeekuHLee kongyeeku163com
 * @version 1.0
 */
public class UserConverter extends StrutsTypeConverter
{
    public Object convertFromString(Map context
        , String[] values, Class toClass)
    {
        Set result = new HashSet();
        for (int i = 0; i < values.length ; i++ )
        {
            // Create a User instance 
            User User = new new User ();
             // only the first array element processing request parameter array,
             // and the comma character string into two strings 
            String [] userValues = values [ I] .split ( "," );
             // for the User instance property assignment 
            user.setName (userValues [0 ]);
            user.setPass (userValues [ . 1 ]);
             // The Set User instance to set 
            result.add (user);
        }
        return result;
    }
    public String convertToString(Map context, Object o)
    {
        // If the type of the object to be converted is the Set 
        IF (o.getClass () == the Set. Class )
        {
            Set users = (Set)o;
            String result = "[";
            for (Object obj : users )
            {
                User user = (User)obj;
                result += "<" + user.getName()
                    + "," + user.getPass() + ">";
            }
            return result + "]";
        }
        else
        {
            return "";
        }
    }
}
package org.crazyit.app.domain;

/**
 * Description:
 * <br/> website: <a href=" http://www.crazyit.org "> crazy Java league </a>
 * <br/>Copyright (C), 2001-2012, Yeeku.H.Lee
 * <br/>This program is protected by copyright laws.
 * <br/>Program Name:
 * <br/>Date:
 * Author   YeekuHLee kongyeeku163com
 * @version  1.0
 */
public class User
{
    private String name;
    private String pass;

    // setter and getter method name of 
    public  void setName (String name)
    {
        this.name = name;
    }
    public String getName()
    {
        return this.name;
    }

    // setter and getter methods pass a 
    public  void SETPASS (String pass)
    {
        this.pass = pass;
    }
    public String getPass()
    {
        return this.pass;
    }

    public boolean equals(Object obj)
    {
        // If the two objects to be compared is the same object, the direct return to true 
        IF ( the this == obj)
        {
            return true;
        }
        // Only when the User object obj is 
        IF (obj! = Null && obj.getClass () == User. Class )
        {
            User the User = (the User) obj;
             // name attribute that is equal to two objects that they are equal 
            return  the this .getName () the equals (user.getName ()).;
        }
        return false;
    }
    // The name calculating the hashCode 
    public  int the hashCode ()
    {
        return name.hashCode();
    }
}

 

Guess you like

Origin www.cnblogs.com/tszr/p/12364841.html