日期:2014-05-18 浏览次数:20651 次
package com.zhang;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.sun.org.apache.xerces.internal.impl.Constants;
public class LoginAction extends ActionSupport
{
private String username;
private String password;
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";
}
}
<?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>
<constant name="struts.ui.theme" value="simple" />
<package name="user" extends="struts-default" namespace="/user">
<interceptors>
<interceptor name="my" class="com.zhang.LoginInterceptor"></interceptor>
</interceptors>
<action name="login" class="com.zhang.LoginAction">
<result>/success.jsp</result>
<result name="login">/login.jsp</result>
<interceptor-ref name="my"></interceptor-ref>
<interceptor-ref name="defaultStack"></interceptor-ref>
</action>
</package>
</struts>
import java.util.Map;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.Interceptor;
public class LoginInterceptor implements Interceptor
{
public void destroy()
{
}
public void init()
{
}
public String intercept(ActionInvocation invocation) throws Exception
{
ActionContext action = invocation.getInvocationContext();
String username = (String) action.get("username");//这里该如何获取login.jsp页面传过来的值?
if (null != username && username.equals("zhang"))
{
System.out.println("拦截器:合法用户登录---");
return invocation.invoke();// 继续面拦截器或action
}
else
{
System.out.println("拦截器:用户未登录---");
return "login";// 转登录页面
}
}
}
<interceptor-ref name="loginInterceptor">
<param name="username">user01</param>
<param name="password">123</param>
</interceptor-ref>