怎样将后台数据显示到jsp页面的select中去

怎样将后台数据显示到jsp页面的select中去,第1张

后台数据显示到jsp页面的select中去的思路:

1、查询dao层的数据库接口得到满足条件的数据

2、将list中的数据传到request范围到jsp页面

3、页面迭代显示到指定的select节点下

具体举例如下:

导入必要的包

<%@ page import="java.sql.*" %>

<%ResultSet resultset =null%>

<HTML>

<HEAD>

   <TITLE>Select element drop down box</TITLE>

</HEAD>

<BODY BGCOLOR=##f89ggh>

<%

   try{

//Class.forName("com.mysql.jdbc.Driver").newInstance()

Connection connection =

        DriverManager.getConnection

           ("jdbc:mysql://localhost/city?user=root&password=root")

      Statement statement = connection.createStatement()

      resultset =statement.executeQuery("select * from new")

%>

<center>

   <h1>Drop down box or select element</h1>

       <select>

    //开始循环处理数据,分别填充到不同的option中

       <%  while(resultset.next()){ %>

           <option><%= resultset.getString(2)%></option>

       <% } %>

       </select>

</center>

<%

       }

       catch(Exception e)

       {

            out.println("wrong entry"+e)

       }

%>

</BODY>

</HTML>

运行结果:

1,写一个查询option值得方法,返回一个List集合,或者json数组字符串

2,每次跳转到该页面时候,调用上面的方法,然后开始遍历,List集合使用<c:foreach>遍历,json使用js来 *** 作.

首先要弄清楼主项目是用struts1还是struts2  如果是1那只能用html标签

如:

如果是struts2  用s:标签

如:

再有如果在jsp页面用<%%>java代码的  都是垃圾页面 因为jsp只负责显示内容 而不是实现逻辑

希望对你有所帮助


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/sjk/6916392.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-03-30
下一篇 2023-03-30

发表评论

登录后才能评论

评论列表(0条)

保存