文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>实现login的两种方法

实现login的两种方法

时间:2010-06-28  来源:hkebao

实现login的两种方法

时间:2010-6-28

方式一:将VO的东西封装到Action里面

编写Action方法

package action;

 

import com.opensymphony.xwork2.ActionSupport;

 

public class LoginAction extends ActionSupport{

    private String username;

    private String password;

    public String getUsername() {

        return username;

    }

    public void setUsername(String username) {

        this.username = username;

    }

    public String getPassword() {

        return password;

    }

    public void setPassword(String password) {

        this.password = password;

    }

    @Override

    public String execute() throws Exception {

        if ("aaa".equalsIgnoreCase(username)&&"aaa".equalsIgnoreCase(password)) {

            return "loginSuc";

        }else {

            return "loginFail";

        }

    }

}

配置文件

<action name="mylogin" class="action.LoginAction">

            <result name="input">/mylogin.jsp</result>

            <result name="loginSuc">/ok.jsp</result>

            <result name="loginFail">/errok.jsp</result>

        </action>

然后编写登录的JSP页面

 

 

 

 

<s:form action="mylogin.action">

        <table width="30%" align="center">

            <tr>

                <td>

                <s:textfield key="username" label="USERNAME" />

                </td>

            </tr>

            <tr>

                <td>

                <s:password key="password" label="PASSWORD" />

                </td>

            </tr>

            <tr>

                <td>

                <s:submit value="SUBMIT" />

                </td>

            </tr>

        </table>

</s:form>

注意这里面的JSP里面的表单名称要与Action里面的名称保持一致的1

也可以将其抽出来/

定义一个VO对象

自定义vo文件名:LoginVO.java
文件内容:
package struts2.login;

public class LoginVO {
    private String username;
    private String password;
  
    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
  
}


在Action文件中,要使用这个vo
文件内容:
package struts2.login;

public class LoginAction {
    private LoginVO user = null;

    public String execute() {
        System.out.println (LoginAction.class.hashCode());
        if (user.getUsername().equalsIgnoreCase("aaa") &&
                user.getPassword().equals("aaaaaa")) {
            return "loginSuc";
        }
        else {
            return "loginFail";
        }
    }

    public LoginVO getUser() {
        return user;
    }

    public void setUser(LoginVO user) {
        this.user = user;
    }

}

登陆成功的文件如下:
<%@ page contentType="text/html; charset=gb2312" %>
<%@ taglib uri="/struts-tags" prefix="s"%>
<meta http-equiv="content-type" content="text/html;charset=gb2312">


欢迎您,<s:property name="user.username">登录成功。

注意login文件的部分也要进行修改
文件内容如下:
<meta http-equiv="content-type" content="text/html;charset=gb2312">
<title>login2</title>

<form action="login.action" method="post">
  username:<input type="input" name="user.username"><br>
  password:<input type="input" name="user.password"><br>
  <input type="submit" value="登录">
</form>

相关阅读 更多 +
排行榜 更多 +
电动火车模拟器手游

电动火车模拟器手游

赛车竞速 下载
俄罗斯汽车碰撞模拟器

俄罗斯汽车碰撞模拟器

赛车竞速 下载
碰撞测试模拟器2

碰撞测试模拟器2

赛车竞速 下载