Struts基于List(未使用泛型)的类型转换

為{幸葍}努か 提交于 2019-11-29 11:10:13

一 视图

1 input.jsp

<%@ 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>直接封装成List</title>
</head>
<body>
<h3>直接封装成List</h3>
<s:form action="login">
    <s:textfield name="users[0].name" label="第一个用户名"/>
    <s:textfield name="users[0].pass" label="第一个密码"/>
    <s:textfield name="users[1].name" label="第二个用户名"/>
    <s:textfield name="users[1].pass" label="第二个密码"/>
    <tr>
        <td colspan="2"><s:submit value="转换" theme="simple"/>
        <s:reset value="重填" theme="simple"/></td>
    </tr>
</s:form>
</body>
</html>

2 welcome.jsp

<%@ 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/>
第一个User实例的用户名为:<s:property value="users[0].name"/><br/>
第一个User实例的密码为:<s:property value="users[0].pass"/><br/>
第二个User实例的用户名为:<s:property value="users[1].name"/><br/>
第二个User实例的密码为:<s:property value="users[1].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>

三 action

package org.crazyit.app.action;

import com.opensymphony.xwork2.ActionSupport;
import java.util.List;

import org.crazyit.app.domain.*;


public class LoginAction extends ActionSupport
{
    // Action类里包含一个不带泛型的List类型的成员变量
    private List users;

    // users的setter和getter方法
    public void setUsers(List users)
    {
        this.users = users;
    }
    public List getUsers()
    {
        return this.users;
    }

    public String execute() throws Exception
    {
        // 在控制台输出Struts 2封装产生的List对象
        System.out.println(getUsers());
        // 因为没有使用泛型,所以要进行强制类型转换
        User firstUser = (User)getUsers().get(0);
        // users属性的第一个User实例来决定控制逻辑
        if (firstUser.getName().equals("crazyit.org")
            && firstUser.getPass().equals("leegang") )
        {
            addActionMessage("登录成功!");
            return SUCCESS;
        }
        addActionMessage("登录失败!!");
        return ERROR;
    }
}

对应的属性文件LoginAction-conversion.properties

Element_users=org.crazyit.app.domain.User

四 领域模型

package org.crazyit.app.domain;

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;
    }
}

五 测试

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