displaying dynamic values in jsp without usinf scriplet using struts mvc

柔情痞子 提交于 2019-12-07 03:06:24

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

Than use jstl to output your values (in jsp):

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

Define 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);

Access in jsp:

<c:forEach var="myBean" items="${myBeans}">
    Name: <c:out value="${myBean.tName}"/>
    Fee: <c:out value="${myBean.tFee}"/>
</c:forEach>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!