The project's js library is loaded only once.

Source: Internet
Author: User

When using struts2 to develop a portlet, You need to load the existing js library on the jsp page for interaction. This portlet may be used repeatedly on the page, which requires that the same js file is loaded only once in multiple portlet on the page. After research, it is found that the Jsr286Dispatcher class can be extended, and then the doHeaders method can be rewritten. The Code is as follows:
 
Java code
@ 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 );
}
//}
}
After running the program, you find that the doHeaders method is not executed. Then, check the source code of the Jsr286Dispatcher class and the source code of the GenericPortlet class. It is found that struts2 has modified the render method and no longer calls the doHeaders method, so I have rewritten the render method. The overall code is as follows:
Java code
/**
*
*/
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;
 
/**
* Supports loading js script files and css style files
*
* @ 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 );
}
//}
}
}
 
Add the initial parameter settings of the js script to the portlet. xml file. The Code is as follows:
Xml Code
<Portlet id = "AnalysisPresentation">
<Description xml: lang = "zh"> analysis data presentation </description>
<Portlet-name> AnalysisPresentation </portlet-name>
<Display-name xml: lang = "zh"> analysis data display </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>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.