JSP中动态⽣成下拉框内容的四种⽅式⼀、使⽤servlet获取数据后跳⼊jsp页⾯
先从biz层调⽅法获取数据库数据存⼊vo对象,使⽤setAttribute⽅法向JSP页⾯传递vo对象
package cn.hnpi.jdbcweb.servlet;
import java.io.IOException;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import cn.hnpi.jdbcweb.biz.TypeBiz;
import cn.hnpi.jdbcweb.biz.impl.TypeBizImpl;
import cn.hnpi.jdbcweb.vo.TypeVo;
@WebServlet("/ToBookAdd")
public class ToBookAddServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { TypeBiz typeDao=new TypeBizImpl();
List<TypeVo> BookType();
request.setAttribute("bookType", ls);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response);
}
}
在jsp页⾯获取vo对象,循环建⽴option标签
<select name="tid" id=typeSel>
<%
List<TypeVo> ls = (ArrayList<TypeVo>)Attribute("bookType");
for (TypeVo typeVo : ls) {%>
<option value="<%=Id()%>"><%=Name() %></option>
<%
}
%>
</select>
⼆、在servlet⽂件中使⽤输出流直接输出js⽂件, 使⽤script标签src属性转到servlet页⾯
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { TypeBiz typeDao=new TypeBizImpl();
List<TypeVo> BookType();
response.setContentType("application/javascript";charset=utf-8");
PrintWriter pw =Writer();
pw.write("var types=[");
for (int i=0;i<ls.size();i++) {
pw.write("{");
pw.write("id:"+ls.get(i).getId()+",");
pw.write("name:'"+ls.get(i).getName()+"'");
pw.write("}");
if (i<ls.size()-1) {
pw.write(",");
}
}
pw.println("];");
pw.write("fillSet(types);");
pw.flush();
}
在jsp⽂件内先写⼀个空src属性script标签,然后使⽤js代码改变该标签src属性,页⾯会去获取指定路径⽂件获取到servlet⽂件内容后,写出上⾯的fillSet(types)⽅法,为下拉框添加选项
<script type="text/javascript" id="myScript"></script>
function fillSet(types){
for (var i = 0; i < types.length; i++) {
var op = new Option(types[i].name,types[i].id);
}
}
三、使⽤iframe标签获取servlet内容
iframe标签获取内容,但iframe标签不识别
response.setContentType("application/javascript";charset=utf-8");
传递的js类型,统⼀将获取内容当做html内容,所以为代码加上script标签并且改成传递HTML页⾯
TypeBiz typeDao=new TypeBizImpl();
List<TypeVo> BookType();
response.setContentType("text/html;charset=utf-8");
PrintWriter pw =Writer();
pw.write("<script type=\"text/javascript\">");
pw.write("var types=[");
for (int i=0;i<ls.size();i++) {
pw.write("{");
pw.write("id:"+ls.get(i).getId()+",");
pw.write("name:'"+ls.get(i).getName()+"'");
pw.write("}");
if (i<ls.size()-1) {
pw.write(",");
}
}
pw.println("];");
pw.write("window.parent.fillSet(types);");
pw.println("</script>");jsp页面输出的三种方式
pw.flush();
添加标签
<iframe src="ToBookAdd"></iframe>
js代码
function fillSet(types){
for (var i = 0; i < types.length; i++) {
var op = new Option(types[i].name,types[i].id);
}
}
四、使⽤jsp页⾯的包含⽅法,获取servlet内容
servlet页⾯内直接使⽤输出流输出HTML代码
TypeBiz typeDao=new TypeBizImpl();
List<TypeVo> BookType();
PrintWriter pw =Writer();
for (TypeVo typeVo : ls) {
pw.println(" <option value='"+Id()+"'>"+Name()+"</option>"); }
在jsp页⾯使⽤包含⽅法
<select name="tid" id=typeSel>
<%
out.flush();
%>
</select>
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论