糯米文學吧

位置:首頁 > 計算機 > java語言

Java自定義簡單標籤實例

java語言9.26K

引導語:當jsp的內置標籤和jstl標籤庫內的標籤都滿足不了需求,這時候就需要開發者自定義標籤。以下是本站小編分享給大家的Java自定義簡單標籤實例,歡迎閲讀!

Java自定義簡單標籤實例

下面將以權限的控制為例自定義一個標籤:

一、標籤類型

步驟:

1.自定義一個類PerssionTag 繼承SimpleTagSupport(自定義標籤一般都會繼承這個類)

package ;

import ception;

import yList;

import ;

import ;

import xception;

import Context;

import leTagSupport;

import ilege;

import urce;

import ;

import ;

/**

*

* @説明 自定義標籤

*/

public class PerssionTag extends SimpleTagSupport {

//自定義標籤屬性,用於標籤傳入參數

private String uri;

//接收標籤傳入的參數

public void setUri(String uri) {

= uri;

}

@Override

public void doTag() throws JspException, IOException {

//獲取用户登陸後保存的ion

PageContext page = (PageContext) spContext();

User user = (User) ession()ttribute("login");

//如果用户登陸

if(user != null) {

//用户登陸判斷用户權限

List list = new ArrayList();

//獲取用户的角色

Set role = ole();

for(Role r:role) {

//獲取角色對應的權限

Set privilege = rivilege();

for(Privilege p:privilege) {

//獲取權限對應的資源

Set res = esource();

for(Resource re:res) {

(ri());

}

}

}

for(String ur:list) {

//判斷用户的權限

if(ls(uri)) {

spBody()ke(null); //有權限輸出標籤體內容

}

}

}

}

}

 2.在WEB-INF下創建tld文件描述標籤。

複製代碼 代碼如下:

version="2.0"

xsi:schemaLocation=" _2_">

the Apache Struts framework includes a library of custom tags.

The tags interact with the framework's validation and internationalization features;

to ensure that input is correct and output is localized.

The Struts Tags can be used with JSP FreeMarker or Velocity."]]>

"Struts Tags"

2.2.3

s

/wxt

per

sionTag

scriptless

uri

true

true

 3.運用標籤

在Jsp頁面導入標籤:

%@taglib prefix="wxt" uri="/wxt" %

運用標籤:

用户管理

用户權限包含uri資源的將會輸出標籤內容。

標籤:自定義 JAVA 實例