import java.util.Map;
import org.apache.struts2.interceptor.SessionAware;
import com.opensymphony.xwork2.ActionSupport;
public class Login extends ActionSupport implements SessionAware {
private String message;
private Map mySession;
public void setSession(Map<String, Object> mySession) {
this.mySession = mySession;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public String execute() {
if ((message == null) || message.isEmpty()) {
addActionError("請輸入信息後提交!");
return INPUT;
} else {
addActionMessage("您輸入的信息是:" + message);
mySession.put("loginFlag", "login");
return SUCCESS;
}
}
}
-----------------------------------------------------------------------------------------------------------------------------
2、創建攔截器:LoginStatus.java
package com.Myfirststruts.action;
import java.util.Map;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
public class LoginStatus extends AbstractInterceptor {
@Override
public String intercept(ActionInvocation invocation) throws Exception {
Object action = invocation.getAction();
if(action instanceof Login){
return invocation.invoke();
}
Map session = invocation.getInvocationContext().getSession();
String login = (String)session.get("loginFlag");
if(login != null && login.equals("login")){
return invocation.invoke();
}
else {
((ActionSupport)action).addActionMessage("來自攔截器的消息:您還沒有登錄。請先登錄!");
return "input";
}
}
}
-------------------------------------------------------------------------------------------------------------------------------------------------------------------
3、在struts.xml註冊並使用攔截器:
<package name="default" namespace="/" extends="struts-default"> <!-- 加上namespace="/" 重要-->
<interceptors>
<interceptor name="loginStatus" class="com.Myfirststruts.action.LoginStatus" />
</interceptors>
<action name="login" class="com.Myfirststruts.action.Login">
<result >/login.jsp</result>
<result name="input">/login.jsp</result>
<interceptor-ref name="defaultStack" />
<interceptor-ref name="loginStatus" />
</action>
<action name="others">
<result>/others.jsp</result>
<result name="input">/login.jsp</result>
<interceptor-ref name="defaultStack" />
<interceptor-ref name="loginStatus" />
</action>
</package>
---------------------------------------------------------------------------------------------------------------------------------------------------------------------
4、 創建jsp頁面:
login.jsp
<%@ page language="java" import="java.util.*" contentType="text/html; charset=utf-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<s:actionmessage/>
<s:actionerror/>
<s:form action="login">
<s:textfield name="message" label="Message" />
<s:submit name="submit" />
</s:form>
<s:a action="others">單擊此處,訪問others Action!</s:a>
---------------------------------------------------
others.jsp
<%@ page language="java" import="java.util.*" contentType="text/html; charset=utf-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
到達此頁面,表示您是在輸入信息後進行的提交。<br>
<s:a href="login.jsp">返回到輸入頁面!</s:a>
----------------------------------------------------------------------------------------------------------
5、最後最重要一步就是:
發佈web項目,重新啓動tomcat服務器,訪問login.jsp時不要在myeclipse的自帶測試器上測試,謹記!!!
======================================================end