Using managed-property with CommandButton in Java Server Faces

后端 未结 3 1681
庸人自扰
庸人自扰 2021-01-23 08:11

In addition to my question \"Creating an “Edit my Item”-page in Java Server Faces with Facelets\" I would liek to cover an issue that this provided.

When I press the com

3条回答
  •  攒了一身酷
    2021-01-23 08:51

    There are several ways to preserve the ID from the original GET URL. I'm not attempting to be comprehensive.

    Add a param to a commandLink

    
      
    
    

    Any time the link is clicked, the ID will be set from parameter.

    Use a hidden field

    
      
      

    ID:

    Info:

    Any time the form is posted, the ID will be set from the form.


    Preserving the URL

    Since the form URL does not include the original query, a post will remove the ID from the URL in the browser bar. This can be rectified by use of a server-side redirect after the action has been performed.

      public String save() {
        System.out.println("Saving changes to persistence store: id=" + id);
        redirect();
        return null; // no navigation
      }
    
      private void redirect() {
        FacesContext context = FacesContext.getCurrentInstance();
        ExternalContext ext = context.getExternalContext();
        UIViewRoot view = context.getViewRoot();
        String actionUrl = context.getApplication().getViewHandler().getActionURL(
            context, view.getViewId());
        try {
          // TODO encode id value
          actionUrl = ext.encodeActionURL(actionUrl + "?ID=" + id);
          ext.redirect(actionUrl);
        } catch (IOException e) {
          throw new FacesException(e);
        }
      }
    

提交回复
热议问题