struts-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Fredrik Boström <fredrik.bost...@helsinki.fi>
Subject Re: Losing form values after validating
Date Wed, 09 Mar 2005 20:08:09 GMT
Phew.. after some testing, recompiling and more testing and more 
recompiling, I have finally found a solution.

After posting the last message, I realised that the "categoryId" form 
field's value was searched for in a request parameter named "category", 
while the form field's name (property) that got submitted and thus 
returned after validation was "categoryId". Thus, the EL 
${param.category} naturally didn't find the "categoryId" parameter.

Changing all occurrences of "category" to "categoryId" solved this 
problem, the "categoryId" value got re-populated when returning from 
validation. Yee!

But the user-fillable fields "topic" and "message" were still empty when 
returning from validation. I realised that if I put an EL in their 
value-attributes as well, I'd probably be able to pick those values up 
from the returning request. So, I changed the tags to

Topic: <html-el:text property="topic" value="${param.topic}" size="20" 
maxlength="40" /><br/>
Message: <html-el:textarea property="message" value="${param.message}" 
cols="60" rows="10" /><br/>

Everything worked out fine! Yee!

Then I thought about my login-form in the application, which works about 
the same way. That form does get re-populated after validation without 
the value="${param.foo}" attributes. In fact, the tags didn't have a 
value-attribute at all.

So the next step was to remove the value-attributes all together from 
the topic and message tags. I recalled that this of some reason didn't 
work earlier (I got some sort of exception about not finding a bean or 
something), so that's why I put those value-attributes there in the 
first place. Well, everything worked fine without the value-attributes. 
Yee!

But when visiting the form later on, the values I filled in earlier was 
still there. Why? Ah, the scope="session" in the action definition! 
Replacing session with request did the job. Now everything works as it 
should!

So, the working configuration:

  - html-el-tags with value-attributes that pick up the request 
parameters when entering the page
  - consistent naming of form field names throughout the app (also in 
form beans, their configurations and business logic beans for safety :)
  - html-tags without the value-attribute where no data has to be picked 
up when entering the page
  - scope="request" in the action mapping
  - input=".tiles.definition.name" in the action mapping


Thanks Antony for the tiles name tip, which got me going again! :)

Regards, Fredrik




Fredrik Boström wrote:
> Thanks for your comment.
> 
> I tried what you proposed, replacing the action path with the tile 
> definition name. The result was that the tile didn't know which form to 
> display (because the page containes three forms, one of which is 
> selected according to the action request parameter), and thus didn't 
> display anything.
> 
> So, I thought "if I put another hidden field into the form holding the 
> action value"... Like this:
> 
> <logic:equal parameter="action" value="thread">
> <!-- Create new thread -->
>   <h1>New Thread</h1>
>   <html:form action="/CreateThread">
>     <html-el:hidden property="author" value="${user.nickname}"/>
>     <html-el:hidden property="action" value="${param.action}"/>
>     <html-el:hidden property="categoryId" value="${param.category}"/>
>     Topic: <html:text property="topic" value="" size="20" maxlength="40" 
> /><br/>
>     Message: <html:textarea property="message" value="" cols="60" 
> rows="10" /><br/>
>     <html:submit value="Create" />
>   </html:form>
> </logic:equal>
> 
> I also added the action field to the DynaValidatorActionForm definition 
> in the struts-config.xml. But in the validator configuration files, the 
> field is ignored.
> 
> The action definition in the struts-config.xml file still has 
> scope="session".
> 
> The result of this was quite surprising:
> 
> When submitting invalid information
>  - the validation works fine
>  - the form returns to the correct page with the correct form displayed
>  - the form fields' values are still missing EXCEPT FOR the user and 
> action fields
> 
> So the missing fields are still:
>  - categoryId
>  - topic
>  - message
> 
> Like this:
> <!-- Create new thread -->
>   <h1>New Thread</h1>
> 
>   <form name="createForm" method="post" 
> action="/forum_dev/CreateThread.do">
>     <input type="hidden" name="author" value="fredde">
>     <input type="hidden" name="action" value="thread">
>     <input type="hidden" name="categoryId" value="">
>     Topic: <input type="text" name="topic" maxlength="40" size="20" 
> value=""><br/>
>     Message: <textarea name="message" cols="60" rows="10"></textarea><br/>
>     <input type="submit" value="Create">
>   </form>
> 
> 
> As I see it, the action and categoryId fields should be treated the 
> exact same way. But still only action gets re-populated when returning 
> from the validation. Confusing...
> 
> Ideas?
> 
> 
> Regards,
> Fredrik
> 
> 
> 
> Antony Joseph wrote:
> 
>> Try this. The input point to your tile.
>> <!-- Process new Category -->
>> <action    path="/CreateCategory"
>>             type="manegen.forum.CreateCategoryAction"
>>             name="createForm"
>>             scope="session"
>>             input="input">
>>    <forward name="input" path=".view.newForm" />
>>    <forward name="success" path="/Home.do"/>
>> </action>
>>
>>
>>
>>
>> ----- Original Message -----
>> From: "Fredrik Boström" <fredrik.bostrom@helsinki.fi>
>> To: "Struts Users Mailing List" <user@struts.apache.org>
>> Subject: Re: Losing form values after validating
>> Date: Wed, 09 Mar 2005 19:46:23 +0200
>>
>>
>>> Just for completeness, I'll post some more relevant information about 
>>> the application.
>>>
>>>
>>> newForm.jsp (partly)
>>> --------------------
>>> <logic:equal parameter="action" value="thread">
>>> <!-- Create new thread -->
>>>   <h1>New Thread</h1>
>>>
>>>   <html:form action="/CreateThread">
>>>     <html-el:hidden property="author" value="${user.nickname}"/>
>>>     <html-el:hidden property="categoryId" value="${param.category}"/>
>>>     Topic: <html:text property="topic" value="" size="20" 
>>> maxlength="40" /><br/>
>>>     Message: <html:textarea property="message" value="" cols="60" 
>>> rows="10" /><br/>
>>>     <html:submit value="Create" />
>>>   </html:form>
>>> </logic:equal>
>>>
>>>
>>> struts-config.xml (partly)
>>> -------------------------
>>>     <!-- Multi purpose form for creating stuff -->
>>>     <form-bean   name="createForm"
>>>
>>> type="org.apache.struts.validator.DynaValidatorActionForm">
>>>       <form-property  name="author"
>>>                       type="java.lang.String" />
>>>       <form-property  name="categoryId"
>>>                       type="java.lang.Integer" />
>>>       <form-property  name="threadId"
>>>                       type="java.lang.Integer" />
>>>       <form-property  name="topic"
>>>                       type="java.lang.String" />
>>>       <form-property  name="message"
>>>                       type="java.lang.String" />
>>>       <form-property  name="replyto"
>>>                       type="java.lang.Integer" />
>>>       <form-property  name="categoryName"
>>>                       type="java.lang.String" />
>>>       <form-property  name="categoryDescription"
>>>                       type="java.lang.String" />
>>>     </form-bean>
>>>   </form-beans>
>>>
>>> <!-- Display new user|category|thread|message form -->
>>> <action    path="/NewForm"
>>>            forward=".view.newForm" />
>>>
>>> <!-- Process new Category -->
>>> <action    path="/CreateCategory"
>>>            type="manegen.forum.CreateCategoryAction"
>>>            name="createForm"
>>>            scope="session"
>>>            input="input">
>>>   <forward name="input" path="/NewForm.do?action=category" />
>>>   <forward name="success" path="/Home.do"/>
>>> </action>
>>>
>>> tiles-defs.xml (partly)
>>> -----------------------
>>> <tiles-definitions>
>>>
>>>   <!-- main layout -->
>>>   <definition name=".layout.main" path="/layout/layout.jsp">
>>>     <put name="title" value="Manegen Forum"/>
>>>     <put name="body" value=""/>
>>>   </definition>
>>>
>>>   <!-- views = extensions to the main layout -->
>>>   <definition name=".view.newForm" extends=".layout.main">
>>>     <put name="body" value="/newForm.jsp" />
>>>   </definition>
>>>
>>> </tiles-definitions>
>>>
>>>
>>>
>>> This is really bothering me, so any ideas would help.
>>>
>>>
>>> Best regards,
>>> Fredrik
>>>
>>>
>>>
>>>
>>> Fredrik Boström wrote:
>>>
>>>> Hi all.
>>>>
>>>> I'm working on a small message-forum project and have stumbled upon 
>>>> many problems along the way, most of which I've been able to more or 
>>>> less solve. To the most recent problem, though, I haven't found any 
>>>> solution despite lots of googling.
>>>>
>>>> I'm using a DynaValidatorActionForm to validate input from a form 
>>>> (actually several forms). The form is one of three forms in the same 
>>>> jsp-page. The form to be used is determined by a logic-tag that 
>>>> looks for an action-parameter and ckecks its value. If the value is 
>>>> "thread" the thread-form is displayed. So the URI to display the 
>>>> thread-form would be /NewForm.do?action=thread.
>>>>
>>>> I also have to pass another parameter to the thread-form, namely to 
>>>> which category the thread belongs. This parameter's value is 
>>>> inserted into a hidden field in the form. So, the final URI would be 
>>>> /NewForm.do?action=thread&category=1.
>>>>
>>>> Other fields in the form are
>>>> - a hidden username field, value from user session bean
>>>> - thread topic (input type="text")
>>>> - message (input type="textarea")
>>>>
>>>> So, the populated form (but not yet filled in by the user) would 
>>>> look like this:
>>>>
>>>> <form name="createForm" method="post" 
>>>> action="/forum_dev/CreateThread.do">
>>>> <input type="hidden" name="author" value="Joe"> <-- from bean
>>>> <input type="hidden" name="categoryId" value="1"> <-- from request

>>>> parameter
>>>> Topic: <input type="text" name="topic" maxlength="40" size="20" 
>>>> value=""><br/>
>>>> Message: <textarea name="message" cols="60" rows="10"></textarea><br/>
>>>> <input type="submit" value="Create">
>>>> </form>
>>>>
>>>> When submitting the form, I want it to be validated and if the 
>>>> validation fails, the user is returned to the same page with 
>>>> error-messages displayed. This is currently done by using the 
>>>> following action (which is the action the form submits to):
>>>>
>>>>      <action    path="/CreateThread"
>>>>                 type="manegen.forum.CreateThreadAction"
>>>>                 name="createForm"
>>>>                 scope="request"
>>>>                 input="input">
>>>>        <forward name="input"   path="/NewForm.do?action=thread" />
>>>>        <forward name="success" path="/ShowMessages.do" />
>>>>        <forward name="failure" path="/ShowThreads.do" />
>>>>      </action>
>>>>
>>>> Validation works fine (when it's failing :), the validator returns 
>>>> to the right page with the right form visible, messages display as 
>>>> they should and everythings seems to be a-ok.
>>>>
>>>> But, the problem is that when the validation fails, all form values 
>>>> (except "author" which comes from the session bean) are lost. This 
>>>> is surely due to the fact that control passes through another action 
>>>> before getting back to the form page. The values somehow get lost on 
>>>> the way.
>>>>
>>>> Is there any way to keep the form data through this process? I've 
>>>> tried putting scope="session" in the /CreateThread action but with 
>>>> no success. Another alternative would be to maintain a session bean 
>>>> which contains fields for the current category, thread etc, but that 
>>>> bean would demand lots of updating all the time. Any simple solution 
>>>> to this problem?
>>>>
>>>> Grateful for any suggestions.
>>>>
>>>> Regards, Fredrik
>>>>
>>>> ---------------------------------------------------------------------
>>>> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
>>>> For additional commands, e-mail: user-help@struts.apache.org
>>>>
>>>
>>> ---------------------------------------------------------------------
>>> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
>>> For additional commands, e-mail: user-help@struts.apache.org
>>
>>
>>
>>
>>
>> Antony Joseph
>> http://www.logicden.com
>> https://workeffort.dev.java.net
>>
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
> 

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Mime
View raw message