【问题标题】:displaying dynamic values in jsp without usinf scriplet using struts mvc使用struts mvc在jsp中显示动态值而不使用scriptlet
【发布时间】:2012-05-23 05:48:46
【问题描述】:

我在我的 Web 应用程序中使用 mvc struts 框架。 我正在使用 scrilets <% %> 在 jsp 页面中显示我的数据库中的动态值 这样做是不好的做法,但我遵循了如何在没有脚本的情况下做到这一点的链接,但不能理解很多..我正在使用 struts,我有动作和 bean 类 这是我的jsp页面

<%@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>

请告诉我如何通过在 formbean 中编写代码并从 bean 到 jsp 逐一检索每个值来显示测试名称和费用。 请告诉我如何在不使用脚本的情况下做到这一点.. 非常感谢您按步骤回答谢谢:)

【问题讨论】:

    标签: jsp jakarta-ee jdbc struts


    【解决方案1】:

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

    比使用 jstl 输出你的值(在 jsp 中):

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

    定义 bean:

    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>
    

    【讨论】:

    • 你的意思是说当我点击链接然后使用 ajax 我调用一些方法并在那个方法中我调用我在我的动作控制器中创建的函数。在那个函数中我写我的数据库读取代码..
    • while (rs.next()) { String testname = rs.getString("tname"); String testfee = rs.getString("tfee");
    • 我在动作控制器中存储这些值的位置
    • 是的。您在动作控制器中处理 ajax 请求。您可以返回此 jsp 并在 javascript 中将其作为某些 div 的 html 内容。通常它调用 MVC - 你有模型,它保存所有数据(它在控制器中形成),控制器 - 处理请求和视图 - 它不应该从数据库获取数据或准备模型,只显示来自该控制器的模型的数据准备。
    • 如果你在控制器中调用:request.setAttribute("myAttribute", "some value");与在 jsp 中相比,您可以使用 ${myAttribute} 访问它。要输出此值,您可以使用答案中的 jstl(它将转义特殊字符)。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-10-18
    • 1970-01-01
    • 1970-01-01
    • 2013-06-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多