Static and Dynamic inclusion-view reuse Method

Source: Internet
Author: User

The previous blogs introduced the abstract, decoupling, and reuse of servlet, listern, and *. Java in the MVC Architecture. How does v JSP process abstraction and decoupling?

Include

Although JSP cannot use abstraction and Inheritance like M and C, it has its own method: include. Through inclusion, jsp can abstract several independent parts and then combine them. Based on the differences in presentation, the combination methods are also different to achieve decoupling and reuse between different parts. Including static and dynamic include.

Static inclusion

Static includes the following labels:

<%@ include file="" %>

Static inclusion means that the JSP used in the page is merged into one before compilation, because it is merged into one before compilation, therefore, each component cannot have the same variable name.

Dynamic inclusion

Dynamically include the following tags:

<jsp:include page="">

Dynamic inclusion refers to the execution of the contained files during the runtime, that is, each part is compiled separately to form multiple files. Because they are compiled separately, they have their own Dom structure, of course, the variable names of each component can be the same.

Includes test JSP

Content. jsp

<table border="1"><tr><td>col1</td><td>col2</td></tr><tr><td>col1</td><td>col2</td></tr></table>

Header content header. jsp

Footer. jsp

Static inclusion

Index. jsp

<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%><%@ include file="/header.jsp" %><%@ include file="/content.jsp" %><%@ include file="/footer.jsp" %>

Compiled class files


Merged HTML page

Dynamic inclusion

Index. jsp

<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%><%@ include file="/header.jsp" %><jsp:include page="/content.jsp"></jsp:include><jsp:include page="/footer.jsp"></jsp:include>

Compiled class files


Generated HTML page

We can see that the HTML pages generated by the two are the same. When should dynamic inclusion be used? When should I use static inclusion? In my understanding, dynamic inclusion is used when components are highly correlated, such as using the same Dom structure and data to be referenced. When components are not closely related, for example, dynamic and static content can be included in HTML static content.

Static inclusion

Take the commonly used el and jstl as examples. Combined with the above, it is equivalent to the content that does not need to be compiled. We use static inclusion. Write the jstl Declaration as a separate JSP file and then reference it, <% @ include file = "/common. jsp" %>.

Common. jsp

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

In this way, common. jsp can be reused.


For more DRP blogs, visit DRP project summary.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.