EL语言(减少JSP页面中的Java代码)

String password = request.getParameter("password");

%>

username:

password:

username: ${param.username }

password: ${param.password }

属性范围->在EL中的名称

Page->pageScope;Request->requestScope;Session->sessionScope;Application->applicationScope.

此外,启动tomcat服务器,在浏览器中访问localhost:8080/examples,选择JSP Examples,其中给出了若干实例,也可以进行相关的学习。

首先创建标签处理类HelloTag.java:

package com.jsp.tag;

import javax.servlet.jsp.JspException;

import javax.servlet.jsp.JspTagException;

import javax.servlet.jsp.tagext.TagSupport;

public class HelloTag extends TagSupport

{

public HelloTag()

{

}

public int doStartTag() throws JspException

{

try

{

this.pageContext.getOut().print("nihao");

}

catch (Exception ex)

{

throw new JspTagException(ex.getMessage());

}

return EVAL_BODY_INCLUDE;

}

// Method called when the closing hello tag is encountered

public int doEndTag() throws JspException

{

try

{

// We use the pageContext to get a Writer

// We then print the text string Hello

this.pageContext.getOut().print("Hello");

}

catch (Exception e)

{

throw new JspTagException(e.getMessage());

}

return EVAL_PAGE;

}

public void release()

{

// Call the parent's release to release any resources

// used by the parent tag.

// This is just good practice for when you start creating

// hierarchies of tags.

super.release();

}

}

然后,创建标签库描述文件(在WEB-INF下新建一个tld(taglib description )文件,这里命名为mytaglib.tld):

/p>

PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.1//EN"

"http://java.sun.com/j2ee/dtds/web-jsptaglibrary_1_1.dtd">

1.0

1.1

mytaglib

/mytaglib

hello

com.jsp.tag.HelloTag

empty

然后,在hellowithtag1.jsp中引入标签库,然后插入标签:

tag library example

访问http://localhost:8080/test/hellowithtag1.jsp,输出nihaoHello

范例2:

创建一个能替换test应用中JSP网页的静态文本的标签,这个标签名为message,它放在mytaglib标签库中。

首先在WEB-INF下面放置一个静态文本messageresource.properties

hello.title = Tile of hello.jsp

hello.hello = Hello

然后通过一个DispatcherServlet装载:

package com.test.servlet;

import java.io.InputStream;

import java.util.Properties;

import javax.servlet.ServletConfig;

import javax.servlet.ServletContext;

import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;

public class DispatcherServlet extends HttpServlet

{

public void init(ServletConfig config) throws ServletException

{

super.init(config);

Properties ps = new Properties();

try

{

ServletContext context = config.getServletContext();

InputStream in = context

.getResourceAsStream("/WEB-INF/messageresource.properties");

ps.load(in);

in.close();

context.setAttribute("ps", ps);

}

catch (Exception e)

{

e.printStackTrace();

}

}

public void destroy()

{

}

}

接下来,是标签库的处理类MessageTag.java:

package com.jsp.tag;

import java.util.Properties;

import javax.servlet.jsp.JspException;

import javax.servlet.jsp.JspTagException;

import javax.servlet.jsp.PageContext;

import javax.servlet.jsp.tagext.TagSupport;

public class MessageTag extends TagSupport

{

private String key = null;

public MessageTag()

{

}

public String getKey()

{

return this.key;

}

public void setKey(String key)

{

this.key = key;

}

// Method called when the closing hello tag is encountered

public int doEndTag() throws JspException

{

try

{

Properties ps = (Properties) pageContext.getAttribute("ps",

PageContext.APPLICATION_SCOPE);

String message = (String) ps.get(key);

pageContext.getOut().print(message);

}

catch (Exception e)

{

throw new JspTagException(e.getMessage());

}

return EVAL_PAGE;

}

public void release()

{

super.release();

}

}

添加相关信息到标签库描述文件mytaglib.tld中:

/p>

PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.1//EN"

"http://java.sun.com/j2ee/dtds/web-jsptaglibrary_1_1.dtd">

1.0

1.1

mytaglib

/mytaglib

hello

com.jsp.tag.HelloTag

empty

message

com.jsp.tag.MessageTag

empty

key

true

最后,在hellowithtag2.jsp文件中引入标签库,然后插入标签:

为了在web应用启动时通过DispatcherServlet装载静态文本,应该在web.xml中配置这个Servlet时设置load-on-startup属性:

DispatcherServlet

com.test.servlet.DispatcherServlet

5

在MessageTag的doEndTag方法中,首先从pageContext中读取包含静态文本的Properties对象:

public int doEndTag() throws JspException

{

try

{

Properties ps = (Properties) pageContext.getAttribute("ps",

PageContext.APPLICATION_SCOPE);

String message = (String) ps.get(key);

pageContext.getOut().print(message);

}

catch (Exception e)

{

throw new JspTagException(e.getMessage());

}

return EVAL_PAGE;

}

然后从Properties对象中读取key对应的静态文本,最后输出该文本。

最后,访问http://localhost:8080/test/hellowithtag2.jsp,输出hello。

java 客户化排序_第八部分_客户化JSP标签相关推荐

  1. java选择排序解释_选择排序

    号内回复数据结构,获取整套算法视频 本文作者:skywang12345 欢迎点击下方阅读原文 选择排序介绍 选择排序(Selection sort)是一种简单直观的排序算法. 它的基本思想是:首先在未 ...

  2. java编写排序的代码_在Java 8之前,您编写了几行代码来对对象集合进行排序?...

    java编写排序的代码 在Java 8之前,您编写了几行代码来对对象集合进行排序? Java 8您需要多少个? 您可以在Java 8中用一行完成. 让我们看看下面的Employee类. public ...

  3. java取出字符串中的后四位_[原]Java面试题-将字符串中数字提取出来排序后输出...

    [Title][原]Java面试题-将字符串中数字提取出来排序后输出 [Date]2013-09-15 [Abstract]很简单的面试题,要求现场在纸上写出来. [Keywords]面试.Java. ...

  4. 20172311『Java程序设计』课程 结对编程练习_四则运算第一周阶段总结

    20172311『Java程序设计』课程 结对编程练习_四则运算第一周阶段总结 结对伙伴 学号 :20172307 姓名 :黄宇瑭 伙伴第一周博客地址: http://www.cnblogs.com/ ...

  5. java编写桌球游戏素材小球图片_你学不好Java还是有原因的!拿走这套Java系统教程,自学必备...

    很多同学都遇到过一种情况:就是无论自己怎么学Java,到头来发现学的都是皮毛,当真正去用python去做一个项目的时候,脑袋里面一片空白.完全不知道从何做起! 那是你没有系统的学过一次Java,现在的 ...

  6. java web主流框架整合开发 光盘_开发者突击:Java Web主流框架整合开发(第2版)(附CD光盘1张)...

    序言 一.了解软件架构 (一)基本概念 (二)软件架构的历史 (三)软件架构的目标 二.J2EE开发模型分析 (一)一层架构模式 (二)两层架构模式Model 1 (三)三层架构模式Model 2 ( ...

  7. 【源码+教程】Java课设项目_12款最热最新Java游戏项目_Java游戏开发_Java小游戏_飞翔的小鸟_王者荣耀_超级玛丽_推箱子_黄金矿工_贪吃蛇

    马上就要期末了,同学们课设做的如何了呢?本篇为大家带来了12款热门Java小游戏项目的源码和教程,助力大家顺利迎接暑假![源码+教程]Java课设项目_12款最热最新Java游戏项目_Java游戏开发 ...

  8. JAVA入门_继承与重载_饲养员喂养动物

    JAVA入门_继承与重载_饲养员喂养动物 实验要求 Tiger类 Feeder类 MainClass 运行结果 实验要求 本实验要求:本实验以饲养员喂养老虎为业务背景,体验"函数重载&quo ...

  9. Java视频教程(最适合Java初学者的经典入门视频)_讲 师:张孝祥

    下载地址:http://www.itsoba.com/Article.asp?id=849 下载地址:http://www.itsoba.com/Article.asp?id=849 视频介绍: 本视 ...

最新文章

  1. c10k问题及其解决方案
  2. javascript的原始类型(primitive type)之间的关系。
  3. python绘制3d图形-python matlibplot绘制3D图形
  4. 使用pos标记寻找三词短语
  5. rails表单控件helper
  6. SQL Server 2000卸载后重新安装的问题
  7. 穿越火线全部服务器都显示爆满,穿越火线大区全部爆满,频道挤不进去背后的故事!...
  8. 2014年度优秀员工代表发言稿
  9. 基础编程题之最近公共祖先
  10. 基于dijsktra算法的最短路径求解_基于dijkstra算法的AGV路径规划(含C++代码)
  11. jquery批量控制form禁用的代码
  12. G2 可视化引擎-统计图表
  13. Linux安装软件包时的“依赖关系树”算法(C#)
  14. osgi java web_基于 OSGi 和 Spring 开发 Web 应用
  15. 计算机网络技术用古诗文描述,【多媒体技术论文】多媒体技术在古诗文教学的运用(共2828字)...
  16. 搭建STM32开发环境
  17. 学习英文必记的九种前缀与三种后缀
  18. 阿里网盘阿里云盘----手机端PC端
  19. C++接口实现汉字拼音转换
  20. inline函数在IAR中的使用

热门文章

  1. socket网络编程多线程
  2. 给程序员的10条建议
  3. 从“嵌入式”到“物联网”有哪些变化?
  4. 基于SLS构建RDS审计合规监控
  5. 大数据的下一站是什么?服务/分析一体化
  6. 基于MaxCompute 衣二三帮助客户找到合适自己的衣服
  7. 小蜜团队万字长文 | 讲透对话管理模型最新研究进展
  8. 即将发版!Apache Flink 1.9 版本有哪些新特性?
  9. 亿级消息系统的核心存储:Tablestore发布Timeline 2.0模型
  10. 用Python玩转时序数据