jsp taglib指令_JSP指令–页面,包含和taglib示例

jsp taglib指令

JSP Directives are used to give special instruction to container for translation of JSP to Servlet code. JSP Directives are placed between <%@ %>.

JSP指令用于为容器提供特殊指令,以将JSP转换为Servlet代码。 JSP指令位于<%@ %>

JSP指令 (JSP Directives)

jsp directives, jsp page directive, jsp taglib, jsp include

JSP provides three directives for us to use;


JSP提供了三个指令供我们使用。

  1. JSP page directive

    JSP页面指令
  2. JSP include directive

    JSP包含指令
  3. JSP taglib directive

    JSP taglib指令

Every jsp directive has a set of attributes to provide specific type of instructions. So usually a JSP directive will look like

每个jsp指令都有一组属性来提供特定类型的指令。 因此,通常JSP指令看起来像

<%@ directive attribute="value" %>

.

If you have directly landed here, you might want to check out JSP Tutorial for Beginners and JSP Implicit Objects.

如果您直接登陆这里,则可能要查看《初学者的JSP教程》和《 JSP隐式对象》

JSP页面指令 (JSP page directive)

page directive provides attributes that get applied to the entire JSP page. page directive has a lot of attributes that we will look at now. We can define multiple attributes in a single page directive or we can have multiple page directives in a single JSP page.

page指令提供了应用于整个JSP页面的属性。 page指令具有许多我们现在要看的属性。 我们可以在单个页面指令中定义多个属性,也可以在单个JSP页面中具有多个页面指令。

  1. import attribute: This is one of the most used page directive attribute. It’s used to instruct container to import other java classes, interfaces, enums etc. while generating servlet code. This is similar to import statements in java classes, interfaces. An example of import page directive usage is:
    <%@ page import="java.util.Date,java.util.List,java.io.*" %>

    import属性 :这是最常用的页面指令属性之一。 它用于指示容器在生成Servlet代码时导入其他Java类,接口,枚举等。 这类似于java类,接口中的import语句。 导入页面指令用法的一个示例是:
  2. contentType attribute: This attribute is used to set the content type and character set of the response. The default value of contentType attribute is”text/html; charset=ISO-8859-1″. We can use it like below.
    <%@ page contentType="text/html; charset=US-ASCII" %>

    contentType属性 :此属性用于设置响应的内容类型和字符集。 contentType属性的默认值为“ text / html; charset = ISO-8859-1“。 我们可以像下面这样使用它。
  3. pageEncoding attribute: We can set response encoding type with this page directive attribute, its default value is “ISO-8859-1”.
    <%@ page pageEncoding="US-ASCII" %>

    pageEncoding属性 :我们可以使用此page指令属性设置响应编码类型,其默认值为“ ISO-8859-1”。
  4. extends attribute: This attribute is used to define the super class of the generated servlet code. This is very rarely used and we can use it if we have extended HttpServlet and overridden some of it’s implementations. For example;
    <%@ page extends="org.apache.jasper.runtime.HttpJspBase" %>

    extend属性 :此属性用于定义生成的servlet代码的超类。 这很少使用,如果扩展了HttpServlet并覆盖了其中的某些实现,则可以使用它。 例如;
  5. info attribute: We can use this attribute to set the servlet description and we can retrieve it using Servlet interface getServletInfo() method. For example;
    <%@ page info="Home Page JSP" %>

    info属性 :我们可以使用此属性来设置servlet描述,并可以使用Servlet接口getServletInfo()方法检索它。 例如;
  6. buffer attribute: We know that JspWriter has buffering capabilities, we can use this attribute to set the buffer size in KB to handle output generated by JSP page. Default value of buffer attribute is 8kb. We can define 16 KB buffer size as;
    <%@ page buffer="16kb" %>

    buffer属性 :我们知道JspWriter具有缓冲功能,我们可以使用此属性设置缓冲区大小(以KB为单位),以处理JSP页面生成的输出。 缓冲区属性的默认值为8kb。 我们可以将16 KB的缓冲区大小定义为:
  7. language attribute: language attribute is added to specify the scripting language used in JSP page. It’s default value is “java” and this is the only value it can have. May be in future, JSPs provide support to include other scripting languages like C++ or PHP too.
    <%@ page language="java" %>

    language属性 :语言属性已添加,以指定JSP页面中使用的脚本语言。 它的默认值是“ java”,这是它唯一的值。 可能在将来,JSP也提供支持以包括其他脚本语言,例如C ++或PHP。
  8. isELIgnored attribute: We can ignore the Expression Language (EL) in JSP using this page directive attribute. Its datatype is Java Enum and default value is false, so EL is enabled by default. We can instruct container to ignore EL using below directive;
    <%@ page isELIgnored="true" %>

    isELIgnored属性 :使用此页面指令属性,我们可以忽略JSP中的表达式语言(EL)。 其数据类型为Java Enum ,默认值为false ,因此默认情况下启用EL。 我们可以使用以下指令指示容器忽略EL;
  9. isThreadSafe attribute: We can use this attribute to implement SingleThreadModel interface in generated servlet. Its an Enum with default value as true. If we set it’s value to false, the generated servlet will implement SingleThreadModel and eventually we will loose all the benefits of servlet multi-threading features. You should never set it’s value to false.
    <%@ page isThreadSafe="false" %>

    isThreadSafe属性 :我们可以使用此属性在生成的servlet中实现SingleThreadModel接口。 一个枚举,默认值为true。 如果将其值设置为false,则生成的servlet将实现SingleThreadModel,最终我们将失去servlet 多线程功能的所有优势。 您永远不要将其值设置为false。
  10. errorPage attribute: This attribute is used to set the error page for the JSP, if the JSP throws exception, the request is redirected to the error handler defined in this attribute. It’s datatype is URI. For example;
    <%@ page errorPage="errorHandler.jsp" %>

    errorPage属性 :此属性用于设置JSP的错误页面,如果JSP引发异常,则请求将重定向到此属性中定义的错误处理程序。 它的数据类型是URI。 例如;
  11. isErrorPage attribute: This attribute is used to declare that current JSP page is an error page. It’s of type Enum and default value is false. If we are creating an error handler JSP page for our application, we have to use this attribute to let container know that it’s an error page. JSP implicit attribute exception is available only to the error page JSPs. For example;
    <%@ page isErrorPage="true" %>

    isErrorPage属性 :此属性用于声明当前JSP页面是错误页面。 它是Enum类型,默认值为false。 如果要为我们的应用程序创建错误处理程序JSP页面,则必须使用此属性使容器知道它是错误页面。 JSP隐式属性异常仅适用于错误页面JSP。 例如;
  12. autoFlush attribute: autoFlush attribute is to control the buffer output. Its default value is true and output is flushed automatically when the buffer is full. If we set it to false, the buffer will not be flushed automatically and if it’s full, we will get an exception for buffer overflow. We can use this attribute when we want to make sure that the JSP response is sent in full or none. For example;
    <%@ page autoFlush="false" %>

    autoFlush属性 :autoFlush属性用于控制缓冲区的输出。 其默认值为true,并且在缓冲区已满时自动刷新输出。 如果将其设置为false,则不会自动刷新缓冲区,如果缓冲区已满,则会获得缓冲区溢出的异常。 当我们要确保完全或完全不发送JSP响应时,可以使用此属性。 例如;
  13. session attribute: By default JSP page creates a session but sometimes we don’t need session in JSP page. We can use this attribute to indicate compiler to not create session by default. It’s default value is true and session is created. To disable the session creation, we can use it like below.
    <%@ page session ="false" %>

    session属性 :默认情况下,JSP页面创建一个会话,但是有时我们不需要JSP页面中的会话。 我们可以使用此属性来指示编译器默认情况下不创建会话。 默认值为true,并创建会话。 要禁用会话创建,我们可以像下面这样使用它。
  14. trimDirectiveWhitespaces attribute: This attribute was added in JSP 2.1 and used to strip out extra white spaces from JSP page output. Its default value is false. It helps in reducing the generated code size, notice the generate servlet code keeping this attribute value as true and false. You will notice no out.write("\n") when it’s true.
    <%@ page trimDirectiveWhitespaces ="true" %>

    trimDirectiveWhitespaces属性 :此属性在JSP 2.1中添加,用于从JSP页面输出中去除多余的空格。 其默认值为false。 它有助于减少生成的代码大小,注意生成的servlet代码将此属性值保持为true和false。 您会发现没有true的out.write("\n")

JSP包含指令 (JSP include directive)

JSP include directive is used to include the contents of another file to the current JSP page. The included file can be HTML, JSP, text files etc. Include directive is very helpful in creating templates for user views and break the pages into header, footer, sidebar sections.

JSP include指令用于将另一个文件的内容包含到当前JSP页面中。 包含的文件可以是HTML,JSP,文本文件等。include指令在为用户视图创建模板并将页面分为页眉,页脚,侧边栏部分时非常有用。

We can include any resources in the JSP page like below.

我们可以在JSP页面中包括任何资源,如下所示。

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

The file attribute value should be the relative URI of the resource from the current JSP page.

文件属性值应该是当前JSP页面中资源的相对URI。

JSP taglib指令 (JSP taglib directive)

JSP taglib directive is used to define a tag library with the prefix that we can use in JSP, we will look into more details in the JSP Custom Tags tutorial.

JSP taglib指令用于定义一个带有我们可以在JSP中使用的前缀的标签库,我们将在JSP Custom Tags教程中研究更多细节。

We can define JSP tag libraries is like below;

我们可以定义JSP标签库,如下所示;

<%@ taglib uri="/WEB-INF/c.tld" prefix="c"%>

JSP taglib directive is used in JSP standard tag libraries, please read JSTL Tutorial.

JSP标准标签库中使用了JSP taglib指令,请阅读JSTL教程

That’s all for JSP directives, we will look into Expression Language (EL) and JSP Actions next.

这就是JSP指令的全部内容,接下来我们将研究表达式语言(EL)和JSP动作。

Update: JSP EL and JSP Action Tags articles are live now.

更新JSP ELJSP动作标签文章现已上线。

翻译自: https://www.journaldev.com/2044/jsp-directives-page-include-taglib-example

jsp taglib指令

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值