zoukankan      html  css  js  c++  java
  • 第四周作业

    jsp中自定义标签库之HelloWorld

     
    在项目中需要用到自定义标签库,它是JSP的插件,在网上查了些资料,发现版本不同,就自已做了实验,在netbeans6.9中进行开发,自定义标签库的开发步骤如下:
    1.编写标签库的java类MyTags.java
    import java.io.IOException;
    import javax.servlet.jsp.JspException;
    import javax.servlet.jsp.JspWriter;
    import javax.servlet.jsp.tagext.TagSupport;
     
    public class MyTags extends TagSupport {
     
        public int doStartTag() throws JspException {
            JspWriter out = pageContext.getOut();
            try {
                out.println("Hello,World"); //页面中显示的内容
            } catch (IOException e) {
                throw new JspException(e);
            }
            return SKIP_BODY; //不包含主体内容
        }
    }
     
    2.编写标记库描述符taglibs.tld
    <?xml version="1.0" encoding="UTF-8"?>
    <taglib version="2.1" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-jsptaglibrary_2_1.xsd">
      <tlib-version>1.0</tlib-version>
      <short-name>taglibs</short-name>
      <uri> http://davychen.cvv.shop.com/taglibs.tld</uri>
      <tag>
          <name>tags</name>
          <tag-class>MyTags</tag-class>
      </tag>
      <!-- A validator verifies that the tags are used correctly at JSP
             translation time. Validator entries look like this: 
          <validator>
              <validator-class>com.mycompany.TagLibValidator</validator-class>
              <init-param>
                 <param-name>parameter</param-name>
                 <param-value>value</param-value>
     </init-param>
          </validator>
       -->
      <!-- A tag library can register Servlet Context event listeners in
            case it needs to react to such events. Listener entries look
            like this: 
         <listener>
             <listener-class>com.mycompany.TagLibListener</listener-class> 
         </listener>
       -->
    </taglib>
     
    3.在web.xml中添加配置
    <!-- 新版本中不需要配置 -->
        <jsp-config>
            <taglib>
                <taglib-uri>
                    http://davychen.cvv.shop.com/taglibs.tld
                </taglib-uri>
                <taglib-location>
                        /WEB-INF/tlds/taglibs.tld
                </taglib-location>
            </taglib>
        </jsp-config>
     
    4.在welcome.jsp中使用
    <%@ page contentType="text/html; charset=UTF-8" language="java" errorPage="" %>
    <%@ taglib uri="http://davychen.cvv.shop.com/taglibs.tld" prefix="cvvout" %>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
        "http://www.w3.org/TR/html4/loose.dtd">
     
    <html>
        <head>
            <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
            <title>JSP Page</title>
        </head>
        <body>
            <h1>Hello World!</h1>
            <cvvout:tags/>
        </body>
    </html>
     

    Hello类叫做管理bean类,它为facelets页面表达式所使用的name属性提供了getter和setter方法,默认该facelets页面表达式引用的是Hello类的名字,不过第一个字母是小写字母(例如:hello.name)。

            如果你使用的是默认的bean类的类名,你注解可以用@Model来替代@Named和@RequestScoped。@Model注释称为原型,是一个包含其他注释的注释的术语。

           在 Hello.java类中,注解javax.inject.Named和javax.enterprise.context.RequestScoped使用请求scope来标识Hello类为管理bean类。scope定义应用程序数据是如何保存和共享的。

          在JSF中最常用的scope如下:

                     Request(@RequestScoped):请求scope在Web应用程序中的单个HTTP请求期间仍然存在。像hello1应用,该应用由单个请求和响应组成,bean使用请求scope。

                     Session (@SessionScoped):会话scope持续存在于Web应用程序中的多个HTTP请求中。当应用程序包含需要维护数据的多个请求和响应时,bean使用会话scope。 

                     Application (@ApplicationScoped):应用程序scope在所有用户与Web应用程序的交互中持久存在。

  • 相关阅读:
    DEBIAN下中文显示
    SpringMVC整合Quartz实现定时任务以及Tomcat服务执行初始化定时任务
    SpringMVC 配置定时执行任务
    Mybatis update In
    mybatis在xml文件中处理大于号小于号的方法
    解决Cannot change version of project facet Dynamic web module to 2.5
    android 圆角边框及图片
    Android MotionEvent事件响应机制
    android:configChanges属性
    Android之ScrollView嵌套ListView
  • 原文地址:https://www.cnblogs.com/yhyh/p/8759256.html
Copyright © 2011-2022 走看看