且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在JSP MVC设计中,是否可以在页面加载时自动调用Command?

更新时间:2023-12-01 18:52:16

您没有.您要做的就是调用控制器,并将其转发给JSP.您永远不会直接调用JSP.

You don't. What you do is you call the controller and have if forward to the JSP. You never call the JSPs directly themselves.

所以,最终结果是:

request --- invokes ---> Controller --- forwards to ---> JSP

在调用JSP呈现所有内容之前,Controller可以获取任何必需的内容并适当地填充请求.

The Controller can fetch whatever is necessary and populate the request appropriately before called the JSP to render it all.

附录-

这是一个简单的Servlet,映射到/MyServlet:

Here is a simple Servlet, mapped to /MyServlet :

public class MyServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        MemberDAO dao = DAOFactory.getMemberDAO();
        List<Member> members = dao.getMembers();
        request.setAttribute("members", members);
        RequestDispatcher rd = getServletContext().getRequestDispatcher("/WEB-INF/jsp/members.jsp");
        rd.forward(request, response);
    }
}

这是放在/WEB-INF/jsp/members.jsp的关联JSP:

And here is an associated JSP placed at /WEB-INF/jsp/members.jsp:

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Members List</title>
    </head>
    <body>
        <h1>Members List</h1>
        <table>
            <tr>
                <td>Member ID</td>
                <td>First Name</td>
                <td>Last Name</td>
            </tr>
            <c:forEach items="${members}" var="member">
                <tr>
                    <td>${member.id}</td>
                    <td>${member.firstName}</td>
                    <td>${member.lastName}</td>
                </tr>
            </c:forEach>
        </table>
    </body>
</html>

在浏览器中,您点击: http://yourhost/yourapp/MyServlet

In your browser, you hit: http://yourhost/yourapp/MyServlet

充当控制器的Servlet接受请求,然后对它进行操作(在这种情况下,使用简单的DAO模式从数据库中获取所有成员的列表),然后将结果放入请求中标签"members"(request.setAttribute("members", members)会执行此操作).

The servlet, acting as a controller, takes the request, acts on it (in this case getting a list of all of the members from the database using a simple DAO pattern), and then puts the results in to the request with the tag "members" (the request.setAttribute("members", members) does this).

其中一个请求已正确填充了有趣的信息,然后将servlet转发到JSP.

One the request is properly populated with interesting information, the servlet forward to the JSP.

请注意,在这种情况下,JSP位于WEB-INF目录下.根本无法从浏览器访问位于WEB-INF内的JSP.因此,请求 http://yourhost/yourapp/WEB-INF/jsp/members.jsp 只会失败.

Note in this case the JSP is located below the WEB-INF directory. JSPs located within WEB-INF are NOT accessible at all from the browser. So a request to http://yourhost/yourapp/WEB-INF/jsp/members.jsp will simply fail.

但是它们可以在内部访问.

But they are accessible internally.

因此,Servlet转发到members.jsp,然后进行members.jsp渲染,从请求中找到members值(JSTL c:forEach标记中的$ {members}),然后c:forEach遍历该请求列表,填充member变量,然后从那里填充表中的行.

So, the Servlet forwards to members.jsp, and members.jsp renders, locating the members value from the request (${members} in the JSTL c:forEach tag), and the c:forEach iterates across that list, populating the member variable, and from there filling out the rows in the table.

这是一个经典的控制器优先"模式,它使JSP不受干扰.它还有助于确保JSP仅位于MVC的View层中.在这个简单的示例中,成员和列表是模型,控制器中的Servlet,而JSP是视图.

This is a classic "controller first" pattern which keeps the JSP out of the way. It also helps maintain that the JSPs only live in the View layer of MVC. In this simple example, Member and the List is the model, the Servlet in the Controller, and the JSP is the view.