FacesContext.getCurrentInstance().getExternalContext().redirect不会立即重定向 [英] FacesContext.getCurrentInstance().getExternalContext().redirect does not redirect immediately

查看:238
本文介绍了FacesContext.getCurrentInstance().getExternalContext().redirect不会立即重定向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Id作为Url参数传入.我试图确保id是一个数字.如果没有重定向到主页

An Id is passing in as a Url parameter. I try to make sure that the id is an number. If not redirect to the main page

if(facilityId != null){
    try{
        Long.parseLong(facilityId);
    }catch(NumberFormatException e){
        try {
            FacesContext.getCurrentInstance().getExternalContext().redirect("DisplayList.jsf");                    
        } catch (IOException ex) {}
    }
    facility = documentSBean.findFacilityById(Long.parseLong(facilityId));
    ...
}

所以如果我输入这样的ID

so if I pass in an id like this

www....?facilityId=3?sdfasfda

我发现3?sdfasfda不是数字,然后转到redirect语句,但是它没有正确的重定向方式,它执行接下来的几行尝试将3?sdfasfda转换为Long,从而产生NumberFormatException.因此,有没有一种方法可以立即强制重定向,或者有其他方法可以解决此问题.希望在catch:D:D之后有一个else.上面的代码在我的@PostConstruct init()方法中

I catch that 3?sdfasfda is not a number, and get to the redirect statement, but it does not redirect right a way, it execute the next couple lines which try to convert 3?sdfasfda to a Long, hence yield NumberFormatException. So is there a way to force redirect right away, or is there some other way to solve this problem. Wish that there is an else after a catch :D :D. The above codes are inside my @PostConstruct init() method

推荐答案

是的,只需从方法中return:

FacesContext.getCurrentInstance()
   .getExternalContext().redirect("DisplayList.jsf");
return;

当您调用redirect(..)时,唯一发生的是在响应对象中设置了特殊的标头(Location).但是,除非您在调用重定向后从return调用它,否则调用方法的流程将继续进行.

When you invoke redirect(..) the only thing that happens is that a special header (Location) is set in the response object. But the flow of the invoking method continues, unless you return from it after you call redirect.

这篇关于FacesContext.getCurrentInstance().getExternalContext().redirect不会立即重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆