File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Struts and the fly likes [Struts 2] Validation Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "[Struts 2] Validation" Watch "[Struts 2] Validation" New topic
Author

[Struts 2] Validation

Fabian Angy
Ranch Hand

Joined: Oct 27, 2008
Posts: 90
Hi!

I have an ArrayList that i display in my jsp with an iterator (one textfield by iteration), but when my validate found an error (missing field, ...), i lost all my data.
So, i put a <s:hidden> with the name of my ArrayList, now, i retrieve my data BUT struts put all my data in ONLY ONE textfield and separated by a comma (,).

Someone has an idea? How to display my List correctly after the validation?

Thanks in advance!


SCJP 5
Nishan Patel
Ranch Hand

Joined: Sep 07, 2008
Posts: 684

Can you please make your question more descriptive...

But what i understand I you lost your data then give your text filed to Value property .


Thanks, Nishan Patel
SCJP 1.5, SCWCD 1.5, OCPJWSD Java Developer,My Blog
Fabian Angy
Ranch Hand

Joined: Oct 27, 2008
Posts: 90
Well..

- I have an ArrayList (of objects):



- I display this ArrayList in my JSP.



->

- But if i have an error in my validation, my data look like that (1 textfield instead of 4 textfield)

- >

My question is: how can i retrieve my data (if i have an error in my validation) in 4 textfield different like the first display?
Nishan Patel
Ranch Hand

Joined: Sep 07, 2008
Posts: 684

Hi Fabian,

let me explain you proper...

myList = getMyList();


let say your myList contains size 5. So, when you are iterate through your list it will iterate 5 times.... now what happens each times
<s:textfield name="beginDate" />


your <s:textfield contains same name name="beginDate" . So when you post your form your ><s:textfield contains all five value...

that is why it shows look like this... So, just each iteration you have to change your ><s:textfield name property.... >
Fabian Angy
Ranch Hand

Joined: Oct 27, 2008
Posts: 90
Yes, but I need that textfields have the same names to retrieve each value after the submit

Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9291
    
  17

Fabian, first of all remove the hidden text field. Now I'm really confused what you are trying to achieve here. Your textfield's name is beginDate and you are iterating over myList. So when you submit your form and there is a validation error, how are you expecting your values to be retained. Maybe I'm missing something. Can you show the source code of the Action. What I think is that when validation fails, then your myList is empty and thus you don't get any text fields because there is no myList to iterate over...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Fabian Angy
Ranch Hand

Joined: Oct 27, 2008
Posts: 90
Ankit Garg wrote:Fabian, first of all remove the hidden text field. Now I'm really confused what you are trying to achieve here. Your textfield's name is beginDate and you are iterating over myList. So when you submit your form and there is a validation error, how are you expecting your values to be retained. Maybe I'm missing something. Can you show the source code of the Action. What I think is that when validation fails, then your myList is empty and thus you don't get any text fields because there is no myList to iterate over...


Arg! You're right! My ArrayList is empty ><

But, how can i retrieve my ArrayList? :/
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9291
    
  17

Actually I don't understand the structure of your application. Is it like this. There is an action Action1 which has a property myList. That action's success forward page is MyJsp.jsp (which you showed us here). That JSP has a form whose target is Action2 which has a property beginDate...
Sahil Chopra
Greenhorn

Joined: Aug 12, 2012
Posts: 1
i m also facing the same problem..... help..


my jsp is...



<body>
<s:form validate="true" action="demo.action" >
<s:actionerror/>

<s:textarea name="ab" ></s:textarea>
<s:textarea name="ab"></s:textarea>
<s:textarea name="ab"></s:textarea>
<s:textarea name="ab"></s:textarea>
<s:submit></s:submit>
</s:form>
</body>



inf.java(action file is)

package action;

import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.interceptor.ServletRequestAware;

import com.opensymphony.xwork2.ActionSupport;

import db.DbAction;

public class inf extends ActionSupport implements ServletRequestAware {

HttpServletRequest request;
ArrayList <String> ab;

public ArrayList<String> getAb() {
return ab;
}

public void setAb(ArrayList<String> ab) {
this.ab = ab;
}

public HttpServletRequest getRequest() {
return request;
}

public void setServletRequest(HttpServletRequest arg0) {
request=arg0;

}
public String demoo() {


System.out.println("action performed"+ab);

return SUCCESS;
}


}




inf-validation is....

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE validators PUBLIC
"-//OpenSymphony Group//XWork Validator 1.0.2//EN"
"http://www.opensymphony.com/xwork/xwork-validator-1.0.2.dtd">
<validators>
<field name="ab">
<field-validator type="requiredstring">

<message>Enter Data</message>
</field-validator>
</field>

</validators>


xml file is

<action name="demo" class="action.inf" method="demoo">
<result name="success" >/jsp/demo2.jsp</result>
<result name="input" >/jsp/demo.jsp</result>
</action>




[Download untitled.bmp] Download

[Download 2.bmp] Download

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: [Struts 2] Validation
 
Similar Threads
which is best, stuts validator or javascript
jFormattedTextField issue
help on forward on actionForm's validation fail
where to put validation code???
Struts validation