Search code examples
javajspstruts2ognlstruts-tags

Why the select tag errors in Struts 2?


Well, I'm blocked with this, I have an JSP with 2 forms, one of them is to look for Pets according to an id of the customer, and the other is to register a service for one of those Pets, so the problem is that when I look for the Pets with the following form, which is linked to a PetsAction:

<s:form action="searchPets">
<div class="col-xs-3">
    <input type="text" name="customer.id" class="form-ontrol">              
</div>
    <s:submit/>
</s:form>  

I have a problem with a select tag, that is loaded by first time with data from the prepare() method of the ServiceAction, when the data from the form "searchPets" come to the JSP.

<s:select list="employees"
name="GrooService.employee.IdEmp"
listKey="IdEmp"
listValue="nameEmp"
cssClass="form-control"
id="GrooList"
/>

This is the error that I get

tag 'select', field 'list', name 'GrooService.employee.IdEmp': 
The requested list key 'employees' could not be resolved as a 
collection/array/map/enumeration/iterator type. 
Example: people or people.{name} - [unknown location]

So, what should I modify? Is it correct if I load the data of the select tag from the PetsAction too? Should I have just one action for both forms?


Solution

  • As far as both actions return the same result JSP which contains a select tag and different action classes hold the action method you have to write prepare() in both classes.

    Otherwise you can use one action class for both actions/methods.

    The exception is thrown because the list attribute of the <s:select> tag couldn't be null. You should properly initialize the variable used for the tag before returning a result that has references to that variable.