I am using Strust2 for the presentation layer.I have struts form with drop down which binds with the java object(Application) list.
Drop down displays application object list, user can select one Application and submit.When retrieve the user input value in the Action class, receiving value type is “String”, Can’t we retrieve objects directly from the struts drop down,In my case “Application” objects
private List<Application> applicaionList = new ArrayList<Application>();
@Autowired
private ApplicationService applicationService;
private Application application;
public void loadTheForm(){
applicationList = applicationService.findAll();
}
public void submitForm(){
Document doc = new Document();
doc.setApplication(application);
}
//Getter Setters...
}
application.jsp
<s:form action ="submitForm">
<s:select list ="applicationList" headerValue="---Select---" headerKey="-1" name="application"/>
</s:form>
struts.xml
<action name="submitForm" class="com.ActionSupport" method="submitForm">
<result name="success" type="tiles">/newAdminDocumentRequired.tiles</result>
</action>
When user select the value from the drop down and submit, the submitted value is string,
Can’t we take the object directly in Struts, if we can’t how we can get the object of the selected value?
Thank you,
Udeshika
What I got from your question is
I assume your Application class has a property ‘id’ which is unique to all applications. And a applicationName, which you have to show to the users.
So now, I would have solved this issue as follows
Now, this tag will create a drop-down like follows
Note that values(1,2,3,4) in OPTION elements are application.id and titles(Demo 1 App, Demo 2 App,etc) are application.applicationName.
Now, user will select and submit. The id of selected application will be sent to struts action in parameter “application”.
In Action, you can do this
Please note that I have changed type of application to String. And yes, I don’t think you can pass directly object from drop-down.
Hope it helps.