使用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) { 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); } // } } }
?
然后在portlet.xml中添加js脚本的初始参数设置,代码如下:
<portlet id="AnalysisPresentation"> <description xml:lang="zh">分析数据展现</description> <portlet-name>AnalysisPresentation</portlet-name> <display-name xml:lang="zh">分析数据展现</display-name> <portlet-class>com.lily.dap.web.portlet.dispatcher.Dispatcher</portlet-class> <init-param> <name>scripts</name> <value>/scripts/jquery-1.7.1/jquery.min.js /fc/FusionCharts.js /fc/FusionCharts.jqueryplugin.js /scripts/presentation-chart.js</value> </init-param> <init-param> <name>styles</name> <value>/styles/report.css</value> </init-param> <!-- The view mode namespace. Maps to a namespace in the Struts 2 config file. --> <init-param> <name>viewNamespace</name> <value>/presentation</value> </init-param> <!-- The default action to invoke in view mode. --> <init-param> <name>defaultViewAction</name> <value>analysis-presentation</value> </init-param> <!-- The edit mode namespace. Maps to a namespace in the Struts 2 config file. --> <init-param> <name>editNamespace</name> <value>/presentation</value> </init-param> <!-- The default action to invoke in edit mode. --> <init-param> <name>defaultEditAction</name> <value>analysis-presentation-edit</value> </init-param> ... </portlet>
?
至此,即实现了多个portlet中的同一个js只加载一次的要求