struts2实现页面模板化



如果不想引入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>

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值