Controller部分:
@Controller
public class D004Controller {
@RequestMapping("/welcome.action")// 映射
public String welcome(HttpServletRequest request, HttpServletResponse response)
throws Exception {
DAO d004dao = new DAOImp(); //创建DAO实现类的对象
List<Shipper> shipperList = d004dao.selShipperAll();//调用该对象的方法
//将shipperList值放入"shipperList"中,JSP中直接用el表达式调用即可
request.setAttribute("shipperList", shipperList);
return "D004";//根据SpringMVC设置的路径,返回到WEB-INF/jsp/D004.jsp中
}
@RequestMapping("/jilian.action")//注意命名规范
@ResponseBody// ajax
public Map<String, Object> test(HttpServletRequest request, HttpServletResponse
response) throws Exception{
//从jsp中获取NAME为"shipperCode"的值(val)
String shipperCode = request.getParameter("shipperCode");
DAO d004dao = new DAOImp();
List<ShipmentPlace> ShipmentPlaceList = d004dao.ShipmentPlaceAll(shipperCode);
Map<String, Object> map = new HashMap<String, Object>();
map.put("ShipmentPlaceList", ShipmentPlaceList);
request.getSession().setAttribute("map", map);
return map;
}
}
Impl部分:
public class DAOImp implements DAO {
@Override
//根据项目需求写方法的返回值,参数等
public List<Shipper> selShipperAll() throws Exception {
Connection conn = DBConnection.getConnection();//创建Statement对象
List<Shipper> shipperList = new ArrayList<Shipper>();
Shipper shipper = null;
String sql = "select SHIPPER_NAME丆 SHIPPER_CODE from Shipper";
Statement st = conn.createStatement();
ResultSet rs = st.executeQuery(sql);
while (rs.next()) {
shipper = new Shipper();
shipper.setShipperName(rs.getString(1));
shipper.setShipperCode(rs.getString(2));
shipperList.add(shipper);
}
conn.close();
return shipperList;
}
@Override
public List<ShipmentPlace> ShipmentPlaceAll(String shipperCode) throws Exception {
Connection conn = DBConnection.getConnection();//连接数据库
List<ShipmentPlace> ShipmentPlaceList = new ArrayList<ShipmentPlace>();
ShipmentPlace ShipmentPlace = null;
String sql = "select SHIPMENT_PLACE_NAME,SHIPMENT_PLACE_CODE
from SHIPMENT_PLACE where SHIPPER_CODE= ?";
//预处理,PreparedStatement比Statement安全
PreparedStatement ps = conn.prepareStatement(sql);
ps.setString(1, shipperCode);
//PreparedStatement调用executeQuery()方法,并返回一个结果集
ResultSet rs = ps.executeQuery();
//Statement里面带有很多方法,比如executeUpdate可以实现插入,
更新和删除等,返回值类型为int
while (rs.next()) {
ShipmentPlace = new ShipmentPlace();
ShipmentPlace.setShipmentPlaceName(rs.getString(1));
ShipmentPlace.setShipmentPlaceCode(rs.getString(2));
ShipmentPlaceList.add(ShipmentPlace);
}
conn.close();
return ShipmentPlaceList;
}
}
JSP页面:
<head>
<script src="js/jquery-3.2.1.min.js" type="text/javascript" charset="utf-8"></script>
<script type="text/javascript">
$(function() {
$("#s1").change(
function() {
$.ajax({
type : "post",//请求方式(post或get)
url : "jilian.action",//发送请求的地址
async : true,//默认为true,所有请求均为异步请求
data : "shipperCode=" + $("#s1").val(),
success : function(data) {
$("#s2").empty();//清空数值
$("#s2").append( '<option value="0">' + "選択してください" +
'</option>');
$.each(data.ShipmentPlaceList, function(key, val) {
$("#s2").append('<option value="' + val.shipmentPlaceCode +
'">' + val.shipmentPlaceName + '</option>');
}
);
}
});
})
})
</script>
<body>
<td>
<select name="shipper" style="width: 200px;" id="s1">
<option value="0">選択してください</option>
//jsp标准标签库(JSTL),需要导包,items-要被循环的信息,
var-代表当前条目的变量名称
<c:forEach var="shipper" items="${shipperList}">
//<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
//<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<option value="${shipper.shipperCode}" class="td1" >
${shipper.shipperName}
</option>
</c:forEach>
</select>
</td>
</body>