Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Struts - Multiple Selection in Combo box

Status
Not open for further replies.

pamnanipayal

Programmer
Oct 17, 2002
9
IN
Please Help.
I want to mark multiple values in a list box as SELECTED.
in tag <html:select> the attribute value is a String. I can't even pass the String array, as it says type mismatch.
Please let me know what to pass for value in the following tag

<html:select property=&quot;serviceType&quot; size=&quot;3&quot; multiple=&quot;true&quot; value=&quot;<%=str3[0]%>&quot;>

Above tag marks the first element in String[] str3 as selected.
It throws error if i pass str3 as the value.

Please help me out. It is very very urgent.

Thanks in advance,
Payal.
 
You can not assign the value as you are doing.Your ActionForm subclass would have a set/get method for the property serviceType and variable in which you are storing the value would be string array.Now you would retrieve and set the values for this property in the Action subclass.

I have written a code where you select few options and click on the submit button.You would find these values in the list box as SELECTED when the page reloads.

Hope the following source code would help you

/* submit.jsp */

<%@ page language=&quot;java&quot; %>
<%@ taglib uri=&quot;/WEB-INF/struts-bean.tld&quot; prefix=&quot;bean&quot; %>
<%@ taglib uri=&quot;/WEB-INF/struts-html.tld&quot; prefix=&quot;html&quot; %>
<%@ taglib uri=&quot;/WEB-INF/struts-logic.tld&quot; prefix=&quot;logic&quot; %>

<html>
<head>
<title>Submit example</title>
</head>

<body>
<h3>Example Submit Page</h3>

<html:errors/>

<html:form action=&quot;submit.do&quot;>
<html:select property=&quot;serviceType&quot; size=&quot;3&quot; multiple=&quot;true&quot; >
<html:eek:ption value=&quot;a&quot;>Service 1</html:eek:ption>
<html:eek:ption value=&quot;b&quot;>Service 2</html:eek:ption>
<html:eek:ption value=&quot;c&quot;>Service 3</html:eek:ption>
</html:select><br>

<html:submit/>
</html:form>
</body>
</html>

/*SubmitForm.java */
package hansen.playground;

import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.*;

public final class SubmitForm extends ActionForm {


private String[] serviceType = null; // default value
public String[] getServiceType() {
return (this.serviceType);
}
public void setServiceType(String[] serviceType) {
this.serviceType = serviceType;
}


}

/*SubmitAction.java */
package hansen.playground;

import javax.servlet.http.*;
import org.apache.struts.action.*;

public final class SubmitAction extends Action {

public ActionForward perform(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response) {

SubmitForm f = (SubmitForm) form; // get the form bean
// and take the service types selected
String[] serviceType = f.getServiceType();
//and save it in the request object
request.setAttribute(&quot;serviceType&quot;, serviceType);

// Forward control to the specified success target
return (mapping.findForward(&quot;success&quot;));
}
}

/*struts-config.xml */
<?xml version=&quot;1.0&quot; encoding=&quot;ISO-8859-1&quot; ?>



<!DOCTYPE struts-config PUBLIC

&quot;-//Apache Software Foundation//DTD Struts Configuration 1.0//EN&quot;

&quot;


<!--

This is a blank Struts configuration file based on the example application,

with commented examples of some items.



NOTE: If you have a generator tool to create the corresponding Java classes

for you, you could include the details in the &quot;form-bean&quot; declarations.

Otherwise, you would only define the &quot;form-bean&quot; element itself, with the

corresponding &quot;name&quot; and &quot;type&quot; attributes, as shown here.

-->





<struts-config>





<!-- ========== Data Source Configuration =============================== -->

<!--

<data-sources>

<data-source

autoCommit=&quot;false&quot;

description=&quot;Example Data Source Configuration&quot;

driverClass=&quot;org.postgresql.Driver&quot;

maxCount=&quot;4&quot;

minCount=&quot;2&quot;

password=&quot;mypassword&quot;

url=&quot;jdbc:postgresql://localhost/mydatabase&quot;

user=&quot;myusername&quot;

/>

</data-sources>

-->



<!-- ========== Form Bean Definitions =================================== -->

<form-beans>
<form-bean name=&quot;submitForm&quot; type=&quot;hansen.playground.SubmitForm&quot;/>
</form-beans>




<!-- ========== Global Forward Definitions ============================== -->

<global-forwards>



<!-- Example logon forward

<forward name=&quot;logon&quot; path=&quot;/logon.jsp&quot;/>

-->



</global-forwards>





<!-- ========== Action Mapping Definitions ============================== -->

<action-mappings>

<action path=&quot;/submit&quot; type=&quot;hansen.playground.SubmitAction&quot; name=&quot;submitForm&quot; input=&quot;/jsp/submit.jsp&quot; scope=&quot;request&quot;>
<forward name=&quot;success&quot; path=&quot;/jsp/submit.jsp&quot;/>
<forward name=&quot;failure&quot; path=&quot;/jsp/submit.jsp&quot;/>
</action>
</action-mappings>



</struts-config>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top