Mapping dynamic values ​​in jsp without usinf scriplet using struts mvc

I am using the mvc struts framework in my web application. I show dynamic values ​​from my database on a jsp page using scrilets, <% %> it was bad practice, but I followed the links on how to do this without scripts, but I can not understand a lot. I use struts and I have an action and the bean class 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 show test names and fees by typing code in formbean and return each value from bean to jsp each time .. please tell me how I can do this without using scriplets .. the answer in the steps will be very grateful thanks: )

+1
source share
1 answer

db . 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>
+1

All Articles