struts2里拦截器的返回值的作用是什么? action里的返回值就是拦截器invoke后得到的返回值,你可以在拦截器里打印invoke前后的return值 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 拦截器的值返回值要么代表继续拦截 要么是success等一些类Action类提供的静态量它会根据这个值去做出相应的出来例如 如果是success那么会转向这个action配置的result name="success" 的页面其他的同样的道路你可以通过打印的方式去看这个resultSystem.out.println(invocation.invoke()); 你看看你返回的值是不是success?如果都配置是好的,应该是返回你配置的相关页面 lz 可能 是 哪个 地方 搞错了 咯 ,这个 是 肯定不可能的事情 ,lz 在 仔细找找 ,断点调调 看 排出下以下几种可能:1、楼主有使用validate框架,验证不通过时返回input对应的url2、在interceptor可能有多个返回值的地方,很有可能是return invocation.invoke();了 我重新写了个例子拦截器:public class MyInterceptor implements Interceptor{ public void destroy() { System.out.println("调用拦截器的destroy方法"); } public void init() { System.out.println("调用拦截器的init方法"); } public String intercept(ActionInvocation invocation) throws Exception { Long start = System.currentTimeMillis(); System.out.println("开始计数"); String r = invocation.invoke();//返回"success" Long end = System.currentTimeMillis(); System.out.println(end - start); System.out.println("计数完毕"); System.out.println(r); //打印的也是"success" return "add"; //然后这边写成任意的字符串,都会返回到视图为success的界面,为何? } }<struts> <package name="default1" namespace="/" extends="struts-default"> <interceptors> <interceptor name="myIntcpt" class="com.wjf.action.MyInterceptor"></interceptor> <interceptor-stack name="mydefault"> <interceptor-ref name="defaultStack"></interceptor-ref><!-- 系统的放在前面 --> <interceptor-ref name="myIntcpt"></interceptor-ref> </interceptor-stack> </interceptors> <action name="test" class="com.wjf.action.TestAction"> <interceptor-ref name="mydefault"></interceptor-ref> <result name="success">/success.jsp</result> <result name="add">/add.jsp</result> </action> </package> </struts> action:public class TestAction extends ActionSupport{ public String execute() throws Exception { System.out.println("执行了action的execute方法"); return super.execute(); }} 拦截器方法直接写一句return “add”;看看 楼主的代码与配置无问题,在家用你的代码运行了下,和你描述的情况一样。查看原码发现STRUTS2的运行机制就是这样的,如果你有调用action里对应的方法,action有返回值就按其返回值去执行相应的result,如果不调用action里对应的方法就以最后一个拦截器返回值去执行相应的result.测试代码如下:public class MyInterceptor implements Interceptor { public void destroy() { System.out.println("调用拦截器的destroy方法"); } public void init() { System.out.println("调用拦截器的init方法"); } public static boolean condition = true;// 用于循环使用 public String intercept(ActionInvocation invocation) throws Exception { if (condition) { condition = false; Long start = System.currentTimeMillis(); System.out.println("开始计数"); String r = invocation.invoke();// 返回"success" Long end = System.currentTimeMillis(); System.out.println(end - start); System.out.println("计数完毕"); System.out.println(r); // 打印的也是"success" } else { condition = true; } return "add"; // 然后这边写成任意的字符串,都会返回到视图为success的界面,为何? }} 楼上是正解,这是struts2的机制,只要调用 invoke()方法后,如果能成功的调用了对应Action类中的方法,strtus2就会按照该方法的返回值去找对应的result,从而忽略拦截器的返回值;如果你不调用invoke()方法,那么页面就会跳转到你在拦截器中指定的result对应的页面。这个方式通常用于权限验证,当符合权限要求的时候才会调用invoke()方法,执行Action类中的方法;不满足权限的调价直接返回错误页面,这是就用到了拦截器中的返回值了。 【高分求解】关于编辑新闻的控件 问下怎么接受 xml 格式的响应!! c3p0连接数问题 菜鸟提问:更改用户密码,用户资料 Applet如何取得服务器的IP地址? DIV+CSS所谓的web2.0标准制作网页究竟好不好? 在同一目录下编译成功的两个文件,一个可运行一个不行?? 用JDBC连接SQL Server2000的问题 急!!如何实现内容的上移下移的排序功能 servlet传中文时乱码 一对多映射 请教大家:关于java的堆创建的问题
它会根据这个值去做出相应的出来
例如 如果是success那么会转向这个action配置的result name="success" 的页面
其他的同样的道路
你可以通过打印的方式去看这个result
System.out.println(invocation.invoke());
如果都配置是好的,应该是返回你配置的相关页面
1、楼主有使用validate框架,验证不通过时返回input对应的url
2、在interceptor可能有多个返回值的地方,很有可能是return invocation.invoke();了
public class MyInterceptor implements Interceptor{ public void destroy() {
System.out.println("调用拦截器的destroy方法");
}
public void init() {
System.out.println("调用拦截器的init方法");
}
public String intercept(ActionInvocation invocation) throws Exception {
Long start = System.currentTimeMillis();
System.out.println("开始计数");
String r = invocation.invoke();//返回"success"
Long end = System.currentTimeMillis();
System.out.println(end - start);
System.out.println("计数完毕");
System.out.println(r); //打印的也是"success"
return "add"; //然后这边写成任意的字符串,都会返回到视图为success的界面,为何?
}
}<struts>
<package name="default1" namespace="/" extends="struts-default">
<interceptors>
<interceptor name="myIntcpt" class="com.wjf.action.MyInterceptor"></interceptor>
<interceptor-stack name="mydefault">
<interceptor-ref name="defaultStack"></interceptor-ref><!-- 系统的放在前面 -->
<interceptor-ref name="myIntcpt"></interceptor-ref>
</interceptor-stack>
</interceptors>
<action name="test" class="com.wjf.action.TestAction">
<interceptor-ref name="mydefault"></interceptor-ref>
<result name="success">/success.jsp</result>
<result name="add">/add.jsp</result>
</action>
</package>
</struts>
action:
public class TestAction extends ActionSupport{ public String execute() throws Exception {
System.out.println("执行了action的execute方法");
return super.execute();
}
}
System.out.println("调用拦截器的destroy方法");
} public void init() {
System.out.println("调用拦截器的init方法");
} public static boolean condition = true;// 用于循环使用 public String intercept(ActionInvocation invocation) throws Exception {
if (condition) {
condition = false;
Long start = System.currentTimeMillis();
System.out.println("开始计数");
String r = invocation.invoke();// 返回"success"
Long end = System.currentTimeMillis();
System.out.println(end - start);
System.out.println("计数完毕");
System.out.println(r); // 打印的也是"success"
} else {
condition = true;
}
return "add"; // 然后这边写成任意的字符串,都会返回到视图为success的界面,为何?
}
}