日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

java 客户化排序_第八部分_客户化JSP标签

發(fā)布時間:2024/9/27 javascript 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java 客户化排序_第八部分_客户化JSP标签 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

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服務(wù)器,在瀏覽器中訪問localhost:8080/examples,選擇JSP Examples,其中給出了若干實例,也可以進(jìn)行相關(guān)的學(xué)習(xí)。

首先創(chuàng)建標(biāo)簽處理類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();

}

}

然后,創(chuàng)建標(biāo)簽庫描述文件(在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中引入標(biāo)簽庫,然后插入標(biāo)簽:

tag library example

訪問http://localhost:8080/test/hellowithtag1.jsp,輸出nihaoHello

范例2:

創(chuàng)建一個能替換test應(yīng)用中JSP網(wǎng)頁的靜態(tài)文本的標(biāo)簽,這個標(biāo)簽名為message,它放在mytaglib標(biāo)簽庫中。

首先在WEB-INF下面放置一個靜態(tài)文本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()

{

}

}

接下來,是標(biāo)簽庫的處理類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();

}

}

添加相關(guān)信息到標(biāo)簽庫描述文件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文件中引入標(biāo)簽庫,然后插入標(biāo)簽:

為了在web應(yīng)用啟動時通過DispatcherServlet裝載靜態(tài)文本,應(yīng)該在web.xml中配置這個Servlet時設(shè)置load-on-startup屬性:

DispatcherServlet

com.test.servlet.DispatcherServlet

5

在MessageTag的doEndTag方法中,首先從pageContext中讀取包含靜態(tài)文本的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對應(yīng)的靜態(tài)文本,最后輸出該文本。

最后,訪問http://localhost:8080/test/hellowithtag2.jsp,輸出hello。

總結(jié)

以上是生活随笔為你收集整理的java 客户化排序_第八部分_客户化JSP标签的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。