Jsp fragment

Jsp fragment DEFAULT

Class JspFragment

java.lang.Object extended byjavax.servlet.jsp.tagext.JspFragment
public abstract class JspFragment
extends java.lang.Object

Encapsulates a portion of JSP code in an object that can be invoked as many times as needed. JSP Fragments are defined using JSP syntax as the body of a tag for an invocation to a SimpleTag handler, or as the body of a <jsp:attribute> standard action specifying the value of an attribute that is declared as a fragment, or to be of type JspFragment in the TLD.

The definition of the JSP fragment must only contain template text and JSP action elements. In other words, it must not contain scriptlets or scriptlet expressions. At translation time, the container generates an implementation of the JspFragment abstract class capable of executing the defined fragment.

A tag handler can invoke the fragment zero or more times, or pass it along to other tags, before returning. To communicate values to/from a JSP fragment, tag handlers store/retrieve values in the JspContext associated with the fragment.

Note that tag library developers and page authors should not generate JspFragment implementations manually.

Implementation Note: It is not necessary to generate a separate class for each fragment. One possible implementation is to generate a single helper class for each page that implements JspFragment. Upon construction, a discriminator can be passed to select which fragment that instance will execute.



          Returns the JspContext that is bound to this JspFragment.

          Executes the fragment and directs all output to the given Writer, or the JspWriter returned by the getOut() method of the JspContext associated with the fragment if out is null.
Methods inherited from class java.lang.Object


public JspFragment()


public abstract void invoke(java.io.Writer out) throws JspException, java.io.IOException
Executes the fragment and directs all output to the given Writer, or the JspWriter returned by the getOut() method of the JspContext associated with the fragment if out is null.

- The Writer to output the fragment to, or null if output should be sent to JspContext.getOut().
- Thrown if an error occured while invoking this fragment.
- Thrown if the page that (either directly or indirectly) invoked the tag handler that invoked this fragment is to cease evaluation. The container must throw this exception if a Classic Tag Handler returned Tag.SKIP_PAGE or if a Simple Tag Handler threw SkipPageException.
- If there was an error writing to the stream.


public abstract JspContextgetJspContext()
Returns the JspContext that is bound to this JspFragment.

The JspContext used by this fragment at invocation time.

Copyright © 1999-2012 The Apache Software Foundation. All Rights Reserved.
Sours: https://tomcat.apache.org/tomcat-5.5-doc/jspapi/javax/servlet/jsp/tagext/JspFragment.html

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:

- A JSP Page with a Form to Submit Data

- A controller Servlet having both and defined separately so that it can acts as a dispatcher for path

- A Fragment with some JSTL code to show up the Search Results

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 that should include the Fragment after its own codes.

So that, I can have a View Defined in such a way as:

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.

Let me know if I need to post some code if necessary, or need any corrections.

asked Nov 4 '15 at 15:48


4,79466 gold badges3535 silver badges5353 bronze badges

Sours: https://stackoverflow.com/questions/33526097/how-to-include-a-jsp-fragment-into-a-jsp-page-while-forwading-from-servlet
  1. 2005 escalade horsepower
  2. Wiper amazon
  3. Anime girl puzzles

Fragment Attributes

A JSP fragment is a portion of JSP code passed to a tag handler that can be invoked as many times as needed. You can think of a fragment as a template that is used by a tag handler to produce customized content. Thus, unlike a simple attribute which is evaluated by the container, a fragment attribute is evaluated by a tag handler during tag invocation.

To declare a fragment attribute, you use the attribute of the directive (see Declaring Tag Attributes in Tag Files) or use the subelement of the TLD element (see Declaring Tag Attributes for Tag Handlers). You define the value of a fragment attribute by using a element. When used to specify a fragment attribute, the body of the element can contain only static text and standard and custom tags; it cannot contain scripting elements (see Chapter 9, Scripting in JSP Pages).

JSP fragments can be parameterized by means of expression language (EL) variables in the JSP code that composes the fragment. The EL variables are set by the tag handler, thus allowing the handler to customize the fragment each time it is invoked (see Declaring Tag Variables in Tag Files, and Declaring Tag Variables for Tag Handlers).

The tag discussed earlier accepts two fragments: , which is displayed for a product that’s full price, and , which is displayed for a product that’s on sale.

<sc:catalog bookDB ="${bookDB}" color="#cccccc"> <jsp:attribute name="normalPrice"> <fmt:formatNumber value="${price}" type="currency"/> </jsp:attribute> <jsp:attribute name="onSale"> <strike><fmt:formatNumber value="${price}" type="currency"/></strike><br/> <font color="red"><fmt:formatNumber value="${salePrice}" type="currency"/></font> </jsp:attribute> </sc:catalog>

The tag executes the fragment, using the values for the EL variable, if the product is full price. If the product is on sale, the tag executes the fragment using the and variables.

  • © 2010, Oracle Corporation and/or its affiliates
Sours: https://docs.oracle.com/cd/E19159-01/819-3669/bnalq/index.html
Java EE для начинающих. Урок 4: Java Server Pages (JSP). Введение.

Simple and Fragment Attribute and Variable Example

The Duke’s Bookstore tag, defined in , is a custom tag with simple and fragment attributes and variables. The tag renders the catalog of a book database as an HTML table. The tag file declares that it sets variables named and using directives. The fragment uses the variable , and the fragment uses the variables and . Before the tag invokes the fragment attributes using the element, the web container passes values for the variables back to the calling page.

<%@ attribute name="bookDB" required="true" type="database.BookDB" %> <%@ attribute name="color" required="true" %> <%@ attribute name="normalPrice" fragment="true" %> <%@ attribute name="onSale" fragment="true" %> <%@ variable name-given="price" %> <%@ variable name-given="salePrice" %> <center> <table> <c:forEach var="book" begin="0" items="${bookDB.books}"> <tr> <c:set var="bookId" value="${book.bookId}" /> <td bgcolor="${color}"> <c:url var="url" value="/bookdetails" > <c:param name="bookId" value="${bookId}" /> </c:url> <a href="${url}">< strong>${book.title}&nbsp;</strong></a></td> <td bgcolor="${color}" rowspan=2> <c:set var="salePrice" value="${book.price * .85}" /> <c:set var="price" value="${book.price}" /> <c:choose> <c:when test="${book.onSale}" > <jsp:invoke fragment="onSale" /> </c:when> <c:otherwise> <jsp:invoke fragment="normalPrice"/> </c:otherwise> </c:choose> &nbsp;</td> ... </table> </center>

The page invokes the tag that has the simple attributes , which contains catalog data, and , which customizes the coloring of the table rows. The formatting of the book price is determined by two fragment attributes, and , that are conditionally invoked by the tag according to data retrieved from the book database.

<sc:catalog bookDB ="${bookDB}" color="#cccccc"> <jsp:attribute name="normalPrice"> <fmt:formatNumber value="${price}" type="currency"/> </jsp:attribute> <jsp:attribute name="onSale"> <strike> <fmt:formatNumber value="${price}" type="currency"/> </strike><br/> <font color="red"> <fmt:formatNumber value="${salePrice}" type="currency"/> </font> </jsp:attribute> </sc:catalog>

The screen produced by is shown in Figure 8–2. You can compare it to the version in Figure 5–2.

Figure 8–2 Book Catalog

Screen capture of Duke's Bookstore book catalog, with titles, authors, prices, and "Add to Cart" links. Three discount prices are highlighted.
  • © 2010, Oracle Corporation and/or its affiliates
Sours: https://docs.oracle.com/cd/E19159-01/819-3669/6n5sg7b66/index.html

Fragment jsp

What is a JSPF file?

The file with .jspf extension is called JSP fragment; a static file included in another JSP file. The JSPF files are not compiled on their own, however, they are compiled along side the page in which they included. Its syntax is similar to the Java Server Pages (JSP) code. It contains just a fragment of JSP; not included all the entire JSP document.

JSPF file format

The term “JSP segment” is used instead as the term “JSP fragment” is overloaded in the JSP 2.0 Specification. The JSP fragments can use either .jsp or .jspf extensions and should be placed either in /WEB-INF/jspf or with the rest of the static files. The JSP fragments that are not complete pages must use the .jspf extension and must be placed in /WEB-INF/jspf

JSP or JSP Fragment File Organization

A JSP file contains the following sections in the order they are listed:

  1. Opening comments
  2. JSP page directive(s)
  3. Optional tag library directive(s)
  4. Optional JSP declaration(s)
  5. HTML and JSP code

A JSP or JSPF file both begins with a server side style comment which is called Opening Comment:

This comment can be visible only on the server side because it is removed during JSP page rendering.

When to use JSP Fragment file?

When a JSP page requires a certain but complex structure which may also re-use in other JSP pages, one way to handle this is to break it up into pieces, using the Composite View pattern (the Patterns section of Java Blueprints). For example, a JSP page sometimes has the following logical layout in its presentation structure:

JSPF File Format

In this situation, this composite JSP page can be converted into various modules, each will be called a separate JSP fragment. The JSP fragments can then be placed in appropriate locations in the composite JSP page. Hence, the JSPF file are used when static include directives are used to include a page that would not be called by itself, the files with .jspf extension should be placed in the /WEB-INF/jspf/ directory of the Web application archive (war).

JSPF Example


See Also

Sours: https://docs.fileformat.com/programming/jspf/
🖼️ JSTL для JSP (Java Servlet)

Kode Java

In this example you can learn how to include a JSP fragment into another JSP page. This is a common practice when creating a web application where we usually have a navigation section, the main content and the footer of a web page. Using the directive make it simpler to maintain the fragment of a web page, which mean that when we need to change for example the footer section we just need to alter the footer include file and all the page that includes it will get the benefit.

The page inclusion that using the direction will occur at page translation time, it is when the JSP page is translated into a Servlet by JSP container. We can use any file extension name for the JSP fragment used by the directive. In this example we use the extension which is short for JSP Fragment.

Here is an example of JSP with directive.



Like this:

Sours: https://kodejava.org/how-do-i-include-a-page-fragment-into-jsp/

Similar news:


614 615 616 617 618