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.