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