Struts中action指定method属性实战

一 action

package org.crazyit.app.action;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ActionContext;


public class LoginRegistAction
    extends ActionSupport
{
    // 封装用户请求参数的两个成员变量
    private String username;
    private String password;
    // username的setter和getter方法
    public String getUsername()
    {
        return username;
    }
    public void setUsername(String username)
    {
        this.username = username;
    }
    // password的getter和setter方法
    public String getPassword()
    {
        return password;
    }
    public void setPassword(String password)
    {
        this.password = password;
    }
    // Action包含的注册控制逻辑
    public String regist() throws Exception
    {
        ActionContext.getContext().getSession()
            .put("user" , getUsername());
        addActionMessage("恭喜您," + getUsername() + ",您已经注册成功!");
        return SUCCESS;
    }
    // Action默认包含的控制逻辑
    public String execute() throws Exception
    {
        if (getUsername().equals("crazyit.org")
            && getPassword().equals("leegang") )
        {
            ActionContext.getContext().getSession()
                .put("user" , getUsername());
            addActionMessage("欢迎," + getUsername() + ",您已经登录成功!");
            return SUCCESS;
        }
        return ERROR;
    }
}

二 配置文件

<?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">
        <!-- 配置login Action,处理类为LoginRegistAction
            默认使用execute方法处理请求-->
        <action name="login" class="org.crazyit.app.action.LoginRegistAction">
            <!-- 定义逻辑视图和物理视图之间的映射关系 -->
            <result name="error">/WEB-INF/content/error.jsp</result>
            <result>/WEB-INF/content/welcome.jsp</result>
        </action>
        <!-- 配置regist Action,处理类为LoginRegistAction
            指定使用regist方法处理请求-->
        <action name="regist" class="org.crazyit.app.action.LoginRegistAction"
            method="regist">
            <!-- 定义逻辑视图和物理视图之间的映射关系 -->
            <result name="error">/WEB-INF/content/error.jsp</result>
            <result>/WEB-INF/content/welcome.jsp</result>
        </action>
        <action name="*">
            <result>/WEB-INF/content/{1}.jsp</result>
        </action>
    </package>
</struts>

三 视图

1 loginForm.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>
<table width="300" align="center">
<form action="login" method="post">
    <tr>
        <td>用户名:</td>
        <td><input type="text" name="username"/></td>
    </tr>
    <tr>
        <td>密&nbsp;&nbsp;码:</td>
        <td><input type="text" name="password"/></td>
    </tr>
    <tr>
        <td><input type="submit" value="登录"
            onclick="this.form.action='login';"/></td>
        <td><input type="submit" value="注册"
            onclick="regist();"/></td>
    </tr>
</form>
<table>
<script type="text/javascript">
function regist()
{
    // 获取页面的第一个表单
    targetForm = document.forms[0];
    // 动态修改表单的action属性
    targetForm.action = "regist";
}
</script>
</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/>
</body>
</html>

3 error.jsp

<%@ page contentType="text/html; charset=GBK" language="java" errorPage="" %>
<!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>
    对不起,您登录失败!
</body>
</html>

四 测试

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值