超碰免费人人操|国产视频二区久久艹人人操|欧美激情第一页在线|久热最新无码中文视频|91精品国际成人|亚洲成人精品在线视频青青草|久草免费高清完整在线观看|你懂的AV在线日本黄网页|国产黄色AV日韩女同网|欧美成人色区导航片av

Java自定義簡單標簽實例

時間:2025-10-22 03:22:41 java語言

Java自定義簡單標簽實例

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

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

  一、標簽類型

  步驟:

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

  package cn.com.liveuc.privilege.tag;

  import java.io.IOException;

  import java.util.ArrayList;

  import java.util.List;

  import java.util.Set;

  import javax.servlet.jsp.JspException;

  import javax.servlet.jsp.PageContext;

  import javax.servlet.jsp.tagext.SimpleTagSupport;

  import cn.com.liveuc.privilege.model.Privilege;

  import cn.com.liveuc.privilege.model.Resource;

  import cn.com.liveuc.privilege.model.Role;

  import cn.com.liveuc.privilege.model.User;

  /**

  *

  * @說明 自定義標簽

  */

  public class PerssionTag extends SimpleTagSupport {

  /pic/p>

  private String uri;

  /pic/p>

  public void setUri(String uri) {

  this.uri = uri;

  }

  @Override

  public void doTag() throws JspException, IOException {

  /pic/p>

  PageContext page = (PageContext) this.getJspContext();

  User user = (User) page.getSession().getAttribute("login");

  /pic/p>

  if(user != null) {

  /pic/p>

  List list = new ArrayList();

  /pic/p>

  Set role = user.getRole();

  for(Role r:role) {

  /pic/p>

  Set privilege = r.getPrivilege();

  for(Privilege p:privilege) {

  /pic/p>

  Set res = p.getResource();

  for(Resource re:res) {

  list.add(re.getUri());

  }

  }

  }

  for(String ur:list) {

  /pic/p>

  if(ur.equals(uri)) {

  this.getJspBody().invoke(null); /pic/p>

  }

  }

  }

  }

  }

  2.在WEB-INF下創(chuàng)建tld文件描述標簽。

  復制代碼 代碼如下:

  version="2.0"

  xsi:schemaLocation="/pic/xml/ns/j2ee /pic/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd">

  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

  cn.com.liveuc.privilege.tag.PerssionTag

  scriptless

  uri

  true

  true

  3.運用標簽

  在Jsp頁面導入標簽:

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

  運用標簽:

  用戶管理

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

【Java自定義簡單標簽實例】相關文章:

php自定義錯誤日志實例詳解12-15

Java中synchronized的使用實例10-06

java動態(tài)方法調度實例09-29

Java中的Spring入門實例02-27

Java for循環(huán)語句使用實例01-13

Java鍵盤輸入實例10-31

Java實現多繼承的實例12-11

JAVA語言中的實例變量12-15

java調用c函數的實例12-20