FacesContext.getCurrentInstance().getExternalContext().redirect不会立即重定向

Tha*_*ham 6 jsf redirect

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

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));
    ...
}
Run Code Online (Sandbox Code Playgroud)

所以,如果我传入这样的id

www....?facilityId=3?sdfasfda
Run Code Online (Sandbox Code Playgroud)

我发现这3?sdfasfda不是一个数字,并且转到redirect语句,但它没有正确地重定向,它执行下一对试图转换3?sdfasfda为Long的行,因此产生NumberFormatException.那么有没有办法立即强制重定向,或者是否有其他方法来解决这个问题.希望有一个elsecatch:D:D.上面的代码在我的@PostConstruct init()方法中

Boz*_*zho 20

是的,只需return从方法:

FacesContext.getCurrentInstance()
   .getExternalContext().redirect("DisplayList.jsf");
return;
Run Code Online (Sandbox Code Playgroud)

当您调用时redirect(..),唯一发生的事情是Location在响应对象中设置了一个特殊的header().但是调用方法的流程会继续,除非您return在调用重定向后从中调用它.