The include label causes the js path to fail to be found.

Source: Internet
Author: User

Precautions for Using jsp: include labels and <% @ include labels
Recall the differences between the two (for this article, there is no difference in usage between the two)

 


Jsp: include: Compile the embedded ded. jsp file first, then include the compiled file, and then include
@ Include is to first include the file, then the unified compilation first contains, and then the compilation


This afternoon, I want to integrate Jquery into the project, and I will see problems. The reason is the path problem.


During integration, my idea was to write a public JSP file containing some common js files. Of course, I used the Jquery plug-in here.


But after writing it, I found that it is usable to test the public JSP separately, but JQuery is never available for pages that contain the public JSP page. At first I thought it was a question of Loading Order.


After several tests, I finally found the problem.

 


Because there are many project files, the files are not stored in one folder: On the public JSP page, there are


<% @ Page language = "java" import = "java. util. *" pageEncoding = "UTF-8" %>
<Script type = "text/javascript" src = "js/jquery132min. js"> </script>


<Script type = "text/javascript">
$ (Function (){
Alert ('hello1 ');
});
</Script>


When calling it, there are:


<% @ Include file = ".../../common_ext.jsp" %>
However, this may cause errors.

 


The reason is that after the include operation, if the public JSP is loaded to its own JSP, the relative position of JQueryr has changed. That is, the public JSP transmits the relative position of JQuery to its own JSP, but its own JSP is based on its own, and the Jquery plug-in cannot be found through the path. Simply put, if a relative path is used, the path in the public JSP should be the path relative to its own jSP.

 


But it clearly loses the meaning of public JSP, so here we use the absolute path to do it:


<% @ Page language = "java" import = "java. util. *" pageEncoding = "UTF-8" %>


<%
String tPath = request. getContextPath ();
String tBasePath = request. getScheme () + ": //" + request. getServerName () + ":" + request. getServerPort () + tPath + "/";
%>


<Script type = "text/javascript" src = "<% = tBasePath %> js/jquery132min. js"> </script>
<Script type = "text/javascript">

$ (Function (){
Alert ('hello1 ');
});

</Script>


In this way, you only need to introduce public jSP in your own JSP page.

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.