Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

[Struts 2] Validation

 
Fabian Angy
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
Nishan Patel
Ranch Hand
Posts: 689
Eclipse IDE Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 .
 
Fabian Angy
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 689
Eclipse IDE Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, but I need that textfields have the same names to retrieve each value after the submit

 
Ankit Garg
Sheriff
Posts: 9520
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
Fabian Angy
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9520
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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>


Filename: untitled.bmp
Description: input only first 2 fields
File size: 134 Kbytes
[Download untitled.bmp] Download Attachment
Filename: 2.bmp
Description: output is ....
File size: 217 Kbytes
[Download 2.bmp] Download Attachment
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic