aspose file tools*
The moose likes Struts and the fly likes How to set values in select box using request scope Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "How to set values in select box using request scope" Watch "How to set values in select box using request scope" New topic
Author

How to set values in select box using request scope

Raaja Gotluru
Ranch Hand

Joined: Mar 02, 2010
Posts: 112
Hi,

I want to populate values in select box while displaying a jsp page. when I use validations, the values in the combo box will be null (if I submit the form with out selecting any value) after getting validated. How to maintain the values in the select box even after validation with out using sessions.

I am using stuts 1.3.

Thanking you.

--raaja g
vipul bondugula
Ranch Hand

Joined: Oct 14, 2010
Posts: 218
Hi Raaja,

Please post the code.


Thanks
Vipul Kumar
Raaja Gotluru
Ranch Hand

Joined: Mar 02, 2010
Posts: 112
Hi,

I am using mapping dispatch action or lookup dispatch action. I am just pasting the part of the code to get an idea of my approach.

My code is: JSP:
<html:select property="branchId" onchange="getDepartments()" style="textbox" styleId="branchId">
<htmlption value="0">Select Branch</htmlption>
<logic:notEmpty name="branches">
<htmlptions collection="branches" property="branchId" labelProperty="branchName" />
</logic:notEmpty>
<logic:empty name="branches">
No branches
</logic:empty>
</html:select>

<html:submit property="method">
<bean:message bundle="documents" key="documents.uploadfile" />
</html:submit>

Struts-config:
<action
name="documentCheckout"
input="/pages/document_checkout.jsp"
path="/documentsByDeptNoProfileCheckout"
scope="request"
parameter="method"
validate="true"
type="com.krishct.efiler.user.actions.DocumentCheckOut">

<forward name="success" path="/pages/document_checkout.jsp"/>
</action>

validation.xml
<form name="documentCheckout">
<field property="departmentId" depends="required">
<msg name="required" bundle="user" key="errors.required"/>
<arg key="user.department" bundle="user"/>
</field>
</form>

action method:

public ActionForward populate(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
if(request.getSession(false) == null || request.getSession(false).getAttribute("userId") == null) {

System.out.println("...session fail from DocumentCheckOut.....");
return mapping.findForward("sessionfail");
}
List departments=null,profiles=null,documents,branches=null;
Integer profileId=null,roleId=null,deptId=null,branchId=null,userId=0;
String deptName,branchName,userName;
IUserLocation iUserLocation=new UserLocation();
try {
branches=iUserLocation.getBranch();
request.setAttribute("branches", branches);
} catch (Exception e) {
e.printStackTrace();
}
return mapping.findForward("success");
}

public ActionForward documentCheckOut(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
if(request.getSession(false) == null || request.getSession(false).getAttribute("userId") == null) {

System.out.println("...session fail from DocumentCheckOut.....");
return mapping.findForward("sessionfail");
}
IUserLocation iUserLocation=new UserLocation();
List branches=null;
DynaValidatorForm dform=(DynaValidatorForm)form;

Integer status=null;
try {
Integer branchId=(Integer) request.getParameter("branchId");

status=iUserLocation.docCheckOut(branchId);

branches=iUserLocation.getBranch();
request.setAttribute("branches", branches);


if(status>0 ){
messages = new ActionMessages();
messages.add("sts",new ActionMessage("file.checkout.status"));
saveMessages(request, messages);
return mapping.findForward("success");
}
if(status==0 || status.equals(null)){
messages = new ActionMessages();
messages.add("sts",new ActionMessage("file.error.checkout"));
saveErrors(request, messages);
return mapping.findForward("failure");
}
} catch (Exception e) {

e.printStackTrace();
}
return mapping.findForward("success");
}

Problem is if I submit the document without selecting the values validation is happening but the select tag is not populated with the values. It will become empty but if I keep that list object in populate method in session the select tag will be having values even after validation. In my application I have lot of select tags I cant maintain all the values in session. Is there any solution for this.

--raaja g
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to set values in select box using request scope