• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

struts equivalent of request.getParameter

 
Ranch Hand
Posts: 139
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
what is the struts tag equivalent of <%=request.getParameter(name)%> in jsp? is there one?
 
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The struts way to deal with request parameters is to put a property of the same name as the parameter in the ActionForm, and let Struts populate it for you. This way, you don't have to deal with request parameters at all, but rather, are dealing with ActionForm properties.

So, to answer your question, instead of coding

I would code a the property "name" in my ActionForm with a getName() and a setName() method.

In my jsp, I would then code:


If the jsp is called with a name parameter, struts will automatically populate the form bean with the name property.

This is why there's no struts tag that I know of to display a request parameter.

There is one in jstl, however:

<c:out value="${param.name}" />
[ March 11, 2005: Message edited by: Merrill Higginson ]
 
jonathan Greens
Ranch Hand
Posts: 139
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Merril,
thank you for the explaination. But It seems like the form bean can not automatically populate its hidden field.
Here is what my jsp
<%
out.println("UnitType is "+request.getParameter("unitType"));
%>
now show it as bean prop:
<bean:write name="newUnitForm" property="unitType"/>

<html:form onsubmit="return validateNewUnitForm(this)" action="/newUnit" method="post">
<html:hidden property="unitType"/>
...
</html:form>

Here is my bean, it's a dynaValidatorBean

and I do have the correct action mapping that maps the action newUnit to newUnitBean.
I have gotten the parameter by using request.getParameter("unitType");
but <bean:write name="newUnitForm" property="unitType"/> yeilds ""
any idea?
thank you!!
 
Merrill Higginson
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I was a little unclear in my response. What I meant to say is that when you call the action with a parameter (myAction.do?name=Harry) and that action forwards to a jsp, Struts will automatically populate the form with the parameter prior to forwaring to it. If you just call the JSP with a paramter (myJsp.jsp?name=Harry) it won't automatically populate the form bean.
 
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
it's no way answer for question asked.I am learning struts ,according to my understanding even if you call ac.jsp where action-mapping is
<action-mapping>
<action path="abc"
path="com.act"
name="acform"
input="ac.jsp">
</action-mapping>
where acform is FormBean for Action act.Form action for input form ac.jsp has abc Action name,so when we submit the Form all fields of form will be populated in acform Form Bean.Am I right?.Correct me if I am wrong.
Thanks n advance
 
Merrill Higginson
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Anupa,

Yes, you are correct. That's one of the main features of Struts is that it saves you a lot of coding by automatically populating the form bean for you.
 
jonathan Greens
Ranch Hand
Posts: 139
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Merrill,
in a.jsp i have a form that submits to actionA which forwards a parameter x to b.jsp.
in b.jsp i have a bForm, bForm is assoicated with actionB but it has a getX() property. Do i need to associate bForm with actionA in order to have struts auto-populate field x after actionA forwards x to b.jsp? but bForm is really suppose to be used by actionB only...

thank you!!
 
Merrill Higginson
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In this case, rather than passing parameter x to b.jsp, I would simply call bForm.setX() before forwarding to b.jsp. Since bForm is passed to b.jsp in the request, the property x will be available to any of the struts tags in b.jsp.

I hope this answers your question.
 
jonathan Greens
Ranch Hand
Posts: 139
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Merrill,
but I don't even have reference to bForm when I am invoking actionA.. actionA is only associated with aForm.
what am i missing?
thanks for your help!
 
Merrill Higginson
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jonathan,

You can instantiate an ActionForm any time you want and put it in the request scope. It can then be used by the JSP you forward to.

I just noticed that you appear to be using a DynaValidatorForm, so the process would be:
  • Instantiate the Form

  • DynaValidatorForm myForm = new DynaValidatorForm();
  • Set the property

  • myForm.set("x", x);
  • Put the form bean in the request using the name you gave the form in the struts configuration file

  • request.setAttribute("bForm", myForm);

    In my previous posts, I was assuming you were actually coding a form bean that was a subclass of ActionForm. If this were the case, you would instantiate the bean that you wrote, and call it's setter method.

    The point of all this is that in Struts, the standard way to pass information back and forth between the Action class and the html page is not with parameters, but through ActionForms.
     
    jonathan Greens
    Ranch Hand
    Posts: 139
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Merrill,
    thank you for your continuous guidance.
     
    Ranch Hand
    Posts: 415
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    This way, you don't have to deal with request parameters at all, but rather, are dealing with ActionForm properties.

    Hi merilly

    i wont agree with ur statement ...see i may not have all my request parameters(may be hidden fileds) in my form bean .then struts do provide a tag for accessing the reuqest parameters (even though if theya re not inur form bean)

    The equivalent code for <%=request.getParameter("user")%>

    is <bean arameter id="username" name="user"/> and <%=username%>

    but ofcourse u cant avoid 100% of the scriplets but still the second one looks better than the earlier

    else u can write ur own tag which will be so simple
     
    Merrill Higginson
    Ranch Hand
    Posts: 4864
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Sreenath,

    Thanks for pointing that out. I'd forgotten about the <bean:parameter> tag, but you're right, you can use that to display a parameter.

    However, I still believe that it's better to keep most if not all of the variable data for a given jsp in the form bean. That way you have one place you can go to find information related to that form. Also, most of the Struts tags operate on either the form bean, or some other bean that is declared.
     
    reply
      Bookmark Topic Watch Topic
    • New Topic