简体   繁体   English

如何在从Servlet转发时将JSP片段包含到JSP页面中?

[英]How to Include a JSP Fragment into a JSP Page while forwading from Servlet?

First, Please suggest me if my question heading is not correct. 首先,如果我的问题标题不正确,请建议我。

Moving on to question: Say I am having below components: 继续提问:说我有以下组件:

search.jsp - A JSP Page with a Form to Submit Data search.jsp - 具有提交数据的表单的JSP页面

Search.java - A controller Servlet having both get() and post() defined separately so that it can acts as a dispatcher for path /search.jsp Search.java - 一个控制器Servlet,它同时定义了get()post()以便它可以充当路径/search.jsp的调度/search.jsp

searchResults.jspf - A Fragment with some JSTL code to show up the Search Results searchResults.jspf - 带有一些JSTL代码的片段,用于显示搜索结果

What I want here is for every POST request the controller servlet has to do its calculation, set results as Request Attributes and than - forward the request to the view search.jsp that should include the Fragment after its own codes . 我想要的是每个POST请求,控制器servlet必须进行计算,将结果设置为请求属性,然后 - 将请求转发给视图search.jsp ,该视图应包含自己代码后的片段

So that, I can have a View Defined in such a way as: 所以,我可以通过以下方式定义视图:

search.jsp
+
searchResults.jspf

on a single page. 在一个页面上。

Problem is, I can either do Forward or Include with the dispatcher as I don't know how can i Include a fragment while forwarding to a JSP into it. 问题是,我可以使用调度程序执行转发或包含,因为我不知道如何在将JSP转发到其中时包含片段。

Let me know if I need to post some code if necessary, or need any corrections. 如果我需要在必要时发布一些代码,或者需要进行任何更正,请告诉我。

In your search.jsp embed your searchResult.jsp using jsp:include: 在你的search.jsp中使用jsp:include来嵌入你的searchResult.jsp:

<jsp:include page="searchResult.jsp"></jsp:include>

Exemple: 1. The servlet: 例子:1。servlet:

@WebServlet(name = "Servlet", urlPatterns = "/myForwardTest")
public class Servlet extends HttpServlet {

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.getRequestDispatcher("search.jsp").forward(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
          doPost(request, response);
    }
 }
  1. search.jsp: search.jsp的:

     <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>In search resust</title> </head> <body> Search.jsp embed searchResult.jsp <jsp:include page="searchResult.jsp" /> </body> </html> 
  2. searchResult.jsp searchResult.jsp

     <%@ page contentType="text/html;charset=UTF-8" language="java" %> <body> in searchResult </body> </html> 

You can include your jspf in your jsp like below: 你可以在你的jsp中包含你的jspf,如下所示:

<%@include file="searchResult.jspf" %>

you can set a statement to execute a certain section only if a particular test evaluates to true . 只有在特定测试的计算结果为true时,才能设置语句来执行某个部分。

Ex: 例如:

if(.....==true){
<%@include file="searchResult.jspf" %>
}else{
<%@include file="someOther.jspf" %>
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM