struts 标签

这两天看了下struts标签

 

蛋疼呀。。。如果不是必要。还是不要用struts标签。。。哎!

 

先说下struts标签的继承

 

BodyTagSupport

        StrutsBodyTagSupport

                ComponentTagSupport

                         AbstractUITag

 

每个struts标签都继承AbstractUITag

 

重点是ComponentTagSupport:

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import javax.servlet.jsp.JspException;

import org.apache.struts2.components.Component;

import org.apache.struts2.dispatcher.Dispatcher;

import com.opensymphony.xwork2.inject.Container;

import com.opensymphony.xwork2.util.ValueStack;

public abstract class ComponentTagSupport extends StrutsBodyTagSupport {

	protected Component component;

	// 定义一个组件(虚方法)
	public abstract Component getBean(ValueStack stack, HttpServletRequest req, HttpServletResponse res);

	public int doEndTag() throws JspException {
		component.end(pageContext.getOut(), getBody());
		component = null;
		return EVAL_PAGE;
	}

	public int doStartTag() throws JspException {
		// 获取组件
		component = getBean(getStack(), (HttpServletRequest) pageContext.getRequest(), (HttpServletResponse) pageContext.getResponse());
		Container container = Dispatcher.getInstance().getContainer();
		container.inject(component);
		// 填充参数
		populateParams();
		// 组件开始
		boolean evalBody = component.start(pageContext.getOut());
		if (evalBody) {
			return component.usesBody() ? EVAL_BODY_BUFFERED : EVAL_BODY_INCLUDE;
		} else {
			return SKIP_BODY;
		}
	}

	protected void populateParams() {
	}

	public Component getComponent() {
		return component;
	}

}

 

如果自定义struts标签,那么一定要看这类的源码。这里记录下。。。

 

你可能感兴趣的:(apache,jsp,struts,servlet)