• 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
  • Paul Clapham
  • Ron McLeod
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Rob Spoor
  • Devaka Cooray
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Jj Roberts
  • Al Hobbs
  • Piet Souris

struts 2 validation error issue[instantiat object before returning input result]

 
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hello everyone

i am working in a struts 2 application
i have a page that have a [s:select] element which filled with a List object
when the page is loaded for the first time the combobox is correctlly loaded with the data in the list
when i submit the form & the page returned to itself because of validation error [input result]
the following exception appeared

SEVERE: Servlet.service() for servlet jsp threw exception
tag 'select', field 'list', name 'employee.unit.unitId': The requested list key 'units' could not be resolved as a collection/array/map/enumeration/iterator type. Example: people or people.{name} - [unknown location]
at org.apache.struts2.components.Component.fieldError(Component.java:237)
at org.apache.struts2.components.Component.findValue(Component.java:358)
at org.apache.struts2.components.ListUIBean.evaluateExtraParams(ListUIBean.java:80)
at org.apache.struts2.components.Select.evaluateExtraParams(Select.java:105)
at org.apache.struts2.components.UIBean.evaluateParams(UIBean.java:856)
at org.apache.struts2.components.UIBean.end(UIBean.java:510)

this exception has been got because when the page has rendered for the second time it can't find [units list ] in any scope or find it null

so i need a way to fill the list object in case of validation error for the page to view

regards
 
Ranch Hand
Posts: 182
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ahmed M. Omar ,
To overcome this you need to implement "Preparable" interface and override its "prepare" method. Populate your list keys inside "prepare" method.
 
Ahmed M. Omar
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thax alot Aladiyan


but this suggestion is not the solution

i think i need a method or action that executed before returning the input result
 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Did you try it?
 
Anbarasu Aladiyan
Ranch Hand
Posts: 182
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ahmed M. Omar wrote:i think i need a method or action that executed before returning the input result


That is the purpose of implementing Preparable interface. As "David Newton" said have you tried it?
 
Ahmed M. Omar
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes i tried it and it didn't solve the problem and the src code is:

public class EmployeeAction extends BaseAction /*BaseAction implements Preparable interface*/{

private UnitDao unitDao;
private TitleDao titleDao;
private List<Title> titles;
private List<Unit> units;

/*setters & getters*/
public void prepare() throws Exception {
System.out.println("prepareMethod");
titles = titleDao.getCombo();
units = unitDao.getCombo();
}
public String insert(){

try {
employeeDao.insert(employee);
} catch (ApplicationException e) {
e.printStackTrace();
}
return SUCCESS;
}

/*the reset of the code*/

}

the struts.xml portion:

<action name="employee_*" class="EmployeeAction" method="{1}">
<result name="success" type="redirectAction">
<param name="actionName">employee_list</param>
</result>
<result name="list">base.employeeList</result>
<result name="data">base.employeeData</result>
<result name="input">base.employeeData</result>
</action>


the log4j output:

prepareMethod
Hibernate: select title0_.title_id as title1_4_, title0_.title_desc as title2_4_ from tbl_titles title0_
Hibernate: select unit0_.unit_id as unit1_3_, unit0_.unit_desc as unit2_3_ from tbl_units unit0_
10625 [http-9090-2] DEBUG com.opensymphony.xwork2.DefaultActionInvocation - Executing action method = list


prepareMethod
Hibernate: select title0_.title_id as title1_4_, title0_.title_desc as title2_4_ from tbl_titles title0_
Hibernate: select unit0_.unit_id as unit1_3_, unit0_.unit_desc as unit2_3_ from tbl_units unit0_
13172 [http-9090-2] DEBUG com.opensymphony.xwork2.DefaultActionInvocation - Executing action method = add
 
Ahmed M. Omar
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes i tried it and it didn't solve the problem and the source code is:



please help me
 
Anbarasu Aladiyan
Ranch Hand
Posts: 182
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Are you implementing Preparable interface really? In think you have forgot to un comment that line.
 
Ahmed M. Omar
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
this is the source code :

 
Anbarasu Aladiyan
Ranch Hand
Posts: 182
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I think you have forgot to populate you list keys inside prepare method.
 
Ahmed M. Omar
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
the empty prepare method that you refer to is the method of the super class that implement the prepare able interface not the concrete class Employee Action which handles the requests .The Prepare Method of the employee action has the code that populate the lists as shown above

 
Ahmed M. Omar
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
the prepare method of employee action is executed before any method execution of the action BUT when a validation error occurs and the input result is returned

and this page is rendered

the prepare method not executed
 
Anbarasu Aladiyan
Ranch Hand
Posts: 182
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Are you using more than one action class for these requests?
 
Ahmed M. Omar
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So sorry it is my fault

i am using my own interceptor stack and the order of the interceptors is not correct i put the prepare interceptor after the work flow interceptor so the prepare method was not executed if a validation error are found


i reordered the stack and it got worked correctly

thanks a lot Anbarasu Aladiyan for your efforts
 
Anbarasu Aladiyan
Ranch Hand
Posts: 182
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi i was facing the same problem and solved it using this method.

But in my test.jsp
i first involve


Now while i am in select i have use which works fine.
But if there is any problem in validation and i redirect to my test.jsp



prepare method is executed but when my test.jsp is again loaded it gives
.
id="lists" can't be used again

 
strutsBie hote
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi i was facing the same problem and solved it using this method.

But in my test.jsp
i first involve


Now while i am in select i have use which works fine.
But if there is any problem in validation and i redirect to my test.jsp



prepare method is executed but when my test.jsp is again loaded it gives
.
id="lists" can't be used again
reply
    Bookmark Topic Watch Topic
  • New Topic