Search code examples
jsfvariablesrequestmanaged-beangetparameter

how to place a request.getParameter("") and place it into a bean.method()?


Hi I'm new to java and jsp. I can't get my value from jsp.

Here is my code. These are made in jsp.

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

This is my managed bean code of the method

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";
    }

I'm so sorry, maybe i add on my faces-config.xml data in, maybe you guys will know whats going on. Because i added the codes that you gave and its giving me null values. 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>

Solution

  • You <f:param for that

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

    . . .

    work method code

    HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
    String id= null;
    if(request!=null){
        id= request.getParameter("id");
    
    }