从数据库中取出相应数据,存在list中,下面直接从servlet讲:,
-
servlet:
在servlet中,将存储在list中的数据,转为json类型:
相应代码:
Gson gson = new Gson();//创建Gson对象
String jsonStr = gson.toJson(list);//将list转为json字符串
PrintWriter out = response.getWriter();
out.print(jsStr);//将转化后的字符串,输送到jsp类
-
jsp
-
对应的下拉框
<div class="form-group">
<label for="theid"class="col-sm-2 control-label">主题:</label>
<div class="col-sm-6">
<select class="form-control"name="theid" id="theid">
</select>
</div>
</div>
-
对应的ajax代码
$(function(){
// ajax 请求servlet获取 主题数据
$.ajax({
url:"${pageContext.request.contextPath}/theMe/TheMeServlet?method=findAllTheme",
type:"POST",
dataType:"json",
cache:false,
async:true,
success:function(data){
for(var i = 0;i<data.length;i++){
//alert(data[i].);
$("#theid").append(" <option id='" + data[i].theid + "'>" + data[i].thename+ "</option>");
}
}
})
});