在jstl结果中获取未知字符

时间:2015-02-05 11:06:00

标签: jquery jsp jstl

这是我的jstl代码getData.jsp

<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
 <%@taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql" %>

  <sql:query dataSource="jdbc/auditlog" var="rs">
            select
            concat("[{""male""",":",count(case when e.gender="M" then e.gender end),","
                     ,"""Female""",":",count(case when e.gender="F" then e.gender end),"}]") as datas
        from
            employees e
    </sql:query>

    <c:choose>
    <c:when test="${rs.rowCount>0}">        
            <c:forEach var="row" items="${rs.rows}" varStatus = "vs">${row.datas}</c:forEach>
    </c:when>
        <c:otherwise>null</c:otherwise>
    </c:choose>

这是我的帖子请求

<script>
   $.post("kyc/getData.jsp",function(res){
        alert(res.trim())
    });
</script>
  

我在结果中得到一些随机字符“[B @ 6ce474”

2 个答案:

答案 0 :(得分:0)

这是jstl中alias个名称的问题,所以我建议您尝试将其作为

<c:forEach var="rows" items="${rs.rowsByIndex}"> 
   ${rows[1]}
</c:forEach>

答案 1 :(得分:0)

请使用jsp而不是jstl

<%@page import="java.sql.*, javax.sql.*, javax.naming.*, java.util.Vector"%>
<%
    Context ctx = new InitialContext();
    DataSource ds = (DataSource) ctx.lookup("java:comp/env/jdbc/auditlog");
    Connection conn = ds.getConnection();   

    Statement stmt = conn.createStatement();

    String query = "SELECT CONCAT('{ \" Male \" : ', COUNT(CASE WHEN e.gender='M' THEN e.gender END),' , \" Female \" : ',COUNT(CASE WHEN e.gender='F' THEN e.gender END),' }') as datas FROM employees e  ";

    ResultSet rs = stmt.executeQuery(query);

    if(rs.next()) {
        out.println(rs.getString("datas"));
    } else{
        out.println("No data found!");  
    }

    rs.close();

    conn.close();
%>