sturts2 須要加一個攔截器在action中作登陸驗證校驗,主要的思想是登陸後在session中存儲一個標誌,而後在過濾器中驗證這個標誌,若是有則經過驗證。若是沒有,則返回到登陸頁面。
過濾器代碼以下,我把標誌就設置爲username,須要修改的同窗本身更名字
package com.tc.blacktea.util;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.dispatcher.ServletRedirectResult;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.Interceptor;
public class LoginInterceptor implements Interceptor {
private static final long serialVersionUID = -2255797147687651066L;
private static final Log log = LogFactory.getLog(LoginInterceptor.class);
public String intercept(ActionInvocation invocation) throws Exception {
final ActionContext context = invocation.getInvocationContext();
HttpServletRequest request = (HttpServletRequest) context.get(ServletActionContext.HTTP_REQUEST);
HttpSession session = request.getSession();
String username=(String)session.getAttribute("username");
if (username == null) {
ServletRedirectResult result = new ServletRedirectResult();
result.setLocation("/toLogin.action");
try {
result.execute(invocation);
} catch (Exception ex) {
log.error("Unable to create debugging console", ex);
}
return Action.NONE;
}
return invocation.invoke();
}
public void destroy() {
}
public void init() {
}
}
在struts的總配置文件struts.xml裏增長對攔截器的引用
<package name="blakctea-default" extends="struts-default">
<interceptors>
<interceptor name="loginInterceptor" class="com.tc.blacktea.util.LoginInterceptor">
<param name="loginActionName">/login.action</param>
</interceptor>
</interceptors>
</package>
注意這個package的name,在須要引入這個攔截器的action的配置文件中,首先要extends這個package的name,而後在action中<interceptor-ref >引入攔截器
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<package name="advstoreclass" extends="blacktea-defalut" namespace="/advstoreclass">
<action name="storeclass" class="com.tc.blacktea.adv.action.StoreClassAction">
<!-- 定義轉發路徑對應的字符串名 -->
<result name="success">/jsp/storeclass/storeclasslist.jsp</result>
<result name="queryOne">/jsp/storeclass/storeclass.jsp</result>
<interceptor-ref name="loginInterceptor"></interceptor-ref>
</action>
</package>
</struts>
此時攔截器已經被引入,注意到咱們的攔截器類中
result.setLocation("/toLogin.action");
這句話,因此要有一個配置文件和類專門處理toLogin這個action
新建一個struts-login.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="toLogin" namespace="/" extends="struts-default">
<action name="toLogin" class="com.tc.blacktea.login.action.LoginAction" method="toLogin">
<result>/jsp/index.jsp</result>
</action>
</package>
</struts>
對應的class須要新建一個LoginAction來作跳轉
package com.tc.blacktea.login.action;
import com.tc.blacktea.util.BaseAction;
public class LoginAction extends BaseAction {
private static final long serialVersionUID = 5555663601912656942L;
public String toLogin(){
return SUCCESS;
}
}
大功告成。1.須要注意個人登陸頁面是在jsp/index.jsp下以此對應了sturts-login.xml。同時登陸成功後,要在session中放入username這個屬性。
Map session=(Map)ActionContext.getContext().get(ActionContext.SESSION);
session.put("username", username);