自定义JSP标签
来源:互联网 发布:icp备案后怎么使用域名 编辑:程序博客网 时间:2024/06/04 19:54
1.XML配置
<jsp-config> <taglib> <taglib-uri>/WEB-INF/tld/cmsTagLib</taglib-uri> <taglib-location>/WEB-INF/tld/cmsTagLib.tld</taglib-location> </taglib> </jsp-config>
2.编写/WEB-INF/tld/cmsTagLib.tld
<?xml version="1.0" encoding="UTF-8"?><taglib><tlib-version>1.0</tlib-version><jsp-version>1.2</jsp-version><tag> <name>action</name> <tag-class>com.oemp.cmstemplate.util.CmsTemplateTag</tag-class> <body-content>empty</body-content> <attribute> <name>value</name> <required>false</required> <rtexprvalue>true</rtexprvalue> </attribute></tag><tag> <name>content</name> <tag-class>com.oemp.cmstemplate.util.CmsTemplateContentTag</tag-class> <body-content>jsp</body-content></tag></taglib>
3.实现类
package com.oemp.cmstemplate.util;import java.lang.reflect.Method;import javax.servlet.jsp.JspException;import javax.servlet.jsp.JspTagException;import javax.servlet.jsp.JspWriter;import javax.servlet.jsp.tagext.TagSupport;import com.oemp.cmstemplate.struts2.CmsTemplateAction;import com.util.tools.UtilTools;/** * 自定义标签 */public class CmsTemplateTag extends TagSupport {private static final long serialVersionUID = 1L;private String value = "";public int doStartTag() throws JspException {try {JspWriter out = pageContext.getOut(); String val=value!=null&&value.length()>2?value.substring(0,3):value;if (value.equals("date")) {out.print(UtilTools.getDate());}else if(val.equals("get")){//自定义内容String className = "com.oemp.cmstemplate.struts2.CmsTemplateAction";try {Object obj = Class.forName(className).newInstance();Method method = obj.getClass().getMethod(value);//如果有参数的方法,getMethod后面可以再加一个数组参数out.print(method.invoke(obj));} catch (IllegalAccessException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}}else {CmsTemplateAction cms = new CmsTemplateAction();out.print(cms.getContent(value));}} catch (java.io.IOException e) {throw new JspTagException(e.getMessage());}return SKIP_BODY;}public String getValue() {return value;}public void setValue(String value) {this.value = value;}}
第二种标签:
package com.oemp.cmstemplate.util;import java.io.IOException;import javax.servlet.http.HttpServletRequest;import javax.servlet.jsp.JspTagException;import javax.servlet.jsp.tagext.BodyTagSupport;import com.util.tools.UtilTools;/** * 自定义内容标签 */public class CmsTemplateContentTag extends BodyTagSupport {private static final long serialVersionUID = 1L;public int doStartTag() throws JspTagException {return EVAL_BODY_BUFFERED;}public int doEndTag() throws JspTagException {String body = this.getBodyContent().getString();HttpServletRequest request = (HttpServletRequest) pageContext.getRequest(); //页面中的自定义标签替换<cms:content> Date: $date</cms:content>body = body.replace("$date", UtilTools.getDate());try {pageContext.getOut().print(body);} catch (IOException e) {throw new JspTagException(e.getMessage());}return SKIP_BODY;}}
相关类:
package com.oemp.cmstemplate.struts2;import java.util.HashMap;import java.util.Map;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import com.oemp.cmstemplate.service.CmsTemplateInfoService;import com.util.tools.UtilTools;public class CmsTemplateAction {private CmsTemplateInfoService tempInfoService; /** * 替换标签内容 * @param value * @return */public String getContent(String value) {Map<String,String> param = new HashMap<String,String>(); param.put("ctlName", value); Map<String, String> map= new HashMap<String,String>(); String content = ""; try { ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml"); tempInfoService=(CmsTemplateInfoService)ctx.getBean(CmsTemplateInfoService.class); map = tempInfoService.getTempAction(param); content = UtilTools.isEmpty(map.get("ctlContent"))?"":map.get("ctlContent"); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } return content;} /** * 得到会员信息 * @return */public String getMember() {String content = "中华人民共和国";// TODO Auto-generated method stubreturn content;}}
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%@taglib uri="/WEB-INF/tld/cmsTagLib" prefix="cms"%><%String path = request.getContextPath();%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html> <head> <title>starting page</title> </head> <body> This is my JSP page. <br> Date : <%= new java.util.Date().toString() %> <br/> File : <input value="<%= request.getServletPath() %>" /> <br/> ============================================================ <br/> Date : <cms:action value="date" /><br> File : <cms:action value="mobile_public_login" /> <br/> Method:<cms:action value="getMember" /><br> <hr> <cms:content> Date: $date<br> File: $filename </cms:content> </body></html>
0 0
- JSP----自定义标签---简单标签
- jsp自定义标签-----嵌套标签
- JSP标签编程----自定义标签
- JSP 自定义标签:简单标签
- JSP标签-JSTL-自定义标签
- JSP自定义标签-Select标签
- jsp 自定义标签---标签文件
- JSP自定义标签
- JSP自定义标签学习心得
- JSP自定义标签试验
- JSP 自定义标签库
- 自定义JSP标签
- 创建JSP自定义标签
- 关于自定义标签 jsp
- 关于自定义标签 jsp
- jsp 自定义分页标签
- JSP自定义标签简介
- JSP自定义标签例子
- 使用VS调试Unity脚本
- android studio 中查看数据库
- php之文件上传
- JSP禁用缓存的方式 response.setHeader( "Pragma", "no-cache" ); setDateHeader("Expires", 0);的用法和什么意思
- 请使用/main进行编译,以指定包含入口点类型
- 自定义JSP标签
- python新手
- linux shell脚本登录远程执行命令,交互通过脚本
- Java编程小技巧
- sql语句批量修改字段
- 自定义Dialog并实现接口回调
- Android 软键盘遮挡的四种解决方案
- ORACLE禁用启用触发器
- caffe 中 protobuffer