需求:本人要删除某一数据,需要骓是否登录,如果没有登录就转到登录页面。因为针对的是整个系统的某些方法,所以觉得用拦截器比较理想。当在拦截器里判断用户没有登录时,把异常转到action里处理,该怎么做?
以下是我写的拦截器,请高手帮忙。public class MyInterceptor extends MethodFilterInterceptor { @Override
protected String doIntercept(ActionInvocation invocation) throws Exception {
Object username = ActionContext.getContext().getSession().get(
"username");
System.out.println("username = " + username);
if(username==null){
throw new UserSessionException();
}
String result = invocation.invoke(); return result;
}}
以下是我写的拦截器,请高手帮忙。public class MyInterceptor extends MethodFilterInterceptor { @Override
protected String doIntercept(ActionInvocation invocation) throws Exception {
Object username = ActionContext.getContext().getSession().get(
"username");
System.out.println("username = " + username);
if(username==null){
throw new UserSessionException();
}
String result = invocation.invoke(); return result;
}}
当username是null的时候,给出一个全局跳转,跳到某个action去,这样不好吗?难道你还想分发到每个action中做不同的处理?
当username是null的时候,给出一个全局跳转,跳到某个action去,这样不好吗?难道你还想分发到每个action中做不同的处理?
public String intercept(ActionInvocation invocation) throws Exception {
Object username = ActionContext.getContext().getSession().get("username");
if(username==null){
return "LOGIN";
}
String result = invocation.invoke();
return result;
}xml里面<global-results>
<result name="LOGIN" type="redirect">GetGeneralInfoAction</result>
</global-results>不就完了吗?
你到底要问什么?
//你的拦截器
}catch(Exception e){
return "error";
}然后在你的actioin中定义<result name = "error">这样不就可以了么.如果你想吧错误的信息也带出来
那就利用struts的现实页面编码标签 现实所有内容 不过那看到也没什么用啊 捕获才是正道啊