aspose file tools*
The moose likes Struts and the fly likes When Validation Error I Lose My Bean Value-Struts 2 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of JavaScript Promises Essentials this week in the JavaScript forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "When Validation Error I Lose My Bean Value-Struts 2" Watch "When Validation Error I Lose My Bean Value-Struts 2" New topic
Author

When Validation Error I Lose My Bean Value-Struts 2

Katie Doody
Ranch Hand

Joined: Nov 03, 2006
Posts: 90
For some reason when validation fails on my form I lose my been value and my jsp displays empty. I am iterating through my bean in my jsp. I tried just setting the form field and displaying it in the jsp and this value returns after validation. I don't understand why my bean does not work?
Here is my action:












private String GenerateOutput(File xsltFile,File xmlFile,String req_or_easement) {

/* New code for setting up a FOP driver and showing a read-only PDF document*/
try

{

ByteArrayOutputStream out = new ByteArrayOutputStream();
ServletOutputStream output = null;
// 1. Instantiate org.apache.fop.apps.Driver.


Driver driver = new Driver();

// 2. Set the type of rendering you want to do.

driver.setRenderer(Driver.RENDER_PDF);

// 3. Set a logger to log to.

// driver.setLogger(logger);

//4. Set an Input Handler as an XSLTInputHandler.

InputHandler inputHandler = new XSLTInputHandler(xmlFile,xsltFile);

// 5. Set an output stream to render to.

driver.setOutputStream(out);

// 6. Grab the parser out of this handler and render.


driver.render(inputHandler.getParser(), inputHandler.getInputSource());
// inputHandler.run(driver);




getResponse().setContentType("application/pdf");
//ked changed to avoid use of back button
getResponse().setHeader("Content-disposition","attachment;filename=easEasement.pdf" );
byte[] content = out.toByteArray();
getResponse().setContentLength(content.length);
output = getResponse().getOutputStream();
output.write(content);
output.flush();
output.close();



} catch (Exception e) {
setActionErrors(null);
getLogger().error("Exception in EASEasementTemplateA.GenerateOutput", e);
addActionError(EASMessages.MESSAGE1);
}

return SUCCESS;

}




public Logger getLogger() {
return logger;
}

public void setLogger(Logger logger) {
this.logger = logger;
}



public void setServletResponse(HttpServletResponse httpServletResponse) {
this.setResponse(httpServletResponse);
}

public HttpServletResponse getServletResponse() {
return this.getResponse();
}

public HttpServletRequest getRequest() {
return request;
}

public void setServletRequest(HttpServletRequest request) {
this.setRequest(request);
}

public HttpServletResponse getResponse() {
return response;
}

public void setResponse(HttpServletResponse response) {
this.response = response;
}

public void setRequest(HttpServletRequest request) {
this.request = request;
}


public String getCommand() {
return command;
}

public void setCommand(String command) {
this.command = command;
}

public Map getSession() {
return session;
}

public void setSession(Map session) {
this.session = session;
}

public EASEasementScheduleTO getEasEasementScheduleTO() {
return easEasementScheduleTO;
}

public void setEasEasementScheduleTO(EASEasementScheduleTO easEasementScheduleTO) {
this.easEasementScheduleTO = easEasementScheduleTO;
}

public EASEasementTO getEasEasementTO() {
return easEasementTO;
}

public void setEasEasementTO(EASEasementTO easEasementTO) {
this.easEasementTO = easEasementTO;
}

public String getXslt() {
return xslt;
}

public void setXslt(String xslt) {
this.xslt = xslt;
}

public String getRequestNbr() {
return requestNbr;
}

public void setRequestNbr(String requestNbr) {
this.requestNbr = requestNbr;
}

public int getReqId() {
return reqId;
}

public void setReqId(int reqId) {
this.reqId = reqId;
}

public String getJobNbr() {
return jobNbr;
}

public void setJobNbr(String jobNbr) {
this.jobNbr = jobNbr;
}

public StringBuffer getXmlHistory() {
return xmlHistory;
}

public void setXmlHistory(StringBuffer xmlHistory) {
this.xmlHistory = xmlHistory;
}

public String getPartner() {
return partner;
}

public void setPartner(String partner) {
this.partner = partner;
}

public String getPermAcres() {
return permAcres;
}

public void setPermAcres(String permAcres) {
this.permAcres = permAcres;
}

public String getKeyword() {
return keyword;
}

public void setKeyword(String keyword) {
this.keyword = keyword;
}

public String getTempAcres() {
return tempAcres;
}

public void setTempAcres(String tempAcres) {
this.tempAcres = tempAcres;
}

public String getLawTempEasement() {
return lawTempEasement;
}

public void setLawTempEasement(String lawTempEasement) {
this.lawTempEasement = lawTempEasement;
}

public String getLawPermEasement() {
return lawPermEasement;
}

public void setLawPermEasement(String lawPermEasement) {
this.lawPermEasement = lawPermEasement;
}

public String getWitness() {
return witness;
}

public void setWitness(String witness) {
this.witness = witness;
}

public String getGrantor() {
return grantor;
}

public void setGrantor(String grantor) {
this.grantor = grantor;
}

public String getRange2() {
return range2;
}

public void setRange2(String range2) {
this.range2 = range2;
}

public EASFormParamTO getEasFormParamTO() {
return easFormParamTO;
}

public void setEasFormParamTO(EASFormParamTO easFormParamTO) {
this.easFormParamTO = easFormParamTO;
}

public String getOriginalUser() {
return originalUser;
}

public void setOriginalUser(String originalUser) {
this.originalUser = originalUser;
}


public String getRole() {
return role;
}

public void setRole(String role) {
this.role = role;
}

public String getTitle() {
return title;
}

public void setTitle(String title) {
this.title = title;
}

public String getLastName() {
return lastName;
}

public void setLastName(String lastName) {
this.lastName = lastName;
}

public String getFirstName() {
return firstName;
}

public void setFirstName(String firstName) {
this.firstName = firstName;
}

public EASUserInfoTO getEasUserInfoTO() {
return easUserInfoTO;
}

public void setEasUserInfoTO(EASUserInfoTO easUserInfoTO) {
this.easUserInfoTO = easUserInfoTO;
}

public String getEid() {
return eid;
}

public void setEid(String eid) {
this.eid = eid;
}

public boolean isReadOnly() {
return readOnly;
}

public void setReadOnly(boolean readOnly) {
this.readOnly = readOnly;
}



}


My jsp:
Tom Rispoli
Ranch Hand

Joined: Aug 29, 2008
Posts: 349
This is a lot of info, it might help if you could narrow it down more. Are none of the fields on your JSP populating after the errror? Is it just a subset of thie field? If so, which fields.

Can you also include the html source of some of the fields that are disappearing?
Katie Doody
Ranch Hand

Joined: Nov 03, 2006
Posts: 90
None of the fields were populating because I was using a session bean to display the data. As far as I can find, I need to set all the data in the form in order to do the validation. Well, I know this is now working since I set all the form values. but if anyone knows of a way to use the bean it would be nice.
Katie Doody
Ranch Hand

Joined: Nov 03, 2006
Posts: 90
I am trying to simplify my question in the hopes that someone will answer. I have been stuck on this for a while. In my form I have a getter and setter for transfer object. I also have individual fields that I set.

If I set the individual fields on the form, the values stay when validation fails but when I use the iterate tag or the bean tag the values do not stay.

So I have the following transfer object:


In my form, I have a setter and getter for the object and not for each fields in the object. When I use the object, all the values are wiped out if validation fails.




Any help would be appreciated.
Tom Rispoli
Ranch Hand

Joined: Aug 29, 2008
Posts: 349
From your jsp snippet it looks like the problem is that you aren't storing the values in any type of input field (like a hidden field). If this is the case that data will be lost because it isn't sent back to your application server when your html form is submitted.

I don't think that s:bean tag you have in there is having any effect since I think it creates a new instance of a bean that you have to populate. I think its still getting the data from your action class.

Also, its a little confusing to me the way you're using the word "form". This implies to me that you are using the modelDriven Interceptor to get the effect of a struts 1 form. It looks to me like you are just putting the fields in your action class, but maybe I'm just missing it.
Katie Doody
Ranch Hand

Joined: Nov 03, 2006
Posts: 90
Thanks for your reply. Why is everything moving to the right ? I am using an action form in Struts 2 but I am used to Struts 1. Before I ever added the validation everything worked fine but now that I added the validation I lose any bean with multiple attributes. I know if i took my bean that is made up of first name, last name and id and created getters and setter for each of these on the action form that this would work.

The thing is I have several arraylists that this would not work for and I am not sure how to make it work. I get an error when I try to store the arraylists as hidden fields since they are not really a single hidden field. I know in struts 1 you could use a use bean?

Thanks for your help.
Tom Rispoli
Ranch Hand

Joined: Aug 29, 2008
Posts: 349
I've guessing the reason you didn't see this before has to do with the fact that a validation failure will send you back to the jsp with out running your action class to repopulate the variables in it. So when your JSP runs the only data it has to disaplay is the data that was put into the action class from the request object.

I'm a little confused about what fields you are saying don't have getters and setters, but if you can display the data with an s roperty you should also be able to put it into a hidden field with s:hidden.

If you have an arrayList of complex objects then to hide the data in the jsp you need to iterate over the list (which I assume you are already), and in each iteration move the data you are losing into hidden fields. If a lack of getters and setters in your complex object is stopping you from being able to put them into hidden fields you may need to add them.

I think the bean tag concept is different in struts2 (admittedly I havne't used it yet in struts2) but I think you're making a brand new instance of the bean that you have to populate.
Katie Doody
Ranch Hand

Joined: Nov 03, 2006
Posts: 90
Yes, that works. Thank you so much. I think I might be losing because I don't remember having to do that before.

Thank you...sigh .
sergio labarta
Greenhorn

Joined: Oct 28, 2008
Posts: 2
Katie, I am sergio email me please
labarta@vodafone.es
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: When Validation Error I Lose My Bean Value-Struts 2