File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Struts and the fly likes using ajax to populate select box Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "using ajax to populate select box" Watch "using ajax to populate select box" New topic
Author

using ajax to populate select box

Sunitha Arun
Greenhorn

Joined: Dec 28, 2011
Posts: 8
Hi all,
I am having two select boxes in my jsp page. I need to populate second select box depending upon the value selected in first select box. I am able to do this. But the problem is i am not getting the value selected in the second select box in the action class. i have provided getters and setters for both select boxes.

The code snippets are given below

index.jsp
----------

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<s:head theme="ajax" />
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
<script type="text/javascript">
function show_details() {
dojo.event.topic.publish("populate");
}
</script>
</head>
<body>
<s:form id="innerForm" name="innerForm">
<table>
<tr>
<td>TRANSPORTER:</td>
<td><s:select list="{'APPOLO','RAJENDRA','SUMAN'}"
headerKey="0" headerValue="-Please Select-"
onchange="show_details()" id="sel_transporter"
name="sel_transporter">
</s:select></td>
</tr>
<tr>
<td>TRUCK:</td>
<s:url action="populateTruckAction" id="truck_url" />
<td><s:div href="%{truck_url}" listenTopics="populate"
formId="innerForm" theme="ajax" id="truckDiv"></s:div>
</td>
</tr>
<tr>
<td>Submit:</td>
<td><a id="print_anchor" href="<s:url action='printAction'/>">PRINT
</a></td>
</tr>
</table>
</s:form>
</body>
</html>


trucklist.jsp
------------


<%@ taglib prefix="s" uri="/struts-tags"%>
<s:select list="truckList" headerKey="1" name="sel_truck" id="sel_truck"></s:select>

result.jsp
-----------

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<s:form name="form1">
Transporter:<s:property value="sel_transporter"/>
Truck:<s:property value="sel_truck"/>
</s:form>
</body>
</html>

struts.xml
-----------
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<constant name="struts.enable.DynamicMethodInvocation" value="false" />
<constant name="struts.devMode" value="false" />
<package name="" extends="struts-default">
<action name="populateTruckAction" class="com.sample.MainAction"
method="populateTruck">
<result name="input">/index.jsp</result>
<result name="success">/trucklist.jsp</result>
</action>
<action name="printAction" class="com.sample.MainAction"
method="print">
<result name="success">/result.jsp</result>
</action>
</package>
</struts>


MainAction.java
-----------------

package com.sample;

import java.util.LinkedList;

import com.opensymphony.xwork2.ActionSupport;

public class MainAction extends ActionSupport {
private static String sel_transporter;
private static String sel_truck;
private LinkedList<String> transList = new LinkedList<String>();
private LinkedList<String> truckList = new LinkedList<String>();

public String populateTruck() {
System.out.println("inside populate Truck...." + sel_transporter);
if (sel_transporter.equals("APPOLO")) {
truckList.add("APP01");
truckList.add("APP02");
} else if (sel_transporter.equals("RAJENDRA")) {
truckList.add("RAJ01");
truckList.add("RAJ02");
} else {
truckList.add("SUM01");
truckList.add("SUM02");
}
return SUCCESS;
}

public String print() {
System.out.println(getSel_transporter()+"---"+getSel_truck());
return SUCCESS;
}

public String getSel_transporter() {
return sel_transporter;
}

public void setSel_transporter(String sel_transporter) {
MainAction.sel_transporter = sel_transporter;
}

public String getSel_truck() {
return sel_truck;
}

public void setSel_truck(String sel_truck) {
MainAction.sel_truck = sel_truck;
}

public LinkedList<String> getTransList() {
return transList;
}

public void setTransList(LinkedList<String> transList) {
this.transList = transList;
}

public LinkedList<String> getTruckList() {
return truckList;
}

public void setTruckList(LinkedList<String> truckList) {
this.truckList = truckList;
}
}


Looking forward to hear from you....

Regards,
Sunitha
Shankh Pani Parimal
Greenhorn

Joined: Dec 29, 2011
Posts: 8

In this case trucklist.jsp


Above select tag will be not a part of our form Submitted. We can do with the help of javascript to take selected value in form hidden field before form submition.



"formHiddenFieldValue" will pass selected value to the action.


shankh
Sunitha Arun
Greenhorn

Joined: Dec 28, 2011
Posts: 8
Hi

Thank you so much for your reply..

I tried with what you said.. but it didn't work

I modified trucklist.jsp as follows:

<%@ taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
<script type="text/javascript">
function getSelectedValue() {

var e = document.getElementById("sel_truck");
var strUser = e.options[e.selectedIndex].value;

document.getElementById("hiddenSelectedValue").value = strUser;

}
</script>
</head>
<body>
<s:form>
<s:select list="truckList" headerKey="1" name="sel_truck"
id="sel_truck" onchange="getSelectedValue()"></s:select>
<input type="hidden" id="hiddenSelectedValue"
name="formHiddenFieldValue" />

</s:form>
</body>
</html>

It didnt give me the expected result. Please let me know if i did it in the correct way.

Thanks and Regards,
Sunitha
Shankh Pani Parimal
Greenhorn

Joined: Dec 29, 2011
Posts: 8

You need to write above javascript function and hidden field on main jsp i.e 'index.jsp .
So that, it take the value from truckList and set it in form hidden value. And call the function before form submit.

It will definitely pass the selected value of truckList to action class.

Shankh
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: using ajax to populate select box
 
Similar Threads
Retrieving database values inside the <s:select> tag
form visibility
Struts2: No configuration found for the specified action
struts 2 don�t get radio button
<s:property value=""> in comparison