How to “throw” JSF2 404 error?

前端 未结 3 1905
一向
一向 2020-12-16 16:45

Let\'s say that I have an application which manages users. You can add new user, delete them, edit detail etc. Each user has na ID and has detail page on URL like this:

3条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-16 16:58

    (I ended up here searching for something similar but here's another pattern I used on a similar problem)

    The Validation/ExternalContext response above is a very good way to handle it, alternatively (since you are already inside the context) you can handle the error when parsing in the parameters from the request and deal with it internally. I think it is more of how you want to handle it in your flow than a "here's a better solution"

    //Inside "SomeManagedBean"
      public String getParam()
      {
        String value = (String) FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("key");
        if(value == null)
          return "key not Exist";
        else
          return value;
    
      }
    

    //JSF 2.0 Source (something.xhtml) ... ...

    I think the above is generally easier to work with inside the framework (you don't have to send out to an error page and disrupt the flow), but really it is simply an architectural decision. Both solutions are similar, it is just a question of breaking the flow or internal handling. Either way the ExternalContext is your friend.

提交回复
热议问题