struts2練習:登陸驗證功能

分析:

需要自定義攔截器:除了登陸頁面外的,都需要已登陸狀態

1編寫攔截器

package cn.itheima.web.interceptor;

import java.util.Map;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.MethodFilterInterceptor;

public class LoginInterceptor extends MethodFilterInterceptor{

	//登陸攔截器、如果沒有登陸,重定向到登陸頁面
	
	protected String doIntercept(ActionInvocation invocation) throws Exception {
		//1獲取session
		//2獲取session中的User對象
		Object user = ActionContext.getContext().getSession().get("User");
		//3判斷User對象是否爲空,如果爲空即沒有登陸,如果不爲空,放心
		if(user==null) {
			return "toLogin";
		}else {
			return invocation.invoke();
		}
	}

}
2配置攔截器

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
	"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
	"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
	<!-- 指定struts2是否以開發模式運行
			1.熱加載主配置.(不需要重啓即可生效)
			2.提供更多錯誤信息輸出,方便開發時的調試
	 -->
	<constant name="struts.devMode" value="true"></constant>
	<package name="crm" namespace="/" extends="struts-default" >
	<!-- 配置自定義攔截器 -->
	<interceptors>
		<!-- 1註冊攔截器 -->
		<interceptor name="LoginInterceptor" class="cn.itheima.web.interceptor.LoginInterceptor"></interceptor>
		<!-- 2註冊攔截器棧 -->
		<interceptor-stack name="myStack">
			<interceptor-ref name="LoginInterceptor">
				<param name="excludeMethods">login</param>
			</interceptor-ref>
			<interceptor-ref name="defaultStack"></interceptor-ref>
		</interceptor-stack>
		</interceptors>
		<!-- 指定包中默認攔截器 -->
		<default-interceptor-ref name="myStack"></default-interceptor-ref>
		<!-- 定義全局結果集 -->
		<global-results>
			<result name="toLogin" type="redirect">/login.jsp</result>
		</global-results>
	
		<!-- 如果出現java.lang.RuntimeException異常,就將跳轉到名爲error的結果 -->
		<global-exception-mappings>
			<exception-mapping result="error" exception="java.lang.RuntimeException"></exception-mapping>
		</global-exception-mappings>
		
		<action name="CustomerAction_*" class="cn.itheima.web.action.CustomerAction" method="{1}" >
			<result name="list" >/jsp/customer/list.jsp</result>
			<result name="toList" type="redirectAction">
	             <param name="actionName">CustomerAction_list</param>
	             <param name="namespace">/</param>
	         </result>
		</action>
		<action name="UserAction_*" class="cn.itheima.web.action.UserAction" method="{1}" >
			<result name="toHome" type="redirect">/index.htm</result>
			<result name="error" >/login.jsp</result>
		</action>
	</package>
	
</struts>
3前端頁面完善

可以利用頁面加載事件,把框架中的login.jsp單獨顯示

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    <%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN" "http://www.w3c.org/TR/1999/REC-html401-19991224/frameset.dtd">
<HTML xmlns="http://www.w3.org/1999/xhtml">
<HEAD>
<META http-equiv=Content-Type content="text/html; charset=utf-8">
<STYLE type=text/css>
BODY {
	FONT-SIZE: 12px; COLOR: #ffffff; FONT-FAMILY: 宋體
}
TD {
	FONT-SIZE: 12px; COLOR: #ffffff; FONT-FAMILY: 宋體
}
</STYLE>

<META content="MSHTML 6.00.6000.16809" name=GENERATOR>
<script type="text/javascript">
	window.οnlοad=function(){
		
		if(window.parent != window){// 如果是在框架中
			//就讓框架頁面跳轉到登陸頁面
			window.parent.location.href = "${pageContext.request.contextPath}/login.jsp";
		}
		
	};
</script>

</HEAD>
<BODY>
<FORM id=form1 name=form1 action="${pageContext.request.contextPath}/UserAction_login"  method=post>

<DIV id=UpdatePanel1>
<DIV id=div1 
style="LEFT: 0px; POSITION: absolute; TOP: 0px; BACKGROUND-COLOR: #0066ff"></DIV>
<DIV id=div2 
style="LEFT: 0px; POSITION: absolute; TOP: 0px; BACKGROUND-COLOR: #0066ff"></DIV>


<DIV>   </DIV>
<DIV>
<TABLE cellSpacing=0 cellPadding=0 width=900 align=center border=0>
  <TBODY>
  <TR>
    <TD style="HEIGHT: 105px"><IMG src="images/login_1.gif" 
  border=0></TD></TR>
  <TR>
    <TD background=images/login_2.jpg height=300>
      <TABLE height=300 cellPadding=0 width=900 border=0>
        <TBODY>
        <TR>
          <TD colSpan=2 height=35></TD></TR>
        <TR>
          <TD width=360></TD>
          <TD>
            <TABLE cellSpacing=0 cellPadding=2 border=0>
              <TBODY>
              <TR>
                <TD style="HEIGHT: 28px" width=80>登 錄 名:</TD>
                <TD style="HEIGHT: 28px" width=150><INPUT id=txtName 
                  style="WIDTH: 130px" name="user_code"></TD>
                <TD style="HEIGHT: 28px" width=370><SPAN 
                  id=RequiredFieldValidator3 
                  style="FONT-WEIGHT: bold; VISIBILITY: hidden; COLOR: white">請輸入登錄名</SPAN></TD></TR>
              <TR>
                <TD style="HEIGHT: 28px">登錄密碼:</TD>
                <TD style="HEIGHT: 28px"><INPUT id=txtPwd style="WIDTH: 130px" 
                  type=password name="user_password"></TD>
                <TD style="HEIGHT: 28px"><SPAN id=RequiredFieldValidator4 
                  style="FONT-WEIGHT: bold; VISIBILITY: hidden; COLOR: white">請輸入密碼</SPAN></TD></TR>
              <TR>
                <TD style="HEIGHT: 28px">驗證碼:</TD>
                <TD style="HEIGHT: 28px"><INPUT id=txtcode 
                  style="WIDTH: 130px" name=txtcode></TD>
                <TD style="HEIGHT: 28px"> </TD></TR>
              <TR>
                <TD style="HEIGHT: 18px" colspan="2" ><font color="red" ><s:property value="exception.message" /> </font></TD>
                <TD style="HEIGHT: 18px"></TD></TR>
              <TR>
                <TD></TD>
                <TD><INPUT id=btn 
                  style="BORDER-TOP-WIDTH: 0px; BORDER-LEFT-WIDTH: 0px; BORDER-BOTTOM-WIDTH: 0px; BORDER-RIGHT-WIDTH: 0px" 
                  type=image src="images/login_button.gif" name=btn> 
              </TD></TR></TBODY></TABLE></TD></TR></TBODY></TABLE></TD></TR>
  <TR>
    <TD><IMG src="images/login_3.jpg" 
border=0></TD></TR></TBODY></TABLE></DIV></DIV>


</FORM>
<s:debug></s:debug>
</BODY></HTML>




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章