Pregunta

I'm having a problem with my url. First, I have to display the image and the information of the image But I need to click on a button to display my information.

This is the link with the image displayed and information not being displayed. portal/faces/chicken.jsp?image_id=1

So, after i clicked the button. It becomes portal/faces/chicken.jsp

My code for retrieving the image needs the url image_id=1 as I'm using request.getParameter("image_id") to display out the image.

This is the code for my button from the jsf:

<h:commandButton action="#{bean1.checkwork}" value="Get Info" type="submit">
    <f:param name="id" value="#{param['image_id']}"></f:param>
</h:commandButton>

This is the code for my faces-config.xml:

<navigation-rule>
    <from-view-id>/MainPage.jsp</from-view-id>
    <navigation-case>
        <from-action>#{bean1.checkwork}</from-action>
        <from-outcome>successful</from-outcome>
        <to-view-id>chicken.jsp?image_id=#{param['image_id']}</to-view-id>
    </navigation-case>
</navigation-rule>

This is the code for my function.

      public String checkwork()
      { 
           HttpServletRequest request = (HttpServletRequest)
           FacesContext.getCurrentInstance().getExternalContext().getRequest();
           String image_ID = null;
              if(request!=null)
              {
                   image_ID = request.getParameter("image_id");

                   images(image_ID);
                  student(matric);

               }
          else
          {
               System.out.println("fail");
                }

    return "successful";

}

So, does it have anything to do with the jsf, managed bean or the faces-config.xml? Anyone knows the problem with my code?

¿Fue útil?

Solución

It will indeed not appear in the URL anymore when you press the button, because the command button sends a POST request with the parameter in the request body instead of request URL. So the request parameter is definitely available. With a HTTP traffic checker (press F12 in Chrome/IE9/Firebug) you can see it.

Your concrete problem is caused because you assigned the <f:param> a wrong name.

<f:param name="id" value="#{param['image_id']}"></f:param>

This way it would only be available by request.getParameter("id"), not by getParameter("image_id") as you attempted. Fix the name accordingly:

<f:param name="image_id" value="#{param['image_id']}" />

As to your concrete question about maintaining the URL after a "function" ("function" is the wrong term, you probably meant "method", "action method") is used, just use GET instead of POST. Use <h:button> instead of <h:commandButton>. You'll only need to change the code to invoke the actual action in (post)constructor or <f:event preRenderView> of the bean associated with the target view.


Unrelated to the concrete problem, there are nicer ways to grab it in your backing bean.

String imageId = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("image_id");

Having javax.servlet.* imports in your JSF code is namely a sign that you're doing things possibly the clumsy way. Note that the request is never null (how else would the JSF code be executed?), so that nullcheck on request is also completely unnecessary.

Another way is using @ManagedProperty:

@ManagedProperty("#{param.image_id}")
private String imageId; // +setter

This works only on a request scoped bean though.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top