LoginAction
public class LoginAction extends ActionSupport implements SessionAware{
private String username;
private String password;
private Map session;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String execute(){
if(username.equals("zs")&&password.equals("123")){
session.put("username", username);
session.put("password", password);
return SUCCESS;
}
return ERROR;
}
public void setSession(Map<String, Object> arg0) {
this.session=arg0;
}
}
LoginBeforeAction
import com.opensymphony.xwork2.ActionSupport;
public class LoginBeforeAction extends ActionSupport{
public String execute(){
return SUCCESS;
}
}
LoginAfterAction
public class LoginAfterAction extends ActionSupport implements SessionAware{
private String username;
private String password;
private Map session;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String execute(){
return SUCCESS;
}
public void setSession(Map<String, Object> arg0) {
this.session=arg0;
}
}
LoginIntecepter
public class LoginIntecepter extends AbstractInterceptor{
public String intercept(ActionInvocation invocation) throws Exception {
Map session=(Map)invocation.getInvocationContext().getSession();
Object object=session.get("username");
if(object!=null){
return invocation.invoke();
}
return "error";
}
}
<struts>
<package name="login" namespace="/login" extends="struts-default">
<interceptors>
<interceptor name="loginIntercepter" class="com.login.actions.LoginIntecepter"></interceptor>
<interceptor-stack name="mystack">
<interceptor-ref name="defaultStack"></interceptor-ref>
<interceptor-ref name="loginIntercepter"></interceptor-ref>
</interceptor-stack>
</interceptors>
<action name="before" class="com.login.actions.LoginBeforeAction">
<result>/login.jsp</result>
</action>
<action name="login" class="com.login.actions.LoginAction">
<result>/main.jsp</result>
<result name="error">/login.jsp</result>
</action>
<action name="after" class="com.login.actions.LoginAfterAction">
<result>/main.jsp</result>
<result name="error">/login.jsp</result>
<interceptor-ref name="mystack"></interceptor-ref>
</action>
</package>
</struts>
<form action="login/login.do" method="post">
username:<input type="text" name="username"/><p>
password:<input type="password" name="password"/><p>
<input type="submit" value="submit"/>
</form>