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> 
0

2 Answers 2

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"); } 
Sign up to request clarification or add additional context in comments.

3 Comments

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.
Ahhhh... Im able to get my information now. but the url changed from localhost:8084/portal/faces/chicken.jsp?image_id=2 to localhost:8084/portal/faces/chicken.jsp is there anyway to make the link remain unchanged?
i guess there is , but its another question , if the current question is solved , accept the answer and post a new question...
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" /> 

1 Comment

oh so sorry, I'm new to this so I'm not really too sure. but thanks. will try that out!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.