Struts2登陆的Demo

原创 2015年07月08日 21:19:48

通过Struts2实现登陆的小案例
这里写图片描述

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

代码如下:

struts.xml

<code class="hljs xml has-numbering"><span class="hljs-pi"><?xml version="1.0" encoding="UTF-8" ?></span>
<span class="hljs-doctype"><!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd"></span>
<span class="hljs-tag"><<span class="hljs-title">struts</span>></span>

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

<span class="hljs-tag"></<span class="hljs-title">struts</span>></span>   </code>
<code class="hljs xml has-numbering"> </code>

 

LoginAction.java

<code class="hljs java has-numbering"><span class="hljs-keyword">package</span> com.bzu.gxs;

<span class="hljs-keyword">import</span> com.sun.net.httpserver.Authenticator.Success;

<span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">LoginAction</span> {</span>
    <span class="hljs-keyword">private</span> String uname;

    <span class="hljs-keyword">private</span> String upass;

    <span class="hljs-keyword">public</span> String <span class="hljs-title">getUname</span>() {
        <span class="hljs-keyword">return</span> uname;
    }

    <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">setUname</span>(String uname) {
        <span class="hljs-keyword">this</span>.uname = uname;
    }

    <span class="hljs-keyword">public</span> String <span class="hljs-title">getUpass</span>() {
        <span class="hljs-keyword">return</span> upass;
    }

    <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">setUpass</span>(String upass) {
        <span class="hljs-keyword">this</span>.upass = upass;
    }

    <span class="hljs-comment">// 默认执行action操作</span>
    <span class="hljs-keyword">public</span> String <span class="hljs-title">execute</span>(){

        <span class="hljs-keyword">if</span>(getUname().equals(<span class="hljs-string">"gxs"</span>) && getUpass().equals(<span class="hljs-string">"123"</span>)){
            <span class="hljs-keyword">return</span> <span class="hljs-string">"success"</span>;
        }<span class="hljs-keyword">else</span>{
            <span class="hljs-keyword">return</span> <span class="hljs-string">"error"</span>;
        }
    }


}
</code>

login.jsp

<code class="hljs xml has-numbering"><span class="vbscript"><%@ page language=<span class="hljs-string">"java"</span> import=<span class="hljs-string">"java.util.*"</span> pageEncoding=<span class="hljs-string">"gbk"</span>%></span>
<span class="vbscript"><%
<span class="hljs-built_in">String</span> path = <span class="hljs-built_in">request</span>.getContextPath();
<span class="hljs-built_in">String</span> basePath = <span class="hljs-built_in">request</span>.getScheme()+<span class="hljs-string">"://"</span>+<span class="hljs-built_in">request</span>.getServerName()+<span class="hljs-string">":"</span>+<span class="hljs-built_in">request</span>.getServerPort()+path+<span class="hljs-string">"/"</span>;
%></span>

<span class="hljs-doctype"><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"></span>
<span class="hljs-tag"><<span class="hljs-title">html</span>></span>
  <span class="hljs-tag"><<span class="hljs-title">head</span>></span>
    <span class="hljs-tag"><<span class="hljs-title">base</span> <span class="hljs-attribute">href</span>=<span class="hljs-value">"<%=basePath%>"</span>></span>

    <span class="hljs-tag"><<span class="hljs-title">title</span>></span>My JSP 'login.jsp' starting page<span class="hljs-tag"></<span class="hljs-title">title</span>></span>

    <span class="hljs-tag"><<span class="hljs-title">meta</span> <span class="hljs-attribute">http-equiv</span>=<span class="hljs-value">"pragma"</span> <span class="hljs-attribute">content</span>=<span class="hljs-value">"no-cache"</span>></span>
    <span class="hljs-tag"><<span class="hljs-title">meta</span> <span class="hljs-attribute">http-equiv</span>=<span class="hljs-value">"cache-control"</span> <span class="hljs-attribute">content</span>=<span class="hljs-value">"no-cache"</span>></span>
    <span class="hljs-tag"><<span class="hljs-title">meta</span> <span class="hljs-attribute">http-equiv</span>=<span class="hljs-value">"expires"</span> <span class="hljs-attribute">content</span>=<span class="hljs-value">"0"</span>></span>    
    <span class="hljs-tag"><<span class="hljs-title">meta</span> <span class="hljs-attribute">http-equiv</span>=<span class="hljs-value">"keywords"</span> <span class="hljs-attribute">content</span>=<span class="hljs-value">"keyword1,keyword2,keyword3"</span>></span>
    <span class="hljs-tag"><<span class="hljs-title">meta</span> <span class="hljs-attribute">http-equiv</span>=<span class="hljs-value">"description"</span> <span class="hljs-attribute">content</span>=<span class="hljs-value">"This is my page"</span>></span>

  <span class="hljs-tag"></<span class="hljs-title">head</span>></span>

  <span class="hljs-tag"><<span class="hljs-title">body</span>></span>
    <span class="hljs-tag"><<span class="hljs-title">form</span> <span class="hljs-attribute">name</span>=<span class="hljs-value">"frm"</span> <span class="hljs-attribute">action</span>=<span class="hljs-value">"login.action"</span> <span class="hljs-attribute">method</span>=<span class="hljs-value">"post"</span>></span>
        <span class="hljs-tag"><<span class="hljs-title">table</span> <span class="hljs-attribute">align</span>=<span class="hljs-value">"center"</span> <span class="hljs-attribute">border</span>=<span class="hljs-value">"1"</span>></span>

              <span class="hljs-tag"><<span class="hljs-title">tr</span>></span>
                <span class="hljs-tag"><<span class="hljs-title">td</span>></span>用户名:<span class="hljs-tag"></<span class="hljs-title">td</span>></span>
                <span class="hljs-tag"><<span class="hljs-title">td</span>></span><span class="hljs-tag"><<span class="hljs-title">input</span> <span class="hljs-attribute">type</span>=<span class="hljs-value">"text"</span> <span class="hljs-attribute">id</span>=<span class="hljs-value">"uname"</span> <span class="hljs-attribute">name</span>=<span class="hljs-value">"uname"</span>/></span><span class="hljs-tag"></<span class="hljs-title">td</span>></span>
              <span class="hljs-tag"></<span class="hljs-title">tr</span>></span>

              <span class="hljs-tag"><<span class="hljs-title">tr</span>></span>
                <span class="hljs-tag"><<span class="hljs-title">td</span>></span>密码:<span class="hljs-tag"></<span class="hljs-title">td</span>></span>
                <span class="hljs-tag"><<span class="hljs-title">td</span>></span><span class="hljs-tag"><<span class="hljs-title">input</span> <span class="hljs-attribute">type</span>=<span class="hljs-value">"password"</span> <span class="hljs-attribute">id</span>=<span class="hljs-value">"upass"</span> <span class="hljs-attribute">name</span>=<span class="hljs-value">"upass"</span>/></span><span class="hljs-tag"></<span class="hljs-title">td</span>></span>
              <span class="hljs-tag"></<span class="hljs-title">tr</span>></span>

              <span class="hljs-tag"><<span class="hljs-title">tr</span>></span>
                <span class="hljs-tag"><<span class="hljs-title">td</span> <span class="hljs-attribute">align</span>=<span class="hljs-value">"center"</span> <span class="hljs-attribute">colspan</span>=<span class="hljs-value">"2"</span>></span>
                <span class="hljs-tag"><<span class="hljs-title">input</span> <span class="hljs-attribute">type</span>=<span class="hljs-value">"submit"</span> <span class="hljs-attribute">value</span>=<span class="hljs-value">"登陆"</span> /></span>
                <span class="hljs-tag"><<span class="hljs-title">input</span> <span class="hljs-attribute">type</span>=<span class="hljs-value">"reset"</span>  <span class="hljs-attribute">value</span>=<span class="hljs-value">"重置"</span>></span>
                <span class="hljs-tag"></<span class="hljs-title">td</span>></span>
              <span class="hljs-tag"></<span class="hljs-title">tr</span>></span>

        <span class="hljs-tag"></<span class="hljs-title">table</span>></span>
    <span class="hljs-tag"></<span class="hljs-title">form</span>></span>
    <span class="hljs-tag"></<span class="hljs-title">br</span>></span>
    <span class="hljs-tag"><<span class="hljs-title">hr</span>></span>
    注意:<span class="hljs-tag"></<span class="hljs-title">br</span>></span>
    测试的用户名:gxs<span class="hljs-tag"></<span class="hljs-title">br</span>></span>
    测试的 密  码:123
  <span class="hljs-tag"></<span class="hljs-title">body</span>></span>
<span class="hljs-tag"></<span class="hljs-title">html</span>></span>
</code>
 

success.jsp

<code class="hljs xml has-numbering"><span class="vbscript"><%@ page language=<span class="hljs-string">"java"</span> import=<span class="hljs-string">"java.util.*"</span> pageEncoding=<span class="hljs-string">"gbk"</span>%></span>
<span class="vbscript"><%
<span class="hljs-built_in">String</span> path = <span class="hljs-built_in">request</span>.getContextPath();
<span class="hljs-built_in">String</span> basePath = <span class="hljs-built_in">request</span>.getScheme()+<span class="hljs-string">"://"</span>+<span class="hljs-built_in">request</span>.getServerName()+<span class="hljs-string">":"</span>+<span class="hljs-built_in">request</span>.getServerPort()+path+<span class="hljs-string">"/"</span>;
%></span>

<span class="hljs-doctype"><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"></span>
<span class="hljs-tag"><<span class="hljs-title">html</span>></span>
  <span class="hljs-tag"><<span class="hljs-title">head</span>></span>
    <span class="hljs-tag"><<span class="hljs-title">base</span> <span class="hljs-attribute">href</span>=<span class="hljs-value">"<%=basePath%>"</span>></span>

    <span class="hljs-tag"><<span class="hljs-title">title</span>></span>My JSP 'success.jsp' starting page<span class="hljs-tag"></<span class="hljs-title">title</span>></span>

    <span class="hljs-tag"><<span class="hljs-title">meta</span> <span class="hljs-attribute">http-equiv</span>=<span class="hljs-value">"pragma"</span> <span class="hljs-attribute">content</span>=<span class="hljs-value">"no-cache"</span>></span>
    <span class="hljs-tag"><<span class="hljs-title">meta</span> <span class="hljs-attribute">http-equiv</span>=<span class="hljs-value">"cache-control"</span> <span class="hljs-attribute">content</span>=<span class="hljs-value">"no-cache"</span>></span>
    <span class="hljs-tag"><<span class="hljs-title">meta</span> <span class="hljs-attribute">http-equiv</span>=<span class="hljs-value">"expires"</span> <span class="hljs-attribute">content</span>=<span class="hljs-value">"0"</span>></span>    
    <span class="hljs-tag"><<span class="hljs-title">meta</span> <span class="hljs-attribute">http-equiv</span>=<span class="hljs-value">"keywords"</span> <span class="hljs-attribute">content</span>=<span class="hljs-value">"keyword1,keyword2,keyword3"</span>></span>
    <span class="hljs-tag"><<span class="hljs-title">meta</span> <span class="hljs-attribute">http-equiv</span>=<span class="hljs-value">"description"</span> <span class="hljs-attribute">content</span>=<span class="hljs-value">"This is my page"</span>></span>

  <span class="hljs-tag"></<span class="hljs-title">head</span>></span>

  <span class="hljs-tag"><<span class="hljs-title">body</span>></span>
    <span class="hljs-tag"><<span class="hljs-title">h1</span>></span>查询成功!<span class="hljs-tag"></<span class="hljs-title">h1</span>></span>
  <span class="hljs-tag"></<span class="hljs-title">body</span>></span>

<span class="hljs-tag"></<span class="hljs-title">html</span>></span>
</code>

error.jsp

<code class="hljs xml has-numbering"><span class="vbscript"><%@ page language=<span class="hljs-string">"java"</span> import=<span class="hljs-string">"java.util.*"</span> pageEncoding=<span class="hljs-string">"gbk"</span>%></span>
<span class="vbscript"><%
<span class="hljs-built_in">String</span> path = <span class="hljs-built_in">request</span>.getContextPath();
<span class="hljs-built_in">String</span> basePath = <span class="hljs-built_in">request</span>.getScheme()+<span class="hljs-string">"://"</span>+<span class="hljs-built_in">request</span>.getServerName()+<span class="hljs-string">":"</span>+<span class="hljs-built_in">request</span>.getServerPort()+path+<span class="hljs-string">"/"</span>;
%></span>

<span class="hljs-doctype"><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"></span>
<span class="hljs-tag"><<span class="hljs-title">html</span>></span>
  <span class="hljs-tag"><<span class="hljs-title">head</span>></span>
    <span class="hljs-tag"><<span class="hljs-title">base</span> <span class="hljs-attribute">href</span>=<span class="hljs-value">"<%=basePath%>"</span>></span>

    <span class="hljs-tag"><<span class="hljs-title">title</span>></span>My JSP 'error.jsp' starting page<span class="hljs-tag"></<span class="hljs-title">title</span>></span>

    <span class="hljs-tag"><<span class="hljs-title">meta</span> <span class="hljs-attribute">http-equiv</span>=<span class="hljs-value">"pragma"</span> <span class="hljs-attribute">content</span>=<span class="hljs-value">"no-cache"</span>></span>
    <span class="hljs-tag"><<span class="hljs-title">meta</span> <span class="hljs-attribute">http-equiv</span>=<span class="hljs-value">"cache-control"</span> <span class="hljs-attribute">content</span>=<span class="hljs-value">"no-cache"</span>></span>
    <span class="hljs-tag"><<span class="hljs-title">meta</span> <span class="hljs-attribute">http-equiv</span>=<span class="hljs-value">"expires"</span> <span class="hljs-attribute">content</span>=<span class="hljs-value">"0"</span>></span>    
    <span class="hljs-tag"><<span class="hljs-title">meta</span> <span class="hljs-attribute">http-equiv</span>=<span class="hljs-value">"keywords"</span> <span class="hljs-attribute">content</span>=<span class="hljs-value">"keyword1,keyword2,keyword3"</span>></span>
    <span class="hljs-tag"><<span class="hljs-title">meta</span> <span class="hljs-attribute">http-equiv</span>=<span class="hljs-value">"description"</span> <span class="hljs-attribute">content</span>=<span class="hljs-value">"This is my page"</span>></span>

  <span class="hljs-tag"></<span class="hljs-title">head</span>></span>

  <span class="hljs-tag"><<span class="hljs-title">body</span>></span>
    登陆失败,请重新 <span class="hljs-tag"><<span class="hljs-title">a</span> <span class="hljs-attribute">href</span>=<span class="hljs-value">"login.jsp"</span>></span>登陆<span class="hljs-tag"></<span class="hljs-title">a</span>></span>
  <span class="hljs-tag"></<span class="hljs-title">body</span>></span>
<span class="hljs-tag"></<span class="hljs-title">html</span>></span>
</code>

Struts2实现登陆功能Demo

最近在学习SSH框架,通过用struts实现一个简单的登陆功能,来学习struts相关知识,着重了解了struts的配置。...
  • dutsoft
  • dutsoft
  • 2014年11月26日 12:00
  • 4117

struts2的环境配置与简单登录功能实现

struts2的环境配置与简单登录功能实现
  • qq_30258957
  • qq_30258957
  • 2016年12月19日 16:14
  • 605

一、Shiro+Spring+Struts2集成演示权限控制Demo示例

更好的理解shiro安全框架的好demo,不容错过~~
  • u010834071
  • u010834071
  • 2015年03月27日 11:16
  • 5828

Struts2实现登录流程

本节将演示一个基本的登录流程,在登录界面中若输入正确的用户名和密码,跳转到登录成功界面;否则路转到登录失败界面。   1 建立一个名为LoginDemo的动态Web项目   2 按照上节介绍的流程添加...
  • haishu_zheng
  • haishu_zheng
  • 2016年03月22日 23:07
  • 11820

struts2的登录和退出

freshly debugged source:http://blog.csdn.net/weiyanghuadi/article/details/9081879 SessionAware用的是依赖...
  • weiyanghuadi
  • weiyanghuadi
  • 2013年06月13日 05:56
  • 2421

struts2入门学习篇:实现一个简单的登录操作

其实struts2学习并不难,只是需要注意下细节,本文将实现一个基于struts2的简单的网页登录操作,先说一下流程吧: 1.配置struts,这是最重要的 2.写一个继承ActionSuppor...
  • chengxuzaza
  • chengxuzaza
  • 2017年03月10日 14:20
  • 1206

Struts2 +JDBC——简单登录

struts2+JDBC的简单登录注册
  • so_sss
  • so_sss
  • 2014年09月02日 14:22
  • 2521

Struts2自定义登录验证拦截器

Controller.UserAction.java package Controller; import java.util.Map; import com.opensymphony.xwor...
  • u012997311
  • u012997311
  • 2016年01月04日 14:35
  • 668

【Struts】Struts2简介及实现用户登录代码示例

Struts2是由传统的Struts1和WebWork两个经典的MVC框架发展起来的,无论是从Struts2设计的角度来看,还是从Struts2在实际项目中的易用性来看,Struts2都堪称是一个非常...
  • u010929604
  • u010929604
  • 2016年04月30日 15:53
  • 3131

Struts2 登陆验证、国际化显示Demo

项目结构如下如
  • zziamalei
  • zziamalei
  • 2014年06月05日 12:18
  • 457
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Struts2登陆的Demo
举报原因:
原因补充:

(最多只允许输入30个字)