天天看点

Struts中action模式匹配实战1

涓€聽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鐨剆etter鍜実etter鏂规硶
    public String getUsername()
    {
        return username;
    }
    public void setUsername(String username)
    {
        this.username = username;
    }
    // password鐨刧etter鍜宻etter鏂规硶
    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 login() 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">
        <!-- 浣跨敤妯″紡瀛楃涓插畾涔堿ction鐨刵ame锛屾寚瀹氭墍鏈変互Action缁撳熬鐨勮姹傦紝
        閮藉彲鐢↙oginRegistAction鏉ュ鐞嗭紝method灞炴€т娇鐢▄1}锛?        杩欎釜{1}浠h〃杩涜妯″紡鍖归厤鏃剁涓€涓?鎵€浠f浛鐨勫瓧绗︿覆 -->
        <action name="*Action" class="org.crazyit.app.action.LoginRegistAction"
            method="{1}">
            <!-- 瀹氫箟閫昏緫瑙嗗浘鍜岀墿鐞嗚鍥句箣闂寸殑鏄犲皠鍏崇郴 -->
            <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="loginAction" method="post">
    <tr>
        <td>鐢ㄦ埛鍚?</td>
        <td><input type="text" name="username"/></td>
    </tr>
    <tr>
        <td>瀵?amp;nbsp;&nbsp;鐮?</td>
        <td><input type="text" name="password"/></td>
    </tr>
    <tr>
        <td><input type="submit" value="鐧诲綍"
            onclick="this.form.action='loginAction';"/></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 = "registAction";
}
</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>           

鍥浡犳祴璇?

Struts中action模式匹配实战1
Struts中action模式匹配实战1

继续阅读