struts2登录验证
来源:互联网 发布:.shop域名价值 编辑:程序博客网 时间:2024/05/29 10:13
今天试着写了一下利用Struts2实现登陆验证的试验,挺有意思,要注意细节啊!!下面开始吧!
因为刚刚学习Struts2,所以有写的不到位的地方,希望大家指正,共同进步!也求大神能指导,谢谢了!
首先我将各个文件的代码先写上,最后再作详细解释吧。
1、下面的图是我的项目目录:
Struts2.xml的代码是:
<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"><struts><include file="struts-default.xml"/> <!-- <constant name="struts.devMode" value="true" /> --> <package name="Struts2" extends="struts-default"> <action name="userLog" class="com.struts2.LoginAction"> <result name="success">/welcome.jsp</result> <result name="error">/error.jsp</result> <result name="input">/index.jsp</result> </action> </package></struts>
web.xml中的代码是:
<?xml version="1.0" encoding="UTF-8"?><web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
下面是jsp中的代码,由于很多代码是自动生成的,所以我在这里就只写 body 体中的内容了
error.jsp代码是:
<body> 您输入的用户名或者密码名错误,请重新输入。。 <hr> <li> <a href="login.jsp">重新登录 </a> </li> <br> </body>
login.jsp中的内容:
<body> <form action="userLog!login2.action" method="post"> <table align="center"> <caption><h2>用户登录信息</h2></caption> <tr><td>用户名:<input type="text" name="name"></td></tr> <tr><td> 密 码:<input type="password" name="pass"></td></tr> <tr><td align="center" colspan="3"> <input type="submit" value="登录"> <input type="reset" value="重置"></td> </tr> </table> </form> </body>
regist.jsp代码是:
<body> <form action="userLog!login.action" method="post"> <table align="center"> <caption><h2>用户注册信息</h2></caption> <tr><td>用户名:<input type="text" name="name"></td></tr> <tr><td>密码:<input type="password" name="pass"></td></tr> <tr><td colspan="2"><input type="submit" value="注册"></td></tr> </table> </form> </body>
welcome.jsp代码
<html> <head> <title>登录成功界面</title> </head> 欢迎:${sessionScope.name } 您已经成功登录本系统! <hr> <li> <a href="login.jsp">重新登录 </a> </li> <br> <body> </body></html>
LoginAction.java的代码是:
package com.struts2;import com.opensymphony.xwork2.ActionContext;import com.opensymphony.xwork2.ActionSupport;public class LoginAction extends ActionSupport {private static final long serialVersionUID = 1L;private String name;public String getName() {return name;}public void setName(String name) {this.name = name;}public String getPass() {return pass;}public void setPass(String pass) {this.pass = pass;}public String getMsg() {return msg;}public void setMsg(String msg) {this.msg = msg;}private String pass;private String msg;//结果信息属性public String login2() throws Exception{if("admin".equals(getName())&&"admin".equals(getPass())){msg="登录成功,欢迎 : "+getName();ActionContext actionContext =ActionContext.getContext();//获取ActionContext实例,通过它访问 Servlet 的 APIif(null!=actionContext.getSession().get("name")){msg=getName()+" :已经登录本系统!";}else{actionContext.getSession().put("name",getName());}return this.SUCCESS;}else if(getName()==null||getPass()==null){return "input";}else{msg="登录失败,用户名或者密码错误!!";return this.ERROR;}}public String regist()throws Exception{msg="注册成功!";return this.SUCCESS;}public String execute(){return "success";}}
基本代码就是上面的内容,下面说说实现过程。。。。
当在浏览器中输入:http://localhost:8888/LoginForm/login.jsp的时候会出现:
当输入admin用户名和admin密码的时候,会将这个请求提交给tomcat服务器,tomcat服务器根据web.xml中的配置对请求进行拦截,其中的filter会对所有的请求进行拦截。在login.jsp中提交的Form的Action必须和在Struts.xml中命名的的Action相对应,也就是在login.jsp中action="userLog!login2.action",那么在Struts.xml中的action的nam属性必须是userLog,并且LoginAction.java中的方法名字必须是login(在我这个实例中是这样,其他配置会有差别,大同小异),这样你提交的才会找到对应的Action,
Struts.xml中的result通过返回的值决定返回给客户端所呈现的页面,这样请求页面和业务处理就分开了,扩展性增强,本例在浏览器中输入:http://localhost:8888/LoginForm/regist.jsp会呈现出注册界面,在这里不再扩展了。
1 0
- struts2登录验证码
- struts2登录验证
- struts2 拦截器 登录验证
- struts2 登录(无验证)
- struts2登录验证方法二
- Struts2+mysql登录验证实例
- struts2登录验证小程序
- Struts2拦截器登录验证
- Struts2拦截器登录验证
- Struts2拦截器登录验证
- struts2国际化及登录验证
- struts2整合Spring2.5验证登录
- 一个利用struts2实现的登录验证
- ExtJS4+JSON+Servlet/Struts2实现登录验证
- Extjs4---登录验证码的实现+struts2
- [Struts2应用开发] 统一的登录验证
- json+Struts2+jQuery的登录验证
- struts2 拦截器验证是否登录
- Javascript 实现鼠标移动到html元素出现提示框 转
- Json概述以及python对json的相关操作
- UVA 12295
- Android滑动菜单特效实现,仿人人客户端侧滑效果,史上最简单的侧滑实现
- hdu 3549 最大流
- struts2登录验证
- 关于坐标系和投影的相关知识探讨
- 中大-复试-2005-编程题一
- MSM平台上的AMSS .
- 回复贺老师(也写了点其他)
- Mysql命令行
- Ubuntu 13.04下构建Qt5开发环境
- 第九次作业--forEach
- struts2 第二讲