Tag文件和JSP文件很类似,可以被JSP文件动态加载调用,但是用户不能直接访问.
Tag文件的存储目录:
Tag文件规定放在/WEB-INF/tags目录或其子目录下,文件名随意.
Tag文件的使用:
Tag指令:
1.body-content 属性:设置Tag标记标记体的情况.可取的值:
tagdependent:标签体内容直接被写入BodyContent,由自定义标签类来进行处理,而不被JSP容器解释.
jSP:接受所有JSP语法,如定制的或内部的tag、scripts、静态HTML、脚本元素、JSP指令和动作。
empty:空标记,即起始标记和结束标记之间没有内容。
scriptless:接受文本、EL和JSP动作,默认取值.
2.pageEncoding 属性:设置tag文件的编码:
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标记结束后此对象才有效.
jspContext.setAttribute("result", new Object());
使用例子:
在使用页面上使用的部分代码
message.tag文件内容
1
2
3
4
5
6
7
8
9 ×
10 ${content}11
12
13
后台传回去的数据关键代码
1
5 protected voidaddMessage(RedirectAttributes redirectAttributes, String... messages) {6 StringBuilder sb = newStringBuilder();7 for(String message : messages){8 sb.append(message).append(messages.length>1?"
":"");9 }10 redirectAttributes.addFlashAttribute("message", sb.toString());11 }12
13
14
15
16 @RequestMapping(value = "assignUserPostRole")17 publicString 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信息