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 map = ActionContext.getContext().getContextMap();
			map.put("top", prefix + top);
		}
		if (bottom != null) {
			Map map = ActionContext.getContext().getContextMap();
			map.put("bottom", prefix + bottom);
		}
		if (left != null) {
			Map map = ActionContext.getContext().getContextMap();
			map.put("left", prefix + left);
		}
		if (center != null) {
			Map map = ActionContext.getContext().getContextMap();
			map.put("center", prefix + center);
		}
		if (right != null) {
			Map 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

			
				/layout/borderlayout.jsp
				/jsp
			


borderlayout.jsp如下:

<%@ page language="java" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>




Administrator Console







	
	

 


然后这样使用

        
        	
        		/system/role/search-role-left.jsp
        		/system/role/search-role-center.jsp
        	
        	
        

你可能感兴趣的:(程序设计,java,struts2,layout,布局)