使用Struts MVC在没有usinf脚本的情况下在jsp中显示动态值 [英] displaying dynamic values in jsp without usinf scriplet using struts mvc

查看:116
本文介绍了使用Struts MVC在没有usinf脚本的情况下在jsp中显示动态值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的Web应用程序中使用了mvc struts框架. 我正在使用scrilets <% %>在jsp页面中显示数据库中的动态值 这样做是很不好的做法,但是我遵循了没有scriptlet的方式进行链接,但是却不太了解..我正在使用struts,并且具有操作和bean类 这是我的jsp页面

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>

请告诉我我如何通过在formbean中编写代码来显示测试名称和费用,并从bean到jsp逐个检索每个值. 请告诉我如何不使用Scriplets即可做到这一点. 逐步回答,将不胜感激,谢谢:)

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读取您需要的值,并将其放入请求或模型中.

Move you db reading code to action controller. It should read values you need from db and put it into request or model.

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

Than use jstl to output your values (in 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;
    }
}

在动作控制器中创建集合:

Create collection in action controller:

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>

这篇关于使用Struts MVC在没有usinf脚本的情况下在jsp中显示动态值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆