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, we found that the Jsr286Dispatc... SyntaxHighlight can be extended. When developing A portlet using struts2, We 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:
// 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 );
}
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;
// 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 );
}
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