IT

JSP 포함 지시문, JSP 포함 조치 및 JSP 태그 파일 사용으로 파일을 포함시키는 것의 차이점은 무엇입니까?

lottoking 2020. 6. 11. 07:59
반응형

JSP 포함 지시문, JSP 포함 조치 및 JSP 태그 파일 사용으로 파일을 포함시키는 것의 차이점은 무엇입니까?


JSP로 템플릿을 만드는 두 가지 방법이있는 것 같습니다. 이 문장 중 하나가 포함 된 파일 포함

<%@ include file="foo.html" %>
<jsp:include page="foo.html" />

또는 JSP 태그 파일 사용

// Save this as mytag.tag
<%@ tag description="Description" pageEncoding="UTF-8"%>
<html>
<head>
</head>
<body>
    <jsp:doBody/>
</body>
</html>

그리고 다른 JSP 페이지에서

<%@ taglib prefix="t" tagdir="/WEB-INF/tags" %>

<t:mytag>
    <h1>Hello World</h1>
</t:mytag>

어떤 방법을 사용해야합니까? 이제는 더 이상 사용되지 않는 것으로 간주됩니까 아니면 다른 사용 사례를 모두 다루고 있습니까?

편집하다

이 태그 파일을 include를 사용하는 것과 동일하지 않습니까?

// Save this as product.tag
<%@ tag description="Product templage" pageEncoding="UTF-8"%>
<%@ tag import="com.myapp.Product" %>
<%@ attribute name="product" required="true" type="com.myapp.Product"%>

Product name: ${product.name} <br/>
Quantity: ${product.quantity} <br/>

그리고 다른 JSP에서 호출하십시오.

<%@ taglib prefix="t" tagdir="/WEB-INF/tags" %>

<t:product>
    <c:forEach items="${cart.products}" var="product">
        <t:product product="${product}"/>
    </c:forEach>
</t:product>

그것은 include를 사용하고 매개 변수를 전달하는 것과 매우 똑같습니다. 태그 파일은 포함과 동일합니까?


JSP 파일에서 컨텐츠를 재사용하는 몇 가지 메커니즘이 있습니다.

JSP에 컨텐츠를 포함 하는 다음 4 가지 메커니즘직접 재사용 으로 분류 할 수 있습니다 .
( "Head First Servlets and JSP" 에서 인용 된 처음 3 가지 메커니즘의 경우 )

1) include 지시문 :

<%@ include file="header.html" %>

정적 : 번역시 파일 속성 값의 내용을 현재 페이지 에 추가합니다 . 이 지시문은 원래 HTML 헤더와 같은 정적 레이아웃 템플릿을위한 것입니다.

2) <jsp:include>표준 행동 :

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

동적 : 요청시 페이지 속성 값의 내용을 현재 페이지 에 추가합니다 . JSP에서 오는 동적 컨텐츠를위한 것입니다.

3) <c:import>JSTL 태그 :

<c:import url=”http://www.example.com/foo/bar.html” />

동적 : 요청시 URL 속성 값의 컨텐츠를 현재 페이지에 추가합니다 . 그것은 많이 작동 <jsp:include>하지만 더 강력하고 유연합니다. 다른 두 가지 포함과 달리 <c:import> URL은 웹 컨테이너 외부에서 올 수 있습니다 !

4) 서곡과 코다 :

Static: preludes and codas can be applied only to the beginnings and ends of pages.
You can implicitly include preludes (also called headers) and codas (also called footers) for a group of JSP pages by adding <include-prelude> and <include-coda> elements respectively within a <jsp-property-group> element in the Web application web.xml deployment descriptor. Read more here:
Configuring Implicit Includes at the Beginning and End of JSPs
Defining implicit includes


Tag File is an indirect method of content reuse, the way of encapsulating reusable content. A Tag File is a source file that contains a fragment of JSP code that is reusable as a custom tag.

The PURPOSE of includes and Tag Files is different.

Tag file (a concept introduced with JSP 2.0) is one of the options for creating custom tags. It's a faster and easier way to build custom tags. Custom tags, also known as tag extensions, are JSP elements that allow custom logic and output provided by other Java components to be inserted into JSP pages. The logic provided through a custom tag is implemented by a Java object known as a tag handler.

Some examples of tasks that can be performed by custom tags include operating on implicit objects, processing forms, accessing databases and other enterprise services such as email and directories, and implementing flow control.


Regarding your Edit

Maybe in your example (in your Edit), there is no difference between using direct include and a Tag File. But custom tags have a rich set of features. They can

  • Be customized by means of attributes passed from the calling page.

  • Pass variables back to the calling page.

  • Access all the objects available to JSP pages.

  • Communicate with each other. You can create and initialize a JavaBeans component, create a public EL variable that refers to that bean in one tag, and then use the bean in another tag.

  • Be nested within one another and communicate by means of private variables.

Also read this from "Pro JSP 2": Understanding JSP Custom Tags.


Useful reading.


Conclusion

Use the right tools for each task.


Use Tag Files as a quick and easy way of creating custom tags that can help you encapsulate reusable content.

As for the including content in JSP (quote from here):

  • Use the include directive if the file changes rarely. It’s the fastest mechanism. If your container doesn’t automatically detect changes, you can force the changes to take effect by deleting the main page class file.
  • Use the include action only for content that changes often, and if which page to include cannot be decided until the main page is requested.

Possible Duplicate Question

<@include> - The directive tag instructs the JSP compiler to merge contents of the included file into the JSP before creating the generated servlet code. It is the equivalent to cutting and pasting the text from your include page right into your JSP.

  • Only one servlet is executed at run time.
  • Scriptlet variables declared in the parent page can be accessed in the included page (remember, they are the same page).
  • The included page does not need to able to be compiled as a standalone JSP. It can be a code fragment or plain text. The included page will never be compiled as a standalone. The included page can also have any extension, though .jspf has become a conventionally used extension.
  • One drawback on older containers is that changes to the include pages may not take effect until the parent page is updated. Recent versions of Tomcat will check the include pages for updates and force a recompile of the parent if they're updated.
  • A further drawback is that since the code is inlined directly into the service method of the generated servlet, the method can grow very large. If it exceeds 64 KB, your JSP compilation will likely fail.

<jsp:include> - The JSP Action tag on the other hand instructs the container to pause the execution of this page, go run the included page, and merge the output from that page into the output from this page.

  • Each included page is executed as a separate servlet at run time.
  • Pages can conditionally be included at run time. This is often useful for templating frameworks that build pages out of includes. The parent page can determine which page, if any, to include according to some run-time condition.
  • The values of scriptlet variables need to be explicitly passed to the include page.
  • The included page must be able to be run on its own.
  • You are less likely to run into compilation errors due to the maximum method size being exceeded in the generated servlet class.

Depending on your needs, you may either use <@include> or <jsp:include>


Main advantage of <jsp:include /> over <%@ include > is:

<jsp:include /> allows to pass parameters

<jsp:include page="inclusion.jsp">
    <jsp:param name="menu" value="objectValue"/>
</jsp:include>

which is not possible in <%@include file="somefile.jsp" %>


All three template options - <%@include>, <jsp:include> and <%@tag> are valid, and all three cover different use cases.

With <@include>, the JSP parser in-lines the content of the included file into the JSP before compilation (similar to a C #include). You'd use this option with simple, static content: for example, if you wanted to include header, footer, or navigation elements into every page in your web-app. The included content becomes part of the compiled JSP and there's no extra cost at runtime.

<jsp:include> (and JSTL's <c:import>, which is similar and even more powerful) are best suited to dynamic content. Use these when you need to include content from another URL, local or remote; when the resource you're including is itself dynamic; or when the included content uses variables or bean definitions that conflict with the including page. <c:import> also allows you to store the included text in a variable, which you can further manipulate or reuse. Both these incur an additional runtime cost for the dispatch: this is minimal, but you need to be aware that the dynamic include is not "free".

Use tag files when you want to create reusable user interface components. If you have a List of Widgets, say, and you want to iterate over the Widgets and display properties of each (in a table, or in a form), you'd create a tag. Tags can take arguments, using <%@tag attribute> and these arguments can be either mandatory or optional - somewhat like method parameters.

Tag files are a simpler, JSP-based mechanism of writing tag libraries, which (pre JSP 2.0) you had to write using Java code. It's a lot cleaner to write JSP tag files when there's a lot of rendering to do in the tag: you don't need to mix Java and HTML code as you'd have to do if you wrote your tags in Java.


Java Revisited

  1. Resource included by include directive is loaded during jsp translation time, while resource included by include action is loaded during request time.
  2. Any change on included resource will not be visible in case of include directive until jsp file compiles again. While in case of include action, any change in included resource will be visible in next request.
  3. Include directive is static import, while include action is dynamic import
  4. Include directive uses file attribute to specify resource to be included while include action use page attribute for same purpose.

참고URL : https://stackoverflow.com/questions/14580120/whats-the-difference-between-including-files-with-jsp-include-directive-jsp-in

반응형