English 中文(简体)
使用 strutts mvc 在 jsp 中显示动态值, 不使用 usinf scriplet 来显示动态值
原标题:displaying dynamic values in jsp without usinf scriplet using struts mvc

i am using mvc struts framework in my web application. i am displaying dynamic values from my database in jsp page using scrilets <% %> its been bad practice doing that way but i followed links of how to do it without scriptlets but cant understand much ..i am using struts and i have action and bean class this is my jsp page

<%@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="2" style="font-size:20px;">Tests We Provide</td></tr>
    <tr>
        <th width="194" height="52" align="left" nowrap>Test Name</th>
        <th width="142" align="left" nowrap>Test Fee</th>
    </tr>
    <%
        String sql = "Select tname,tfee from addtest order by tname";
        ResultSet rs = SQLC.getData(sql, null);
        while (rs.next()) {
            String testname = rs.getString("tname");
            String testfee = rs.getString("tfee");
    %>
    <tr>
        <td width="194" height="30" nowrap><%=testname%></td>
        <td width="142" nowrap><%=testfee%></td>
    </tr>  
    <%
        }
    %>
</table>

please tell me how i display testnames and fees by writing code in formbean and retreive each value one by one from bean to jsp .. please tell me how i can do this without using scriplets.. answer in steps would be really appreciated thank you :)

最佳回答

将 db 读取代码移动到动作控制器。 它应该从 db 读取您需要的值, 然后将其放入请求或模型中 。

使用 jstl 输出您的值( 在 jsp 中)

<c:out value="${parameterFromRequest}"/>

定义豆类 :

public class MyBean {
    private final String tName;
    private final String tFee;

    public MyBean(String tName, String tFee) {
        this.tName = tName;
        this.tFee = tFee;
    }

    public String getTName() {
        return tName;
    }

    public String getTFee() {
        return tFee;
    }
}

在动作控制器中创建收藏 :

String sql = "Select tname,tfee from addtest order by tname";
ResultSet rs = SQLC.getData(sql, null);
Collection<MyBean> myBeans = new ArrayList<MyBean>();
while (rs.next()) {
    String testname = rs.getString("tname");
    String testfee = rs.getString("tfee");
    myBeans.add(new MyBean(testname, testfee));
}

request.setAttribute("myBeans", myBeans);

在 jsp 中访问 :

<c:forEach var="myBean" items="${myBeans}">
    Name: <c:out value="${myBean.tName}"/>
    Fee: <c:out value="${myBean.tFee}"/>
</c:forEach>
问题回答

暂无回答




相关问题
MaxPooledStatements setting in JDBC oracle

I can t figure out how to set MaxPooledStatements in Oracle using the Oracle thin JDBC driver. Could someone point me in the right direction?

pass ResultSet from servlet to JSP

I am doing the following in my SampleServlet.java //Fill resultset from db .... try { ArrayList Rows = new ArrayList(); while (resultSet.next()){ ArrayList row = new ArrayList(); ...

How to correctly use ResultSet with h:dataTable

The problem is, that after displaying the ResultSet with <h:dataTable>, the connection is left open. If I close it, it closes the ResultSet too. I m thinking about copying the ResultSet data ...

Mysql session variable in JDBC string

am using this connection string to connect to mysql from java: jdbc:mysql://localhost:3306/db?noDatetimeStringSync=true&useUnicode=yes&characterEncoding=UTF-8 is it possible to set the ...

热门标签