struts2登录验证

2014-11-24 00:35:05 · 作者: · 浏览: 0

今天试着写了一下利用Struts2实现登陆验证的试验,挺有意思,要注意细节啊!!下面开始吧!
因为刚刚学习Struts2,所以有写的不到位的地方,希望大家指正,共同进步!也求大神能指导,谢谢了!

首先我将各个文件的代码先写上,最后再作详细解释吧。


1、下面的图是我的项目目录:


\

Struts2.xml的代码是:

  



  
	
   
    
   
    
    
     
     
      /welcome.jsp
      
     
      /error.jsp
      
     
      /index.jsp
      
     
   


  

web.xml中的代码是:

  

  
  
    
    
     index.jsp
     
   
  
  
    
    
     struts2
     
    
     org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
     
   

    
    
    
     struts2
     
    
     /*
     
   
    

  

下面是jsp中的代码,由于很多代码是自动生成的,所以我在这里就只写 body 体中的内容了

error.jsp代码是:


  	您输入的用户名或者密码名错误,请重新输入。。
  	
  
  • 重新登录


  • http://blog.csdn.net/majianjie/article/details/login.jsp中的内容:

      	
      

    用户登录信息

    用户名:
    密 码:

    regist.jsp代码是:

      	
      

    用户注册信息

    用户名:
    密码:

    welcome.jsp代码
      
        	登录成功界面
      
      		欢迎:${sessionScope.name }  您已经成功登录本系统!
      		
      
  • 重新登录


  • 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/http://blog.csdn.net/majianjie/article/details/login.jsp的时候会出现:
    \
    \
    当输入admin用户名和admin密码的时候,会将这个请求提交给tomcat服务器,tomcat服务器根据web.xml中的配置对请求进行拦截,其中的filter会对所有的请求进行拦截。在http://blog.csdn.net/majianjie/article/details/login.jsp中提交的Form的Action必须和在Struts.xml中命名的的Action相对应,也就是在http://blog.csdn.net/majianjie/article/details/login.jsp中action="userLog!login2.action",那么在Struts.xml中的action的nam属性必须是userLog,并且LoginAction.java中的方法名字必须是login(在我这个实例中是这样,其他配置会有差别,大同小异),这样你提交的才会找到对应的Action, Struts.xml中的result通过返回的值决定返回给客户端所呈现的页面,这样请求页面和业务处理就分开了,扩展性增强,本例在浏览器中输入:http://localhost:8888/LoginForm/regist.jsp会呈现出注册界面,在这里不再扩展了。