用于打印内容的JSP帮助程序类

use*_*157 3 java jsp servlets

我对JSP中的代码重用有疑问.我有一个JSP页面example.jsp,它发出对数据库的调用并获得结果.我有一个HelperClass.java接受记录的java类,并打印出不同的字段

response.getWriter().println
Run Code Online (Sandbox Code Playgroud)

现在我的JSP页面也有HTML,问题是HelperClass打印的内容出现在JSP页面中的内容之前.例如

<body>
    This is the first line <br/>
    HelperClass.printdata("second line"); 
</body>
Run Code Online (Sandbox Code Playgroud)

输出是

secondline This is the first line

这是一个已知的问题.设计HelperClass用于将内容打印到页面的JSP页面的最佳方法是什么.任何指针都将非常感激.

Bal*_*usC 6

只是不要使用"HelperClass来打印数据".这毫无意义.那里有你的EL.

${bean.property}
Run Code Online (Sandbox Code Playgroud)

就这样.使用servlet来控制,预处理和后处理请求.使用taglib(例如JSTL)和EL来访问和显示后端数据.

这是Servlet的基本启动示例,它在JSP中显示之前预处理请求:

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    List<Person> persons = personDAO.list(); // Get list of persons from DB.
    request.setAttribute("persons", persons); // So it's available as `${persons}` in EL.
    request.getRequestDispatcher("/WEB-INF/persons.jsp").forward(request, response); // Forward to JSP for display.
}
Run Code Online (Sandbox Code Playgroud)

在这里,Person它只是一个代表真实世界实体的Javabean类.

public class Person {
    private Long id;
    private String name;
    private String email;
    private Integer age;
    // Add/generate getters and setters here.
}
Run Code Online (Sandbox Code Playgroud)

PersonDAO#list()方法只是返回ListPerson从数据库对象:

public List<Person> list() throws SQLException {
    Connection connection = null;
    Statement statement = null;
    ResultSet resultSet = null;
    List<Person> persons = new ArrayList<Person>();

    try {
        connection = database.getConnection();
        statement = connection.createStatement("SELECT id, name, email, age FROM person");
        resultSet = statement.executeQuery();
        while (resultSet.next()) {
            Person person = new Person();
            person.setId(resultSet.getLong("id"));
            person.setName(resultSet.getString("name"));
            person.setEmail(resultSet.getString("email"));
            person.setAge(resultSet.getInteger("age"));
            persons.add(person);
        }
    } finally {
        if (resultSet != null) try { resultSet.close(); } catch (SQLException logOrIgnore) {}
        if (statement != null) try { statement.close(); } catch (SQLException logOrIgnore) {}
        if (connection != null) try { connection.close(); } catch (SQLException logOrIgnore) {}
    }

    return persons;
}
Run Code Online (Sandbox Code Playgroud)

地图在servlet中web.xmlurl-pattern/persons.隐藏了JSP,/WEB-INF以便没有人可以直接访问它而不首先请求servlet(否则会得到一个空表).

现在,这里是如何persons.jsp的样子,它使用JSTL(刚落,JSTL-1.2.jar/WEB-INF/lib)c:forEach以迭代List,它使用EL访问后端数据和bean属性.servlet将List<Person>as请求属性设置为名称,persons以便${persons}EL中可以使用它.每次迭代c:forEach都会Person返回一个实例,以便您可以使用EL显示它们的优先级.

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>

...

<table>
    <c:forEach items="${persons}" var="person">
        <tr>
            <td>${person.name}</td>
            <td>${person.email}</td>
            <td>${person.age}</td>
        </tr>
    </c:forEach>
</table>
Run Code Online (Sandbox Code Playgroud)

请通过http://example.com/contextname/persons调用它.就这样.不需要一个"助手类打印数据")要了解更多关于JSTL,检查的Java EE教程第二部分第七章,并了解更多关于EL,检查的Java EE教程第二部分第5章.要了解有关PersonDAO背后的更多信息,请查看此文章.