struts2登录验证

今天试着写了一下利用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 的 API
			if(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会呈现出注册界面,在这里不再扩展了。

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值