struts2 제3회: 차단기

3664 단어 struts2
차단기를 실행하고 호출합니다.
==============다음은 LoginIntercepter.java 코드===============

package com;

import java.util.Map;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;

public class LoginIntercepter extends AbstractInterceptor {

	/**
	 *     :      ,action          。
	 *    xml        defaultStack
	 * 
	 */
	public String intercept(ActionInvocation arg0) throws Exception {
		System.out.println("     :LoginIntercepter");
		String requestUrl=ServletActionContext.getRequest().getRequestURI();
        System.out.println("url:"+requestUrl);
		Map session = arg0.getInvocationContext().getSession();
	     String loginId = (String) session.get("loginId");	     
	     if (!(requestUrl.contains("!doLogin")||loginId!=null)) {
	    //   ,      	
	    	return "login";
			
		}
		return arg0.invoke();
	}

}


==============다음은 LoginIntercepter.java 코드===============

	<package name="abc" extends="struts-default">
		<!--        demo -->
		<interceptors>
			<interceptor name="loginCheck" class="com.LoginIntercepter"></interceptor>
		</interceptors>
		<global-results>
			<result name="success">/success.jsp</result>
		</global-results>

		<!--       -->
		<action name="tagTest" class="com.StrutsTagTestAction">
		
			<result name="login">/login.jsp</result>
			<interceptor-ref name="loginCheck" /><!--         -->
			<interceptor-ref name="defaultStack" /><!--            ,           ,   action         -->
		</action>
</package>
</struts>

============login.jsp에서 코드=================

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
    
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body >
<form action="tagTest!doLogin.action" method="post">
<!--    ,       -->
userName:<input type="text" name="userName"><br/>
passWord:<input type="password" name="passWord" /><br/>

<input type="submit"e value="login">
<form>
</body>
</html>

===================action에서 얻은 코드===============================

	public String doLogin() {
        HttpServletRequest requst=ServletActionContext.getRequest();
        requst.getSession().setAttribute("loginId","abing");
        String content=requst.getParameter("content");
		System.out.println("userName:" + userName + "\tpassWord:" + passWord);
			
		return SUCCESS;
	}

이렇게 하면 간단한 로그인 검증 차단기를 실현하였다

좋은 웹페이지 즐겨찾기