2012-03-19 13:45:15.0|分类: struts|浏览量: 1666
1、<%@ taglib uri="/struts-layout-tags" prefix="layout"%>中struts-layout-tags类 <tag> <name>yield</name> <tag-class>com.fangdo.core.view.YieldTag</tag-class> <body-content>JSP</body-content> <description><![CDATA[Include a servlet's output (result of servlet or a JSP page)]]></description> <attribute> <name>id</name> <required>false</required> <rtexprvalue>true</rtexprvalue> <description><![CDATA[id for referencing element. For UI and form tags it will be used as HTML id attribute]]></description> </attribute> <attribute> <name>value</name> <required>false</required> <rtexprvalue>true</rtexprvalue> <description><![CDATA[The jsp/servlet output to include]]></description> </attribute> </tag>
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(); %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title><layout:yield value="title"/></title> <script type="text/javascript" src="<%= rootPath %>/js/jquery.js"></script> 。。。 <link rel="stylesheet" type="text/css" href="<%= rootPath %>/css/easyui/icon.css"/> <link rel="stylesheet" type="text/css" href="<%= rootPath %>/css/easyui/default/easyui.css"> <script type="text/javascript" src="<%= rootPath %>/js/jquery.easyui.min.js"></script> </head> <body class="easyui-layout"> <div region="north" border="false" style="height:60px;background:#B3DFDA;"> <jsp:include page="../layout/top.jsp"></jsp:include> </div> <div region="west" split="true" title="导航" style="width:200px;padding:10px;"> <jsp:include page="../user/_left_menu.jsp"></jsp:include> </div> <div region="south" border="false" style="height:50px;background:#A9FACD;padding:10px;">south region</div> <div region="center" title="Main Title"> <div id="page"> <jsp:include page="../web/_message.jsp"></jsp:include> <div class="workspace"> <layout:yield /> </div> </div>> </div> </body> </html>
|