基于Struts2.1 annotation的Interceptor代码实例 收藏
查阅了网络上的文章,同时结合自己的实践,将今天的学习结果分享如下,欢迎大家拍砖。
/WEB-INF/lib/下包含以下jar包(但不只这两个)
struts2-core-2.1.6.jar
struts2-convention-plugin-2.1.6.jar
Interceptor代码如下:
view plaincopy to clipboardprint?
package com.kompin.kind.action;
import java.util.Map;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
public class AuthorityInterceptor extends AbstractInterceptor {
@Override
public String intercept(ActionInvocation invocation) throws Exception {
System.out.println("Enter AuthorityInterceptor");
Map session = ActionContext.getContext().getSession();
Object userId = session.get("userId");
if (userId == null)
{
return "login";
}
else
{
return invocation.invoke();
}
}
}
package com.kompin.kind.action;
import java.util.Map;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
public class AuthorityInterceptor extends AbstractInterceptor {
@Override
public String intercept(ActionInvocation invocation) throws Exception {
System.out.println("Enter AuthorityInterceptor");
Map session = ActionContext.getContext().getSession();
Object userId = session.get("userId");
if (userId == null)
{
return "login";
}
else
{
return invocation.invoke();
}
}
}
struts.xml文件配置如下:
view plaincopy to clipboardprint?
<struts>
<constant name="struts.118n.encoding" value="GBK"/>
<constant name="struts.custom.i18n.resources" value="globalMessages"/>
<constant name="struts.convention.action.mapAllMatches" value="true"/>
<package name="kompinInterceptor" extends="struts-default">
<interceptors>
<interceptor name="authority"
class="com.kompin.kind.action.interceptor.AuthorityInterceptor"/>
<interceptor-stack name="auctionStack">
<interceptor-ref name="defaultStack"/>
<interceptor-ref name="authority"/>
</interceptor-stack>
</interceptors>
<default-interceptor-ref name="auctionStack"/>
<global-results>
<result name="login">/WEB-INF/jsp/login.jsp</result>
<result name="exception">/WEB-INF/jsp/error.jsp</result>
</global-results>
</package>
</struts>
<struts>
<constant name="struts.118n.encoding" value="GBK"/>
<constant name="struts.custom.i18n.resources" value="globalMessages"/>
<constant name="struts.convention.action.mapAllMatches" value="true"/>
<package name="kompinInterceptor" extends="struts-default">
<interceptors>
<interceptor name="authority"
class="com.kompin.kind.action.interceptor.AuthorityInterceptor"/>
<interceptor-stack name="auctionStack">
<interceptor-ref name="defaultStack"/>
<interceptor-ref name="authority"/>
</interceptor-stack>
</interceptors>
<default-interceptor-ref name="auctionStack"/>
<global-results>
<result name="login">/WEB-INF/jsp/login.jsp</result>
<result name="exception">/WEB-INF/jsp/error.jsp</result>
</global-results>
</package>
</struts>
User action代码如下:
view plaincopy to clipboardprint?
package com.kompin.kind.action;
import java.util.List;
import java.util.Map;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.InterceptorRefs;
import org.apache.struts2.convention.annotation.InterceptorRef;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import com.kompin.kind.business.UserBean;
import com.kompin.kind.service.UserService;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
@ParentPackage("kompinInterceptor")
@InterceptorRefs({
@InterceptorRef("defaultStack")
})
@Results({
@Result(name="login", location="/WEB-INF/jsp/login.jsp")
})
public class UserAction extends ActionSupport {
private Integer id;
private String name;
private String pass;
private String email;
private UserService userSvc;
private List<UserBean> users;
private UserBean user;
@Action(value="/regist",
results={
@Result(name="success", type="dispatcher", location="/WEB-INF/jsp/login.jsp")
,@Result(name="failure", type="dispatcher", location="/WEB-INF/jsp/failure.jsp")
,@Result(name="input", type="dispatcher", location="/WEB-INF/jsp/regist.jsp")
}
)
public String regist()throws Exception{
try {
if(null == getName() ||null == getPass() || null == getEmail()){
return "input";
}
if (userSvc.addUser(getName(), getPass(), getEmail()) > 0) {
return "success";
}
return "failure";
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
throw new Exception("注册用户出现异常");
}
}
@Action(value="/proShowUser",
interceptorRefs=@InterceptorRef("authority"),
results={
@Result(name="success", type="dispatcher", location="/WEB-INF/jsp/viewUser.jsp")
,@Result(name="failure", type="dispatcher", location="/WEB-INF/jsp/failure.jsp")
,@Result(name="input", type="dispatcher", location="/WEB-INF/jsp/login.jsp")
}
)
public String show()throws Exception{
try {
setUsers(userSvc.findAllUser());
return "success";
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
throw new Exception("显示用户出现异常");
}
}
...略...
}
package com.kompin.kind.action;
import java.util.List;
import java.util.Map;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.InterceptorRefs;
import org.apache.struts2.convention.annotation.InterceptorRef;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import com.kompin.kind.business.UserBean;
import com.kompin.kind.service.UserService;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
@ParentPackage("kompinInterceptor")
@InterceptorRefs({
@InterceptorRef("defaultStack")
})
@Results({
@Result(name="login", location="/WEB-INF/jsp/login.jsp")
})
public class UserAction extends ActionSupport {
private Integer id;
private String name;
private String pass;
private String email;
private UserService userSvc;
private List<UserBean> users;
private UserBean user;
@Action(value="/regist",
results={
@Result(name="success", type="dispatcher", location="/WEB-INF/jsp/login.jsp")
,@Result(name="failure", type="dispatcher", location="/WEB-INF/jsp/failure.jsp")
,@Result(name="input", type="dispatcher", location="/WEB-INF/jsp/regist.jsp")
}
)
public String regist()throws Exception{
try {
if(null == getName() ||null == getPass() || null == getEmail()){
return "input";
}
if (userSvc.addUser(getName(), getPass(), getEmail()) > 0) {
return "success";
}
return "failure";
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
throw new Exception("注册用户出现异常");
}
}
@Action(value="/proShowUser",
interceptorRefs=@InterceptorRef("authority"),
results={
@Result(name="success", type="dispatcher", location="/WEB-INF/jsp/viewUser.jsp")
,@Result(name="failure", type="dispatcher", location="/WEB-INF/jsp/failure.jsp")
,@Result(name="input", type="dispatcher", location="/WEB-INF/jsp/login.jsp")
}
)
public String show()throws Exception{
try {
setUsers(userSvc.findAllUser());
return "success";
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
throw new Exception("显示用户出现异常");
}
}
...略...
}
Kind action代码如下:
view plaincopy to clipboardprint?
package com.kompin.kind.action;
import java.util.List;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.InterceptorRef;
import org.apache.struts2.convention.annotation.InterceptorRefs;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import com.kompin.kind.business.KindBean;
import com.kompin.kind.service.KindService;
import com.opensymphony.xwork2.ActionSupport;
@ParentPackage("kompinInterceptor")
@InterceptorRefs({
@InterceptorRef("auctionStack")
})
@Results({
@Result(name="login", location="/WEB-INF/jsp/login.jsp")
})
public class KindAction extends ActionSupport {
private KindService kindSvc;
private List kinds;
private String kindName;
private String kindDesc;
private KindBean kind;
@Action(value="/proAddKind",
results={
@Result(name="success", type="chain", location="proViewKind")
,@Result(name="failure", type="dispatcher", location="/WEB-INF/jsp/addKind.jsp")
,@Result(name="input", type="dispatcher", location="/WEB-INF/jsp/addKind.jsp")
}
)
public String addKind()throws Exception{
try {
if (kindSvc.addKind(kindName, kindDesc) > 0) {
return "success";
}
return "input";
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
return "input";
}
}
...略...
}
package com.kompin.kind.action;
import java.util.List;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.InterceptorRef;
import org.apache.struts2.convention.annotation.InterceptorRefs;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import com.kompin.kind.business.KindBean;
import com.kompin.kind.service.KindService;
import com.opensymphony.xwork2.ActionSupport;
@ParentPackage("kompinInterceptor")
@InterceptorRefs({
@InterceptorRef("auctionStack")
})
@Results({
@Result(name="login", location="/WEB-INF/jsp/login.jsp")
})
public class KindAction extends ActionSupport {
private KindService kindSvc;
private List kinds;
private String kindName;
private String kindDesc;
private KindBean kind;
@Action(value="/proAddKind",
results={
@Result(name="success", type="chain", location="proViewKind")
,@Result(name="failure", type="dispatcher", location="/WEB-INF/jsp/addKind.jsp")
,@Result(name="input", type="dispatcher", location="/WEB-INF/jsp/addKind.jsp")
}
)
public String addKind()throws Exception{
try {
if (kindSvc.addKind(kindName, kindDesc) > 0) {
return "success";
}
return "input";
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
return "input";
}
}
...略...
}
要点说明:
1. @ParentPackage("kompinInterceptor")必须和struts.xml的package的name一致,否则启动tomcat会报错:
"Unable to find interceptor class referenced by ref-name ..."
2. 如果Action中并不是所有的方法都要auctionStack拦截,参考UserAction的代码:
view plaincopy to clipboardprint?
@InterceptorRefs({
@InterceptorRef("defaultStack")
})
@InterceptorRefs({
@InterceptorRef("defaultStack")
})
3. 如果Action中所有的方法都经过同一个拦截器拦截,,参考KindAction的代码:
view plaincopy to clipboardprint?
@InterceptorRefs({
@InterceptorRef("auctionStack")
})
@InterceptorRefs({
@InterceptorRef("auctionStack")
})
4. 如果Action中的某些方法需要特定的拦截器拦截,参考UserAction的代码(请留意proShowUser的Action注解与regist的Action注解的不同点):
view plaincopy to clipboardprint?
@Action(value="/proShowUser",
interceptorRefs=@InterceptorRef("authority"),
results={
@Result(name="success", type="dispatcher", location="/WEB-INF/jsp/viewUser.jsp")
,@Result(name="failure", type="dispatcher", location="/WEB-INF/jsp/failure.jsp")
,@Result(name="input", type="dispatcher", location="/WEB-INF/jsp/login.jsp")
}
)
@Action(value="/proShowUser",
interceptorRefs=@InterceptorRef("authority"),
results={
@Result(name="success", type="dispatcher", location="/WEB-INF/jsp/viewUser.jsp")
,@Result(name="failure", type="dispatcher", location="/WEB-INF/jsp/failure.jsp")
,@Result(name="input", type="dispatcher", location="/WEB-INF/jsp/login.jsp")
}
)
5. 留意<interceptor-stack name="auctionStack">的定义。