Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FacesContext.getCurrentInstance().getExternalContext().redirect does not redirect immediately

Tags:

redirect

jsf

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));
    ...
}

so if I pass in an id like this

www....?facilityId=3?sdfasfda

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

like image 659
Thang Pham Avatar asked Nov 04 '10 22:11

Thang Pham


1 Answers

Yes, simply return from the method:

FacesContext.getCurrentInstance()
   .getExternalContext().redirect("DisplayList.jsf");
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.

like image 114
Bozho Avatar answered Oct 13 '22 15:10

Bozho