关于java:我想避免在我的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

我已经阅读了很多文章,认为scriptlet是一种不好的做法,所以最终我还是决定避免这种情况。我使用struts框架开发了一个项目,其中大多数jsp页面都有Java代码。现在我想从所有页面中删除该代码。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
<%@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>


  • 将JSP与一个动作相关联,从而使一个Action类相关联
  • 用Java代码执行查询-尽管我建议将控制权传递给单独的数据访问层,但是您可以在Action中执行该查询
  • 将查询结果保存到Bean类型的列表中,该列表包含结果中的每个列
  • 将列表设置为请求的属性
  • 使用标签库,例如遍历列表,然后打印输出。

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