user1651129
user1651129

Reputation: 25

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>

Upvotes: 0

Views: 6957

Answers (2)

Daniel
Daniel

Reputation: 37071

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

}

Upvotes: 2

f_puras
f_puras

Reputation: 2504

I suppose you are using JSF. If so, please add the JSF tag to your question. If not, you can ignore my answer.

When you call an action, you need not parametrize it with request data. Instead, you can access all request params via the FacesContext:

public String checkwork() {
    ExternalContext ec = FacesContext.getCurrentInstance().getExternalContext();
    Map<String,String> requestParams = ec.getRequestParameterMap();
    final String id = requestParams.get("id");
    ...

Call it using

<h:commandButton action="#{bean1.checkwork}" value="Get info" />

Upvotes: 1

Related Questions