`
ningbingjian
  • 浏览: 9470 次
社区版块
存档分类
最新评论

01_struts简单的登录验证入门例子

 
阅读更多

    目标:登录验证,登录成功转向成功页面,可以进行用户添加操作,登录失败转向失败页面,未登录就输入添加用用户的连接则非法,强制跳转到登陆页面 

 

   1 . 首先添加struts2 jar

  

    commons-fileupload-1.2.1.jar

  

    commons-io-1.3.2.jar

 

    freemarker-2.3.16.jar

 

    javassist-3.7.ga.jar

 

   ognl-3.0.jar

 

   struts2-core-2.2.1.jar

 

   xwork-core-2.2.1.jar

 

  2 . 配置strtus的拦截器,在web.xml配置

 

  

<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>

 

  

   3.  编写登陆验证的Ation ,LoginAction.java

 

   

//实现modelDriven接口
public class LoginAction implements ModelDriven{
    
	private User user ;
	
	@Override
	public Object getModel() {
		if(user == null){
			user = new User();
		}
		return user ;
	}
    
	//账号密码判断
	public String  login(){
		
		//账号密码正确进入成功页面
		if("admin".equals(user.getUsername()) && "admin".equals(user.getPassword())){
			
			//把user放到session
			ActionContext.getContext().getSession().put("Login", user);
			
			return "success" ;
		}
		
		//否则进入失败页面
		return "fail";
	}
}

 

 

    4 . 编写模拟用户添加的action

     

       

public class UserAction {
    
	public String add(){
		
		return "add";
	}
}

 

  

  5 . 编写判断是否登陆的拦截器

 

  

//实现Interceptor接口
public class LoginInterceptor implements Interceptor{

	@Override
	public void destroy() {
		
		
	}

	@Override
	public void init() {
		
		
	}

	@Override
	public String intercept(ActionInvocation invocation) throws Exception {
		
		//如果从session取不到值的话,表示用户未登录,转到登陆页面
		if(invocation.getInvocationContext().getSession().get("Login")==null){
			
			return "index";
		}
		
		//继续往下执行
		return invocation.invoke();
	}

}

 

 

    6 . 配置struts.xml文件,分三部分贴

 

     (1) 配置登陆拦截器和默认使用的拦截器

 

 

<!-- 一系列的拦截器 -->
      <interceptors>
      
      	<!-- 登陆用到的拦截器 -->
      	<interceptor name="logininterceptor"
      		class="cn.com.action.LoginInterceptor">
      	</interceptor>
      	
      	<!-- 拦截栈 -->
      	<interceptor-stack name="needLoginStack">
      	
      		<interceptor-ref name="exception" />
      		
      		<interceptor-ref name="logininterceptor" />
      		
      		<interceptor-ref name="alias" />
      		
      		<interceptor-ref name="servletConfig" />
      		
      		<interceptor-ref name="i18n" />
      		
      		<interceptor-ref name="prepare" />
      		
      		<interceptor-ref name="chain" />
      		
      		<interceptor-ref name="debugging" />
      		
      		<interceptor-ref name="scopedModelDriven" />
      		
      		<interceptor-ref name="modelDriven">
      		
      			<param name="refreshModelBeforeResult">true</param>
      			
      		</interceptor-ref>
      		
      		<interceptor-ref name="fileUpload" />
      		
      		<interceptor-ref name="checkbox" />
      		
      		<interceptor-ref name="multiselect" />
      		
      		<interceptor-ref name="staticParams" />
      		
      		<interceptor-ref name="actionMappingParams" />
      		
      		<interceptor-ref name="params">
      		
      			<param name="excludeParams">
      				dojo\..*,^struts\..*
      			</param>
      			
      		</interceptor-ref>
      		
      		<interceptor-ref name="conversionError" />
      		
      		<interceptor-ref name="validation">
      		
      			<param name="excludeMethods">
      				input,back,cancel,browse
      			</param>
      			
      		</interceptor-ref>
      		
      		<interceptor-ref name="workflow">
      		
      			<param name="excludeMethods">
      				input,back,cancel,browse
      			</param>
      			
      		</interceptor-ref>
      		
      	</interceptor-stack>

      	<!-- The completeStack is here for backwards compatibility for
      		applications that still refer to the defaultStack by the
      		old name -->
      	<interceptor-stack name="completeStack">
      	
      		<interceptor-ref name="defaultStack" />
      		
      	</interceptor-stack>

      	<!-- Sample execute and wait stack.
      		Note: execAndWait should always be the *last* interceptor. -->
      	<interceptor-stack name="executeAndWaitStack">
      	
      		<interceptor-ref name="execAndWait">
      		
      			<param name="excludeMethods">input,back,cancel</param>
      			
      		</interceptor-ref>
      		
      		<interceptor-ref name="defaultStack" />
      		
      		<interceptor-ref name="execAndWait">
      		
      			<param name="excludeMethods">input,back,cancel</param>
      			
      		</interceptor-ref>

      	</interceptor-stack>

      	<!--  未配置登陆验证的拦截栈 -->
      	<interceptor-stack name="otherStack">
      	
      		<interceptor-ref name="exception" />

      		<interceptor-ref name="alias" />
      		
      		<interceptor-ref name="servletConfig" />
      		
      		<interceptor-ref name="i18n" />
      		
      		<interceptor-ref name="prepare" />
      		
      		<interceptor-ref name="chain" />
      		
      		<interceptor-ref name="debugging" />
      		
      		<interceptor-ref name="scopedModelDriven" />
      		
      		<interceptor-ref name="modelDriven">
      		
      			<param name="refreshModelBeforeResult">true</param>
      			
      		</interceptor-ref>
      		
      		<interceptor-ref name="fileUpload" />
      		
      		<interceptor-ref name="checkbox" />
      		
      		<interceptor-ref name="multiselect" />
      		
      		<interceptor-ref name="staticParams" />
      		
      		<interceptor-ref name="actionMappingParams" />
      		
      		<interceptor-ref name="params">
      		
      			<param name="excludeParams">
      				dojo\..*,^struts\..*
      			</param>
      			
      		</interceptor-ref>
      		
      		<interceptor-ref name="conversionError" />
      		
      		<interceptor-ref name="validation">
      		
      			<param name="excludeMethods">
      				input,back,cancel,browse
      			</param>
      			
      		</interceptor-ref>
      		
      		<interceptor-ref name="workflow">
      		
      			<param name="excludeMethods">
      				input,back,cancel,browse
      			</param>
      			
      		</interceptor-ref>
      		
      	</interceptor-stack>

      </interceptors>
      

    2 . 配置默认使用登陆拦截器

 

 

        <default-interceptor-ref name="needLoginStack"></default-interceptor-ref>
	   
	   <global-results>
	   
	      <result name="index">/index.jsp</result>
	      
	   </global-results>

 

 

 

3 .    配置LoginAction 和UserAction

 

  

<action name="login" class="cn.com.action.LoginAction">
	   
	       <result name="success">/success.jsp</result>
	       
	       <result name="fail">/fail.jsp</result>
	       
	       <!-- 很明显,登陆判断的Action并不需要判断是否登陆 -->
           <interceptor-ref name="otherStack"></interceptor-ref>
           
	   </action>
	   
	   <action name="user" class="cn.com.action.UserAction">
	     
	       <result name="add">/addSuccess.jsp</result>
	       
	       <!-- 除了登陆判断的Action,其他所有的Action都需要判断是否已经登陆,登陆方能操作 -->
	       <interceptor-ref name="needLoginStack"></interceptor-ref>
	       
	   </action>

      

 

    4 . struts.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>
    
      <package name="system" namespace="/system" extends="struts-default">

      <!-- 一系列的拦截器 -->
      <interceptors>
      
      	<!-- 登陆用到的拦截器 -->
      	<interceptor name="logininterceptor"
      		class="cn.com.action.LoginInterceptor">
      	</interceptor>
      	
      	<!-- 拦截栈 -->
      	<interceptor-stack name="needLoginStack">
      	
      		<interceptor-ref name="exception" />
      		
      		<interceptor-ref name="logininterceptor" />
      		
      		<interceptor-ref name="alias" />
      		
      		<interceptor-ref name="servletConfig" />
      		
      		<interceptor-ref name="i18n" />
      		
      		<interceptor-ref name="prepare" />
      		
      		<interceptor-ref name="chain" />
      		
      		<interceptor-ref name="debugging" />
      		
      		<interceptor-ref name="scopedModelDriven" />
      		
      		<interceptor-ref name="modelDriven">
      		
      			<param name="refreshModelBeforeResult">true</param>
      			
      		</interceptor-ref>
      		
      		<interceptor-ref name="fileUpload" />
      		
      		<interceptor-ref name="checkbox" />
      		
      		<interceptor-ref name="multiselect" />
      		
      		<interceptor-ref name="staticParams" />
      		
      		<interceptor-ref name="actionMappingParams" />
      		
      		<interceptor-ref name="params">
      		
      			<param name="excludeParams">
      				dojo\..*,^struts\..*
      			</param>
      			
      		</interceptor-ref>
      		
      		<interceptor-ref name="conversionError" />
      		
      		<interceptor-ref name="validation">
      		
      			<param name="excludeMethods">
      				input,back,cancel,browse
      			</param>
      			
      		</interceptor-ref>
      		
      		<interceptor-ref name="workflow">
      		
      			<param name="excludeMethods">
      				input,back,cancel,browse
      			</param>
      			
      		</interceptor-ref>
      		
      	</interceptor-stack>

      	<!-- The completeStack is here for backwards compatibility for
      		applications that still refer to the defaultStack by the
      		old name -->
      	<interceptor-stack name="completeStack">
      	
      		<interceptor-ref name="defaultStack" />
      		
      	</interceptor-stack>

      	<!-- Sample execute and wait stack.
      		Note: execAndWait should always be the *last* interceptor. -->
      	<interceptor-stack name="executeAndWaitStack">
      	
      		<interceptor-ref name="execAndWait">
      		
      			<param name="excludeMethods">input,back,cancel</param>
      			
      		</interceptor-ref>
      		
      		<interceptor-ref name="defaultStack" />
      		
      		<interceptor-ref name="execAndWait">
      		
      			<param name="excludeMethods">input,back,cancel</param>
      			
      		</interceptor-ref>

      	</interceptor-stack>

      	<!--  未配置登陆验证的拦截栈 -->
      	<interceptor-stack name="otherStack">
      	
      		<interceptor-ref name="exception" />

      		<interceptor-ref name="alias" />
      		
      		<interceptor-ref name="servletConfig" />
      		
      		<interceptor-ref name="i18n" />
      		
      		<interceptor-ref name="prepare" />
      		
      		<interceptor-ref name="chain" />
      		
      		<interceptor-ref name="debugging" />
      		
      		<interceptor-ref name="scopedModelDriven" />
      		
      		<interceptor-ref name="modelDriven">
      		
      			<param name="refreshModelBeforeResult">true</param>
      			
      		</interceptor-ref>
      		
      		<interceptor-ref name="fileUpload" />
      		
      		<interceptor-ref name="checkbox" />
      		
      		<interceptor-ref name="multiselect" />
      		
      		<interceptor-ref name="staticParams" />
      		
      		<interceptor-ref name="actionMappingParams" />
      		
      		<interceptor-ref name="params">
      		
      			<param name="excludeParams">
      				dojo\..*,^struts\..*
      			</param>
      			
      		</interceptor-ref>
      		
      		<interceptor-ref name="conversionError" />
      		
      		<interceptor-ref name="validation">
      		
      			<param name="excludeMethods">
      				input,back,cancel,browse
      			</param>
      			
      		</interceptor-ref>
      		
      		<interceptor-ref name="workflow">
      		
      			<param name="excludeMethods">
      				input,back,cancel,browse
      			</param>
      			
      		</interceptor-ref>
      		
      	</interceptor-stack>

      </interceptors>
      
      	   
	   
	   <default-interceptor-ref name="needLoginStack"></default-interceptor-ref>
	   
	   <global-results>
	   
	      <result name="index">/index.jsp</result>
	      
	   </global-results>

      <action name="login" class="cn.com.action.LoginAction">
	   
	       <result name="success">/success.jsp</result>
	       
	       <result name="fail">/fail.jsp</result>
	       
	       <!-- 很明显,登陆判断的Action并不需要判断是否登陆 -->
           <interceptor-ref name="otherStack"></interceptor-ref>
           
	   </action>
	   
	   <action name="user" class="cn.com.action.UserAction">
	     
	       <result name="add">/addSuccess.jsp</result>
	       
	       <!-- 除了登陆判断的Action,其他所有的Action都需要判断是否已经登陆,登陆方能操作 -->
	       <interceptor-ref name="needLoginStack"></interceptor-ref>
	       
	   </action>

      </package>
    </struts>

  

 6 .编写四个页面

     (1)登陆页面index.jsp

 

    

      <form action="system/login!login.action" method="post">
      
            用户名:<input type="text" name="username"><br>
           
            密码: <input type="password" name="password"><br>
           
           <input type="submit" value="登陆">
      
      </form>

 

(2) 登陆失败和成功页面

 

   success.jsp

      <h1> 登陆成功</h1><br>
       
       <h1><a href="system/user!add.action"> 添加用户</a></h1><br>

  fail.jsp

 

  

    <center>
       <h1>登陆失败</h1><br>
       
    </center>

   (3) 添加用户成功页面 addSuccess.jsp

 

  

    <center>
       <h1>添加一个用户成功</h1>
    </center>

 

 

   最后,启动服务器测试

 

    输入用户名和密码admin可以登陆,可以添加用户

分享到:
评论
1 楼 zsf0615020117 2011-07-13  
不错的文章 谢谢分享
Global site tag (gtag.js) - Google Analytics