如果不想引入tiles等工具,可利用struts2本身实现简易的动态布局处理。
首先定义一个模板类
public class LayoutResult extends ServletDispatcherResult {
private String prefix;
public LayoutResult() {
super();
}
public LayoutResult(String location) {
super(location);
}
public void doExecute(String finalLocation, ActionInvocation invocation) throws Exception {
if (top != null) {
Map<String, Object> map = ActionContext.getContext().getContextMap();
map.put("top", prefix + top);
}
if (bottom != null) {
Map<String, Object> map = ActionContext.getContext().getContextMap();
map.put("bottom", prefix + bottom);
}
if (left != null) {
Map<String, Object> map = ActionContext.getContext().getContextMap();
map.put("left", prefix + left);
}
if (center != null) {
Map<String, Object> map = ActionContext.getContext().getContextMap();
map.put("center", prefix + center);
}
if (right != null) {
Map<String, Object> map = ActionContext.getContext().getContextMap();
map.put("right", prefix + right);
}
super.doExecute(prefix+finalLocation, invocation);
}
private String top;
private String left;
private String center;
private String right;
private String bottom;
public void setLeft(String left) {
this.left = left;
}
public void setCenter(String center) {
this.center = center;
}
public void setRight(String right) {
this.right = right;
}
public void setTop(String top) {
this.top = top;
}
public void setBottom(String bottom) {
this.bottom = bottom;
}
public void setPrefix(String prefix) {
this.prefix = prefix;
}
}
再配置好struts.xml
<result-type name="borderlayout" class="net.xway.base.sys.result.LayoutResult">
<param name="location">/layout/borderlayout.jsp</param>
<param name="prefix">/jsp</param>
</result-type>
borderlayout.jsp如下:
<%@ page language="java" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Administrator Console</title>
<link href="images/favicon.ico" rel="shortcut icon" type="image/vnd.microsoft.icon"/>
<link href="css/ui-lightness/jquery-ui-1.10.4.css" rel="stylesheet">
<script src="js/jquery-1.10.2.js"></script>
<script src="js/jquery-ui-1.10.4.js"></script>
<link rel="stylesheet" href="css/orange.css" media="screen">
</head>
<body>
<jsp:include page="../header.jsp"></jsp:include>
<jsp:include page="../nav.jsp" /><p></p>
<s:if test="#top != null">
<div><s:include value="%{#top }" /></div>
</s:if>
<!-- Three Columns left, center, right -->
<table border="0" width="100%">
<tr>
<s:if test="#left != null">
<td width="200px" valign="top"> <s:include value="%{#left }" /> </td>
<td width="1px" style="background:lightblue"></td>
</s:if>
<s:if test="#center != null">
<td valign="top"> <s:include value="%{#center }" /> </td>
</s:if>
<s:if test="#right != null">
<td width="1px" style="background:lightblue"></td>
<td width="200px" valign="top"> </td>
</s:if>
</tr>
</table>
<s:if test="#bottom != null">
<div><s:include value="%{#bottom }" /></div>
</s:if>
<jsp:include page="../footer.jsp" />
</body>
</html>
然后这样使用
<action name="ListRole" class="ListRoleAction">
<result type="borderlayout">
<param name="left">/system/role/search-role-left.jsp</param>
<param name="center">/system/role/search-role-center.jsp</param>
</result>
</action>