您的位置:首页 > 大数据 > 人工智能

下拉列表---demo---bai

2017-01-08 14:07 267 查看
select.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title>My JSP 'select.jsp' starting page</title>

<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->

</head>
<script src="js/myajax.js">  <!--导入外部的js文件-->
</script>

<script type="text/javascript">

function send_ajax_request_fordept()
{
//b 通过open方法连接服务器
var method ="get";
var url = "DeptServlet";//
var paras = "op=findall";
//调用myajax.js中的send_ajax_request简化代码
send_ajax_request(method,url,paras,doreceive_dept) ;
}

//发送ajax请求,查找该部门对应的员工
function send_ajax_request_foremp()
{
var method = "get";
var deptid = document.getElementById("dept").value;
var url = "DeptServlet";
var paras = "op=findbydepid&deptid="+deptid;
//调用myajax.js中的send_ajax_request简化代码
send_ajax_request(method,url,paras,doreceive_emp) ;
}

//处理部门的响应数据
function doreceive_dept()
{
if(xmlHttpRequest.readyState==4&&xmlHttpRequest.status==200)
{
//alert(xmlHttpRequest.responseText);
//将ajax响应作为下拉列表的内部html
document.getElementById("dept").innerHTML = xmlHttpRequest.responseText;
send_ajax_request_foremp();
}
}

//处理员工的响应数据
function doreceive_emp()
{
if(xmlHttpRequest.readyState==4&&xmlHttpRequest.status==200)
{
//alert(xmlHttpRequest.responseText);
//将ajax响应作为下拉列表的内部html
document.getElementById("emp").innerHTML = xmlHttpRequest.responseText;
}
}

</script>

<body onload="send_ajax_request_fordept()">
请选部门
<select id="dept" name="dept" onchange="send_ajax_request_foremp()">
<option value="-1">请选择...</option>
</select>

员工<select id="emp" name="emp">
<option value="-1">请先选择部门...</option>
</select>
</body>
</html>


  

DeptServlet

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class DeptServlet extends HttpServlet {

/**
* Constructor of the object.
*/
public DeptServlet() {
super();
}

/**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}

/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

doPost(request,response);
}

/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

//1 中文处理
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
//2 获取op参数
String op = request.getParameter("op");
//3 根据op调用具体方法
if("findall".equals(op))
{
findAll(request,response);
}
else if("findbydepid".equals(op))
{
findByDeptId(request,response);
}
}

//根据部门编号,获取对应的员工
private void findByDeptId(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException
{
//1 读取部门 编号

int deptid = Integer.parseInt(request.getParameter("deptid"));
//思路,构造<option></option>作为下拉的子html进行返回
String html = "";
//调用模型,完成查询
List<Emp> list = new EmpDao().findByDeptid(deptid);

for(Emp e:list)
{
html += "<option value='"+e.getEid()+"'>"
+e.getEname()+"</option>";
}
PrintWriter out = response.getWriter();
out.print(html);
out.flush();
out.close();

}

//用于处理ajax请求,查找所有的部门
private void findAll(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException
{
//思路,构造<option></option>作为下拉的子html进行返回
String html = "";
List<Dept> list = new DeptDao().findAll();

for(Dept d:list)
{
html += "<option value='"+d.getDepid()+"'>"
+d.getDepname()+"</option>";
}
PrintWriter out = response.getWriter();
out.print(html);
out.flush();
out.close();
}

/**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occurs
*/
public void init() throws ServletException {
// Put your code here
}

}


  

public class Emp
{
private int eid;
private String ename;

private Dept dept;//该员工对应的部门

public int getEid() {
return eid;
}

public void setEid(int eid) {
this.eid = eid;
}

public String getEname() {
return ename;
}

public void setEname(String ename) {
this.ename = ename;
}

public Dept getDept() {
return dept;
}

public void setDept(Dept dept) {
this.dept = dept;
}

public Emp(int eid, String ename, Dept dept) {
super();
this.eid = eid;
this.ename = ename;
this.dept = dept;
}
}


  

import java.util.ArrayList;
import java.util.List;

public class EmpDao {
private static List<Emp> list = new ArrayList<Emp>();
static
{

try {
Class.forName("DeptDao");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
list.add(new Emp(1, "张3", DeptDao.list.get(0)));
list.add(new Emp(2, "张4", DeptDao.list.get(1)));
list.add(new Emp(3, "张5", DeptDao.list.get(2)));
list.add(new Emp(4, "张6", DeptDao.list.get(3)));
list.add(new Emp(5, "张7", DeptDao.list.get(0)));
list.add(new Emp(6, "张8", DeptDao.list.get(1)));
list.add(new Emp(7, "张9", DeptDao.list.get(2)));
list.add(new Emp(8, "张10", DeptDao.list.get(3)));
}

//给定部门编号,找出该部门对应的所有的员工
public List<Emp> findByDeptid(int depid)
{
List<Emp> result = new ArrayList<Emp>();
for(Emp e:list)
{
if(e.getDept().getDepid()==depid)
{
result.add(e);
}
}
return result;
}
}


  

public class Dept
{
private int depid; //部门编号
private String depname; //部门名
public int getDepid() {
return depid;
}
public void setDepid(int depid) {
this.depid = depid;
}
public String getDepname() {
return depname;
}
public void setDepname(String depname) {
this.depname = depname;
}
public Dept(int depid, String depname) {
super();
this.depid = depid;
this.depname = depname;
}
}


  

import java.util.ArrayList;
import java.util.List;

public class DeptDao
{
public static List<Dept> list = new ArrayList<Dept>();
static
{
list.add(new Dept(1,"软件部"));
list.add(new Dept(2,"商务部"));
list.add(new Dept(3,"行政部"));
list.add(new Dept(4,"咨询部"));
}

//查找所有的部门
public List<Dept>  findAll()
{
return list;
}
}


  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: