一、<%@ taglib uri="/struts-layout-tags" prefix="layout"%>中struts-layout-tags類javascript
<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>
二、yieldTag類css
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;
}
}
三、Yield類
重寫start,end等方法便可。html
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; } }
四、jsp中java
<%@ 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>