码迷,mamicode.com
首页 > 其他好文 > 详细

Struts2中的登录

时间:2016-03-29 23:50:19      阅读:160      评论:0      收藏:0      [点我收藏+]

标签:

1.新建一个项目

2.建立一个struts.xml

在struts.xml中写

<struts>
<package name="default" extends="struts-default">
        <!-- 用户登录 -->
        <action name="userlogin" class="struts.UserAction">
             <result name="success">/main.jsp</result>            
             <result name="error">/error.jsp</result>
        </action>
    </package>
    <!-- <constant name="struts.i18n.encoding" value="gb2312"></constant> -->
</struts>

便可执行跳转功能

3.在src中写后台代码

public class UserAction extends ActionSupport {
    @Override
    public String execute() throws Exception {
        if(userID.equals("aaa") && password.equals("123"))
            return "success";
        else return "error";
        
    }
    private String userID,password;
    public String getUserID() {
    return userID;
   }
    public void setUserID(String userID) {
      this.userID = userID;
   }
    public String getPassword() {
      return password;
  }
    public void setPassword(String password) {
    this.password = password;
  }
    

}

4.在jsp中写登录界面

<html>
  <head>  
    <title>用户登录</title>
  </head>
  <body>
  <form action="userlogin.action" method="post">
  <input type="text" name="userID"><br>
  <input type="text" name="password"><br>
  <input type="submit" value="登录">
  </form>
    </body>
</html>

当你输入的userID为aaa,password为123时显示登录成功

 <body>
  登录成功,欢迎<s:property value="userID"/>进入主页
  </body>

否则则失败

<body>
    登录失败
  </body>

5.要懂得学会运用struts

Struts2中的登录

标签:

原文地址:http://www.cnblogs.com/yumofei/p/5335110.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!