一个简单的数据查询显示jsp

以前使用jstl标签库只是使用core来显示一些数据,非常方便,今天看了下发现jstl还有其他的标签,使用都非常方便,

1、sql标签,可以直接访问数据库,后台代码都不需要了,这在某些时候非常适合使用

看示例:

<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"  %><!--  核心标签库,-->
<%@ taglib prefix="sql" uri="http://java.sun.com/jsp/jstl/sql"  %><!-- 数据库标签 -->
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"  %><!-- 格式化 -->

<%@ taglib prefix="x" uri="http://java.sun.com/jsp/jstl/xml"  %><!-- xml标签 -->
<%@ taglib prefix="fun" uri="http://java.sun.com/jsp/jstl/functions"  %>
<%
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 'index.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>
  <body>
    <sql:setDataSource var="datacon" driver="com.mysql.jdbc.Driver"
     url="jdbc:mysql://localhost/mysql"
     user="root"  password="mysql"/><!-- 数据库标签直接获取数据源 -->
     
     <sql:query var="query" dataSource="${datacon}"> 
        select * from help_category;          
     </sql:query><!-- 数据库标签直接执行sql -->
     
     <table border="1">
         <c:forEach var="row" items="${query.rows}">
             <tr>
                 <td>${row.help_category_id }</td>
                 <td>${row.name }</td>
                 <td>${row.parent_category_id }</td>
             </tr>
         </c:forEach><!-- 核心标签输出 -->
     </table>
    
  </body>
</html>

以上没有使用xml标签和格式化标签,其实使用都非常简单,详细见jstl API

原文地址:https://www.cnblogs.com/huxdiy/p/3663505.html