0

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>
Chris Martin
  • 30,334
  • 10
  • 78
  • 137
user1651129
  • 25
  • 2
  • 6

2 Answers2

2

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

}
Daniel
  • 36,833
  • 10
  • 119
  • 200
  • Hi. Thank you for answering my question :). but it seems the value returned to checkwork is still null.. Maybe I add my faces.config file for you guys to see. – user1651129 Sep 06 '12 at 08:42
  • Ahhhh... Im able to get my information now. but the url changed from http://localhost:8084/portal/faces/chicken.jsp?image_id=2 to http://localhost:8084/portal/faces/chicken.jsp is there anyway to make the link remain unchanged? – user1651129 Sep 06 '12 at 09:18
  • i guess there is , but its another question , if the current question is solved , accept the answer and post a new question... – Daniel Sep 06 '12 at 09:27
1

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" />
f_puras
  • 2,521
  • 4
  • 33
  • 38