index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1" contentType="text/html; charset=UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<!-- <META HTTP-EQUIV="Refresh" CONTENT="10;URL=regd.action"> -->
<title>cascading dropdown</title>
<script language="javascript" type="text/javascript" src="getDeptNameUsingId.js"></script>
</head>
<body>
<form action="#" method="get" id="regdform" name="regdform">
<table>
<tr>
<td colspan="2"><strong><em><font color="#800000">Department of the user</font></em></strong></td></tr>
<tr >
<td><strong>Department :</strong></td>
<td>
<select name="department" style="color: #800000;font-size: medium;width:150px" onchange="findDeptDetails2(this.options[this.selectedIndex].value)">
<option value="">-----------------</option>
<c:forEach items="${allLocations}" var="x">
<option value="${x.getVch_LevelName()}">${x.getVch_LevelName()}</option>
</c:forEach>
</select>
</td></tr>
<tr>
<td><strong>Line Department :</strong></td>
<td>
<select name="linedepartment" id="linedepartment" style="color: #800000;font-size: medium;width:150px">
<option value="">-----------------</option>
</select>
</td></tr>
<tr>
<td><strong>Office :</strong></td>
<td><select name="office" style="color: #800000;font-size: medium;width:150px">
<option value="">-----------------</option>
<option value="A">A</option>
<option value="B">B</option>
<option value="C">C</option>
<option value="D">D</option>
</select></td></tr>
<tr><td><strong>Section :</strong></td>
<td><select name="section" style="color: #800000;font-size: medium;width:150px">
<option value="">-----------------</option>
<option value="A">A</option>
<option value="B">B</option>
<option value="C">C</option>
<option value="D">D</option>
</select></td></tr>
<tr><td><strong>Designation :</strong></td>
<td>
<select name="designation" style="color: #800000;font-size: medium;width:150px">
<option value="">-----------------</option>
<option value="A">A</option>
<option value="B">B</option>
<option value="C">C</option>
<option value="D">D</option>
</select></td></tr>
<tr>
<td><strong>Grade :</strong></td>
<td><select name="grade" style="color: #800000;font-size: medium;width:150px">
<option value="">-----------------</option>
<option value="A">A</option>
<option value="B">B</option>
<option value="C">C</option>
<option value="D">D</option>
</select></td></tr>
<tr><td><strong>Physical location :</strong></td>
<td>
<select name="physicallocation" style="color: #800000;font-size: medium;width:150px">
<option value="">-----------------</option>
<option value="A">A</option>
<option value="B">B</option>
<option value="C">C</option>
<option value="D">D</option>
</select></td></tr>
<tr>
<td><strong>Employee type :</strong></td>
<td>
<select name="emptype" style="color: #800000;font-size: medium;width:150px">
<option value="">-----------------</option>
<option value="A">A</option>
<option value="B">B</option>
<option value="C">C</option>
<option value="D">D</option>
</select></td></tr>
<tr>
<td> <input type="submit" value="submit"/></td></tr>
</table>
</form>
</body>
</html>
getDeptnameUsingId.js
function findDeptDetails2(v){
var xmlhttp;
if(v=="")
{
document.getElementById("linedepartment").selectedValue="";
return;
}
if(window.XMLHttpRequest)
{
xmlhttp=new XMLHttpRequest();
}
else
{
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
var y=xmlhttp.responseText;
var x=document.getElementById("linedepartment");
var option=document.createElement("option");
option.text=y;
try
{
// for IE earlier than version 8
x.add(option,x.options[null]);
}
catch (e)
{
x.add(option,null);
}
}
};
xmlhttp.open("GET","linedept.action?q="+v,true );
xmlhttp.send();
}
アクションクラス
package action;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.struts2.ServletActionContext;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import connection.AdminLevelDetails;
public class DropDownData {
private List<AdminLevelDetails> allLocations;
private List<AdminLevelDetails> allDepts;
private String location;
private List<String> allDetlocation;
public String getLocation() {
return location;
}
public void setLocation(String location) {
this.location = location;
}
public List<AdminLevelDetails> getAllDepts() {
return allDepts;
}
public void setAllDepts(List<AdminLevelDetails> allDepts) {
this.allDepts = allDepts;
}
public List<AdminLevelDetails> getAllLocations() {
return allLocations;
}
public void setAllLocations(List<AdminLevelDetails> allLocations) {
this.allLocations = allLocations;
}
public String execute()
{
try {
SessionFactory sf=(SessionFactory)ServletActionContext.getServletContext().getAttribute("sfkey");
Session s=sf.openSession();
Query q=s.createQuery("from AdminLevelDetails where int_PIdId=0");
allLocations=(ArrayList<AdminLevelDetails>)q.list();
/*Query q1=s.createQuery("from AdminLevelDetails where int_PIdId=1 and int_LevelId=2");
allDepts=(ArrayList<AdminLevelDetails>)q1.list();*/
} catch (Exception e) {
e.printStackTrace();
}
return "success";
}
public String lineDepartmentDetail()
{
try {
SessionFactory sf=(SessionFactory)ServletActionContext.getServletContext().getAttribute("sfkey");
Session s=sf.openSession();
Query q1=s.createQuery("from AdminLevelDetails where int_PIdId=1 and int_LevelId=2");
allDetlocation=(ArrayList<String>)q1.list();
Iterator itr=allDetlocation.iterator();
while(itr.hasNext())
{
System.out.println(itr.next());
}
HttpServletResponse response=ServletActionContext.getResponse();
response.getWriter().println(allDetlocation);
//allDepts=(ArrayList<AdminLevelDetails>)q1.list();
//response.getWriter().println(name);
/* HttpServletRequest request=ServletActionContext.getRequest();
HttpSession hs=request.getSession();
hs.setAttribute("username", name);
hs.setAttribute("userid", uid);*/
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
最初に、onchange イベントの最初のドロップダウンに index.jsp があります。JavaScript ファイルで ajax を呼び出します。