繁体   English   中英

我想避免在我的jsp页面上使用scriptlet。 我有谷歌搜索并在这里搜索,但我无法解决它

[英]i want to avoid scriptlets on my jsp pages. I have google it and search here too but i was not able to solve it

我已经阅读了许多帖子,小脚本是一个不好的做法,所以最后我决定避免这一点。 我使用struts框架开发了一个项目,其中大多数jsp页面都有java代码。 现在我想从我的所有页面中删除该代码。

<%@page import="java.sql.ResultSet"%>
<%@page import="com.pra.sql.SQLC"%>
<%@page import="java.util.ArrayList"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<table align="left" width="346" border="0">
    <tr><td align="center" colspan="3" style="font-size:20px;">Tests We Provide</td></tr>
    <tr>
        <th width="80" height="38" nowrap>Test ID</th>
        <th width="200" nowrap>Test Name</th>
        <th width="144" nowrap>Test Fee</th>
    </tr>
    <%
        String sql = "Select TestID,tname,tfee from addtest order by tname";
        ResultSet rs = SQLC.getData(sql, null);
        while (rs.next()) {
            String testid = rs.getString("TestID");
            String testname = rs.getString("tname");
            String testfee = rs.getString("tfee");
    %>
    <tr>
        <td width="80" height="34" nowrap><%=testid%></td>
        <td width="200" nowrap><%=testname%></td>
        <td width="144" nowrap><%=testfee%></td>
    </tr>  
    <%
        }
    %>
</table>
  1. 将JSP与操作关联,从而将Action关联
  2. 在Java代码中执行查询 - 您可以在Action中执行此操作,但我建议将控制权传递给单独的数据访问层
  3. 将查询结果保存到包含结果中每个列的bean类型的List中
  4. 将List设置为请求的属性
  5. 使用标记库来循环列表,并打印输出。

您可以编写自己的jsp标记,也可以使用JSTL。 JSTL是oracle(原来是sun)提供的标准标记库

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM