日期:2014-05-16  浏览次数:20355 次

基于Struts2的portlet开发时,实现多个portlet中的同一个js只加载一次

使用struts2开发portlet时,需要在jsp页面上加载现有的js库进行交互。这个portlet可能在页面上重复使用,这就要求确保页面上的多个portlet中同样的js文件只会加载一次。经过研究,发现可以通过扩展Jsr286Dispatcher类,然后重写doHeaders方法实现,代码如下:

?

	@Override
	protected void doHeaders(RenderRequest request, RenderResponse response) {
		super.doHeaders(request, response);
		
//        if (request.getPortalContext().getProperty(PortalContext.MARKUP_HEAD_ELEMENT_SUPPORT) == null) {
//			logger.warn("The Portal Server does not support adding markup to the header. This may lead to portlets that do not work properly.");        
//		} else {            
			for (String cssFile : styles) {                    
				Element htmlHeader = response.createElement("link");                    
				htmlHeader.setAttribute("rel", "stylesheet");                    
				htmlHeader.setAttribute("type", "text/css");                    
				htmlHeader.setAttribute("href", response.encodeURL(request.getContextPath() + cssFile));  
				
				response.addProperty(MimeResponse.MARKUP_HEAD_ELEMENT, htmlHeader);                    
				logger.debug("Added {} CSS file to the portal header.", cssFile);                
			}                
				
			for (String javascriptFile : scripts) {                    
				Element htmlHeader = response.createElement("script");                    
				htmlHeader.setAttribute("src", response.encodeURL(request.getContextPath() + javascriptFile));                    
				htmlHeader.setAttribute("type", "text/javascript");   
				
				response.addProperty(MimeResponse.MARKUP_HEAD_ELEMENT, htmlHeader);
				logger.debug("Added {} Javascript file to the portal header.", javascriptFile);                
			}            
//		}    
	}

?运行后发现doHeaders方法不执行,然后查阅Jsr286Dispatcher类的源代码和GenericPortlet类的源代码,发现struts2已经修改了render方法,不再执行调用doHeaders的方法,所以我又重写了render方法,整体代码如下:

/**
 * 
 */
package com.lily.dap.web.portlet.dispatcher;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.portlet.MimeResponse;
//import javax.portlet.PortalContext;
import javax.portlet.PortletConfig;
import javax.portlet.PortletException;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;

import org.apache.struts2.portlet.dispatcher.Jsr286Dispatcher;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.w3c.dom.Element;

/**
 * 支持加载js脚本文件、css样式文件
 * 
 * @author zouxuemo
 *
 */
public class Dispatcher extends Jsr286Dispatcher {
	protected final Logger logger = LoggerFactory.getLogger(getClass());
	
	private List<String> scripts = new ArrayList<String>();
	
	private List<String> styles = new ArrayList<String>();
	
	@Override
	public void init(PortletConfig cfg) throws PortletException {
		super.init(cfg);
		
		String s = cfg.getInitParameter("scripts");
		if (s != null) {
			String[] tmp = s.split(" ");
			for (String script : tmp) {
				script = script.trim();
				if (script.charAt(0) != '/')
					script = '/' + script;
				
				scripts.add(script);
			}
		}
		
		s = cfg.getInitParameter("styles");
		if (s != null) {
			String[] tmp = s.split(" ");
			for (String style : tmp) {
				style = style.trim();
				if (style.charAt(0) != '/')
					style = '/' + style;
				
				styles.add(style);
			}
		}
	}

	@Override
	public void render(RenderRequest request, RenderResponse response)
			throws PortletException, IOException {
		doHeaders(request, response);
		
		super.render(request, response);
	}

	@Override
	protected void doHeaders(RenderRequest request, RenderResponse response) {
		super.doHeaders(request, response);
		
//        if (request.getPortalContext().getProperty(PortalContext.MARKUP_HEAD_ELEMENT_SUPPORT) == null) {
//			logger.warn("The Portal Server does not support adding markup to the header. This may lead to portlets that do not work properly.");        
//		} else {            
			for (String cssFile : styles) {