博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
struts custom tag
阅读量:7224 次
发布时间:2019-06-29

本文共 3185 字,大约阅读时间需要 10 分钟。

hot3.png

1、<%@ taglib uri="/struts-layout-tags" prefix="layout"%>中struts-layout-tags类

yield
com.fangdo.core.view.YieldTag
JSP
id
false
true
value
false
true

2、yieldTag类

继承ComponentTagSupport类是为了获得JSP页面中用户自定义的标签中设置的属性值,并包装成Component对象。
Component:
继承Component类是为了从Struts2中的ValueStack中获得相对应的值。

public class YieldTag extends ComponentTagSupport {	protected String value;		public Component getBean(ValueStack stack, HttpServletRequest req, HttpServletResponse res) {		return new Yield(stack, req, res);	}//protected void populateParams()此方法是用来将JSP页面传来的属性值赋给基本类。这里是继承来的,所以首先调用super的相应方法。 	protected void populateParams() {		super.populateParams();		((Yield) component).setValue(value);	}	public void setValue(String value) {		this.value = value;	}}

3、Yield类 

 重写start,end等方法即可。

public class Yield extends Component {		private static boolean encodingDefined = true;	private static String encoding;        private static String defaultEncoding;    	private String value;	private HttpServletRequest req;	private HttpServletResponse res;		public Yield(ValueStack stack,HttpServletRequest req,HttpServletResponse res){		super(stack);		this.req = req;		this.res = res;	}    @Inject(StrutsConstants.STRUTS_I18N_ENCODING)    public static void setDefaultEncoding(String encoding) {        defaultEncoding = encoding;    }        private static String getEncoding() {        if (encodingDefined) {            try {                encoding = defaultEncoding;            } catch (IllegalArgumentException e) {                encoding = System.getProperty("file.encoding");                encodingDefined = false;            }        }        return encoding;    }    	@Override	public boolean end(Writer writer, String body) {				String name = StringUtils.isBlank(value) ? "content_for_layout" : "content_for_" + value;		FastByteArrayOutputStream content = (FastByteArrayOutputStream) req.getAttribute(name);		if(content !=null){			String encoding = getEncoding();			try{				if (encoding != null) {				content.writeTo(writer, encoding);	        } else {	        	content.writeTo(writer,null);	        }			} catch(IOException e) {				LogFactory.getLog(getClass()).warn("Exception thrown during ", e);			}		}		return super.end(writer, body);	}	public void setValue(String value) {		this.value = value;	}	}

 4、jsp中

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%><%@ taglib uri="/struts-layout-tags" prefix="layout"%><% String rootPath = request.getContextPath(); %>
<layout:yield value="title"/>。。。
south region
>

转载于:https://my.oschina.net/winHerson/blog/49722

你可能感兴趣的文章
Linux命令——pidof
查看>>
关于Linq2Sql有外键表的更新--引发的问题。
查看>>
解决linux 无法下载 oracle 官网 java的 安装包
查看>>
nodejs stream 手册学习
查看>>
go标准库的学习-strconv-字符串转换
查看>>
Beta 冲刺(7/7)
查看>>
nginx跟tp5无法加载控制器
查看>>
PHP Ctype函数
查看>>
cordova(PhoneGap)简单应用
查看>>
在Linux系统安装Nodejs 最简单步骤
查看>>
Delphi Union 使用
查看>>
apache tomcat 集群!
查看>>
一文读懂网络协议
查看>>
iOS开发小技巧--实现毛玻璃效果的方法
查看>>
bzoj 3529: [Sdoi2014]数表
查看>>
CF每日一练(2.11)
查看>>
operator ->
查看>>
react select
查看>>
JDBC 编程初步
查看>>
数据库SQL归纳(一)
查看>>