package com.itheima.interceptor;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
public class InterceptorDemo1 extends AbstractInterceptor {
//动作的每次访问都会调用该方法
public String intercept(ActionInvocation invocation) throws Exception {
System.out.println("拦截前Demo1");
String rtvalue = invocation.invoke();//放行,这里为什么返回string?
因为最终的结果返回的Action的Result,而action的结果是string类型
System.out.println("拦截后Demo1");
return rtvalue;
}
}
package com.itheima.interceptor;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
import com.opensymphony.xwork2.interceptor.PreResultListener;
public class InterceptorDemo2 extends AbstractInterceptor {
//动作的每次访问都会调用该方法
public String intercept(ActionInvocation invocation) throws Exception {
// invocation.addPreResultListener(new PreResultListener() {
//
// public void beforeResult(ActionInvocation invocation, String resultCode) {
// System.out.println("结果显示前");
// }
// });
System.out.println("拦截前Demo2");
String rtvalue = invocation.invoke();//放行
System.out.println("拦截后Demo2");
return rtvalue;
}
}
<package name="p1" extends="struts-default"> <!-- 定义拦截器:只对当前包有效 --> <interceptors> <interceptor name="interceprotDemo1" class="com.itheima.interceptor.InterceptorDemo1"></interceptor> <interceptor name="interceprotDemo2" class="com.itheima.interceptor.InterceptorDemo2"></interceptor> </interceptors> </package>
<action name="action1" class="com.itheima.action.Demo1Action" method="execute"> <!-- 使用定义的拦截器。如过没有指定任何的拦截器,默认使用default-stack栈中的所有拦截器; 一旦指定了任何一个拦截器,默认的就无效了 --> <interceptor-ref name="interceprotDemo1"></interceptor-ref> <interceptor-ref name="interceprotDemo2"></interceptor-ref> <result>/success.jsp</result> </action>
package com.itheima.action;
import com.opensymphony.xwork2.ActionSupport;
public class Demo1Action extends ActionSupport {
@Override
public String execute() throws Exception {
System.out.println("execute执行了");
return SUCCESS;
}
}
<interceptors>
<interceptor name="interceprotDemo1" class="com.itheima.interceptor.InterceptorDemo1"></interceptor>
<interceptor name="interceprotDemo2" class="com.itheima.interceptor.InterceptorDemo2"></interceptor>
<interceptor-stack name="mydefaultStack">
<interceptor-ref name="defaultStack"></interceptor-ref>
<interceptor-ref name="interceprotDemo1"></interceptor-ref>
<interceptor-ref name="interceprotDemo2"></interceptor-ref>
</interceptor-stack>
</interceptors>
<action name="action3" class="com.itheima.action.LoginAction" method="login">
<interceptor-ref name="mydefaultStack"></interceptor-ref>
<result>/success.jsp</result>
</action>
<body>
<form action="${pageContext.request.contextPath}/login.action" method="post">
<input type="text" name="username"/><br/>
<input type="text" name="password"/><br/>
<input type="submit" value="登录"/>
</form>
</body>
package com.itheima.interceptor;
import javax.servlet.http.HttpSession;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
public class LoginCheckInterceptor extends AbstractInterceptor {
public String intercept(ActionInvocation invocation) throws Exception {
HttpSession session = ServletActionContext.getRequest().getSession();//通过ServletActionContext对象获得session对象
Object user = session.getAttribute("user");
if(user==null){
//没有登录
return "login";//返回到某个逻辑视图
}
return invocation.invoke();//放行
}
}
<package name="p2" extends="struts-default">
<interceptors>
<interceptor name="loginCheckInterceptor" class="com.itheima.interceptor.LoginCheckInterceptor"></interceptor>
<interceptor-stack name="mydefaultStack">
<interceptor-ref name="defaultStack"></interceptor-ref>
<interceptor-ref name="loginCheckInterceptor"></interceptor-ref>
</interceptor-stack>
</interceptors>
<action name="login" class="com.itheima.action.CustomerAction" method="login">
<result>/login.jsp</result>
</action>
</package>
package com.itheima.action;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class CustomerAction extends ActionSupport {
public String login(){
System.out.println("登录");
ServletActionContext.getRequest().getSession().setAttribute("user", "ppp");
return SUCCESS;
}
}
package com.itheima.interceptor;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
public class TimerInterceptor extends AbstractInterceptor {
public String intercept(ActionInvocation invocation) throws Exception {
long time = System.nanoTime();
String rtvalue = invocation.invoke();
System.out.println(rtvalue+"执行耗时:"+(System.nanoTime()-time)+"纳秒");
return rtvalue;
}
}
<package name="p2" extends="struts-default">
<interceptors>
<interceptor name="loginCheckInterceptor" class="com.itheima.interceptor.LoginCheckInterceptor"></interceptor>
<interceptor name="timerInterceptor" class="com.itheima.interceptor.TimerInterceptor"></interceptor>
<interceptor-stack name="mydefaultStack">
<interceptor-ref name="defaultStack"></interceptor-ref>
<interceptor-ref name="loginCheckInterceptor"></interceptor-ref>
<interceptor-ref name="timerInterceptor"></interceptor-ref>
</interceptor-stack>
</interceptors>
<result name="login">/login.jsp</result>
</action>
</package>
package com.itheima.interceptor;
import javax.servlet.http.HttpSession;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
import com.opensymphony.xwork2.interceptor.MethodFilterInterceptor;
public class LoginCheckInterceptor extends MethodFilterInterceptor {
protected String doIntercept(ActionInvocation invocation) throws Exception {
HttpSession session = ServletActionContext.getRequest().getSession();
Object user = session.getAttribute("user");
if(user==null){
//没有登录
return "login";//返回到某个逻辑视图
}
return invocation.invoke();//放行
}
}
package com.itheima.action;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class CustomerAction extends ActionSupport {
public String add(){
System.out.println("调用add的service方法");
return SUCCESS;
}
public String edit(){
System.out.println("调用edit的service方法");
return SUCCESS;
}
public String login(){
System.out.println("登录");
ServletActionContext.getRequest().getSession().setAttribute("user", "ppp");
return SUCCESS;
}
}
<body> 添加客户 </body>
<body> 修改客户 </body>
<body>
<form action="${pageContext.request.contextPath}/login.action" method="post">
<input type="text" name="username"/><br/>
<input type="text" name="password"/><br/>
<input type="submit" value="登录"/>
</form>
</body>
<body> oyeah </body>
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有