Struts2登陆的Demo

通过Struts2实现登陆的小案例

源码下载地址:https://coding.net/u/gxs1225/p/Struts2_Login/git

代码如下:

struts.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>

   <package name="mypackage" namespace="" extends="struts-default">
      <action name="login" class="com.bzu.gxs.LoginAction">
         <result name="success">success.jsp</result>
         <result name="error">error.jsp</result>
      </action>
   </package>

</struts>    

LoginAction.java

package com.bzu.gxs;

import com.sun.net.httpserver.Authenticator.Success;

public class LoginAction {
    private String uname;

    private String upass;

    public String getUname() {
        return uname;
    }

    public void setUname(String uname) {
        this.uname = uname;
    }

    public String getUpass() {
        return upass;
    }

    public void setUpass(String upass) {
        this.upass = upass;
    }

    // 默认执行action操作
    public String execute(){

        if(getUname().equals("gxs") && getUpass().equals("123")){
            return "success";
        }else{
            return "error";
        }
    }


}

login.jsp

<%@ page language="java" import="java.util.*" pageEncoding="gbk"%>
<% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">

    <title>My JSP 'login.jsp' starting page</title>

    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">

  </head>

  <body>
    <form name="frm" action="login.action" method="post">
        <table align="center" border="1">

              <tr>
                <td>用户名:</td>
                <td><input type="text" id="uname" name="uname"/></td>
              </tr>

              <tr>
                <td>密码:</td>
                <td><input type="password" id="upass" name="upass"/></td>
              </tr>

              <tr>
                <td align="center" colspan="2">
                <input type="submit" value="登陆" />
                <input type="reset" value="重置">
                </td>
              </tr>

        </table>
    </form>
    </br>
    <hr>
    注意:</br>
    测试的用户名:gxs</br>
    测试的 密  码:123
  </body>
</html>

success.jsp

<%@ page language="java" import="java.util.*" pageEncoding="gbk"%>
<% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">

    <title>My JSP 'success.jsp' starting page</title>

    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">

  </head>

  <body>
    <h1>查询成功!</h1>
  </body>

</html>

error.jsp

<%@ page language="java" import="java.util.*" pageEncoding="gbk"%>
<% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">

    <title>My JSP 'error.jsp' starting page</title>

    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">

  </head>

  <body>
    登陆失败,请重新 <a href="login.jsp">登陆</a>
  </body>
</html>

你可能感兴趣的:(struts2,demo,login)