About the include tag causes the JS path cannot find the problem analysis and solves the _javascript skill

Source: Internet
Author: User
Things to be aware of when using jsp:include tags and <%@ include tags
Recall the difference between the two (there is no difference in usage for this article)
Jsp:include is to compile the included.jsp file first and then include (compile first, then include)
@ include is to include the file first, then unified compilation (first included, later compiled)
I want to integrate jquery into the project this afternoon and find out how it all goes wrong. The reason is the path problem.
In the integration, my idea is to write a public JSP file, which contains some commonly used JS files, of course, I use the jquery plugin.
But after writing to find out that a separate test public JSP is available, but the page containing the public JSP page can never use jquery, the beginning is thought to be the problem of loading order.

After several tests, finally found the problem.
Because there are more project files, the file is not placed under a folder: In the public JSP page, there are
Copy Code code as follows:

<%@ 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>

At the time of calling it, there are:
<%@ include file= ". /.. /common_ext.jsp "%>
But this is likely to lead to errors.

The reason is that, after include, the public JSP is loaded into its own JSP, the relative position of the Jqueryr has changed. That is, the public JSP to the relative position of jquery to its own JSP, but its own JSP as a standard, through the path can not find the jquery plug-ins. Simply put, if you use a relative path, the path in the public JSP should be the path relative to your JSP.
But this obviously loses its significance as a public JSP, so here's the absolute path:
Copy Code code as follows:

<%@ 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, as long as in their own JSP page to introduce a public JSP.

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.