Tag文件使用

Tag文件和JSP文件很类似,可以被JSP文件动态加载调用,但是用户不能直接访问.

Tag文件的存储目录:

    Tag文件规定放在/WEB-INF/tags目录或其子目录下,文件名随意.

Tag文件的使用:

<%@ taglib prefix="tags" tagdir="/WEB-INF/tags" %>

Tag指令:

1.body-content 属性:设置Tag标记标记体的情况.可取的值:

  tagdependent:标签体内容直接被写入BodyContent,由自定义标签类来进行处理,而不被JSP容器解释.

  jSP:接受所有JSP语法,如定制的或内部的tag、scripts、静态HTML、脚本元素、JSP指令和动作。

  empty:空标记,即起始标记和结束标记之间没有内容。

  scriptless:接受文本、EL和JSP动作,默认取值.

2.pageEncoding 属性:设置tag文件的编码: 

<%@ tag language="java" pageEncoding="UTF-8"%>

Include 指令:

  在Tag文件中可以使用这个指令包含其他JSP文件或Tag文件.

attribute 指令:

    用于向Tag标记中传递参数:

        name:参数名

        required:参数是否必须,默认false

        type:参数类型,默认String,若手动指定要叫上包名.

variable 指令:

    用于返回对象(一般是处理结果)

        name-given 属性:指定返回对象的变量名

        variable-class 属性:指定返回对象的类型,默认 java.lang.String,此处必须填完整名称.

        scope 属性:指定对象的有效范围,可取的值有:

            NESTED:对象仅在Tag标记的标记体中有效,默认取值.

            AT_BEGIN:一旦开始使用Tag标记,此对象就有效.

            AT_END:只有在Tag标记结束后此对象才有效.

<%@ variable name-given="result" scope="AT_BEGIN" variable-class="java.lang.String"%>
jspContext.setAttribute("result", new Object());

使用例子:

在使用页面上使用的部分代码

<%@ taglib prefix="tags" tagdir="/WEB-INF/tags" %>

<div>
    <tags:message content="${message}"/>
</div>

message.tag文件内容

 1 <%@ tag language="java" pageEncoding="UTF-8"%>
 2 <%@ include file="/WEB-INF/views/include/taglib.jsp"%>
 3 <%@ attribute name="content" type="java.lang.String" required="true" description="消息内容"%>
 4 <%@ attribute name="type" type="java.lang.String" description="消息类型:info、success、warning、error、loading"%>
 5 <script type="text/javascript">top.$.jBox.closeTip();</script>
 6 <c:if test="${not empty content}">
 7     <c:if test="${not empty type}"><c:set var="ctype" value="${type}"/></c:if><c:if test="${empty type}"><c:set var="ctype" value="${fn:indexOf(content,'失败') eq -1?'success':'error'}"/></c:if>
 8     <div id="messageBox" class="alert alert-${ctype} hide">
 9     <button data-dismiss="alert" class="close">×</button>
10     ${content}
11    </div> 
12     <script type="text/javascript">if(!top.$.jBox.tip.mess){top.$.jBox.tip.mess=1;top.$.jBox.tip("${content}","${ctype}",{persistent:true,opacity:0});$("#messageBox").show();}</script>
13 </c:if>

后台传回去的数据关键代码

 1 /**
 2  * 添加Flash消息
 3  * @param message
 4  */
 5 protected void addMessage(RedirectAttributes redirectAttributes, String... messages) {
 6     StringBuilder sb = new StringBuilder();
 7     for (String message : messages){
 8         sb.append(message).append(messages.length>1?"<br/>":"");
 9     }
10     redirectAttributes.addFlashAttribute("message", sb.toString());
11 }
12 
13 
14 
15 
16 @RequestMapping(value = "assignUserPostRole")
17 public String assignUserPostRole(Long id, RedirectAttributes redirectAttributes) {
18     if (UserUtils.getUser().getId().equals(id)) {
19         addMessage(redirectAttributes, "初始用户角色失败, 不允许初始当前用户");
20     } else if (User.isAdmin(id)) {
21         addMessage(redirectAttributes, "初始用户角色失败, 不允许初始超级管理员用户");
22     } else {
23         systemService.assignUserPostRole(systemService.getUser(id));
24         addMessage(redirectAttributes, "初始用户角色成功");
25     }
26     return "redirect:" + Global.getAdminPath() + "/sys/user/?repage";
27 }

后台返回一个message信息

原文地址:https://www.cnblogs.com/move22/p/7325091.html