一、拦截器的使用:
只有用户登录时才可以查看商品,否则点击查看商品时会跳回login登录页面。
struts2.xml中拦截器Interceptor的配置:
<interceptors>
<!-- 声明自定义的拦截器 -->
<interceptor name="myInterceptor" class="Interceptor.MyInterceptor"></interceptor>
<!-- 声明拦截器栈,可以存放多个拦截器,这里存入自定义拦截器外加默认拦截器 -->
<interceptor-stack name="myInterceptorStack">
<interceptor-ref name="myInterceptor"></interceptor-ref>
<interceptor-ref name="defaultStack"></interceptor-ref>
</interceptor-stack>
</interceptors>
Action中对应方法的配置
<action name="product_show" class="Action.ProductAction" method="productShow" >
<result name="success">/product_show.jsp</result>
<result name="login">/login.jsp</result>
在action中引用拦截器
<interceptor-ref name="myInterceptorStack"></interceptor-ref>
</action>
LoginAction
//利用getModel模型获取数据
if("tom".equals(user.getUsername())&&"123".equals(user.getPassword())) {
//将用户存入session
request.getSession().setAttribute("user", user);
return "success";
} else {
return "fail";
}
MyInterceptor
public class MyInterceptor implements Interceptor {
@Override
public void destroy() {
// TODO Auto-generated method stub
}
@Override
public void init() {
// TODO Auto-generated method stub
}
@Override
public String intercept(ActionInvocation invocation) throws Exception {
User user = (User) ServletActionContext.getRequest().getSession().getAttribute("user");
if(user != null) {
//用户已经登录,直接放行
return invocation.invoke();
} else {
ProductAction productAction = (ProductAction) invocation.getAction();
productAction.addActionError("权限不足");
//没有登录,跳到登录界面
//要在showProduct这个Action中配置名称为login的result视图
return "login";
}
}
}
login.jsp
<body>
<!-- 可以显示vs.set("msg", "用户名或密码错误");的错误信息 -->
<s:property value="msg"/>
<!--可以显示productAction.addActionError("权限不足");的错误信息 -->
<s:actionerror/>
<hr>
<form action="${pageContext.request.contextPath }/login.action" method="post">
username:<input type="text" name="username"><br>
password:<input type="password" name="password"><br>
<input type="submit" value="登录">
</form>
</body>
success.jsp
<body>
<a href="${ pageContext.request.contextPath }/product_productShow">查看商品</a>
</body>
product_show.jsp
<body>
<table border="1">
<tr>
<td>商品名称</td>
<td>商品价格</td>
<td>商品数量</td>
</tr>
<s:iterator value="list">
<tr>
<td><s:property value="name"/></td>
<td><s:property value="price"/></td>
<td><s:property value="count"/></td>
</tr>
</s:iterator>
</table>
<s:debug></s:debug>
</body>
二、假设用通配符,Action中有许多方法,只要拦截其中一个时候的处理
假设现在ProductAction中有add()方法和productShow()方法,现在只要拦截productShow()方法。
拦截器配置
<interceptors>
<!-- 声明自定义的拦截器 -->
<interceptor name="myInterceptor" class="Interceptor.MyInterceptor">
<!-- 声明只拦截productShow方法 -->
<param name="includeMethods">productShow</param>
</interceptor>
<!-- 声明拦截器栈,可以存放多个拦截器,这里存入自定义拦截器外加默认拦截器 -->
<interceptor-stack name="myInterceptorStack">
<interceptor-ref name="myInterceptor"></interceptor-ref>
<interceptor-ref name="defaultStack"></interceptor-ref>
</interceptor-stack>
</interceptors>
对应Action的配置
<action name="product_*" class="Action.ProductAction" method="{1}" >
<result name="success">/product_show.jsp</result>
<result name="login">/login.jsp</result>
<!-- 在action中引用拦截器 -->
<interceptor-ref name="myInterceptorStack"></interceptor-ref>
</action>
这是success.jsp界面,product_* 对应下面的product_productShow, *对应productShow,即Action中的productShow方法。
<body>
<a href="${ pageContext.request.contextPath }/product_productShow">查看商品</a>
</body>
Interceptor(这边是继承MethodFilterInterceptor类,上边是实现Interceptor接口)
public class MyInterceptor extends MethodFilterInterceptor {
@Override
protected String doIntercept(ActionInvocation invocation) throws Exception {
System.out.println("Interceptor执行拦截...");
User user = (User) ServletActionContext.getRequest().getSession().getAttribute("user");
if(user != null) {
//用户已经登录,直接放行
return invocation.invoke();
} else {
ProductAction productAction = (ProductAction) invocation.getAction();
productAction.addActionError("权限不足");
//没有登录,跳到登录界面
return "login";
}
}
}