Struts2, CSS and JavaScript refer to relative paths and absolute path issues in the JSP view page. __js

Source: Internet
Author: User

In the actual use of Struts2, CSS and JavaScript are often introduced into the JSP view page, which involves reference path issues. There are two types of paths, one is the relative path and the other is an absolute path. Let's talk about their impact in the Struts2 Page view, respectively. 1, relative path

The reference format for the relative path is as follows:

<script src= "Resources/scripts/jquery-ui-1.8.custom.min.js" type= "Text/javascript" ></script>

Note that here, the first path directly ahead of the folder path, there is no "/", which is the reference method of relative paths in HTML, in the normal HTML static page of the server environment, refers to the current HTML file path.

However, in a STRUTS2 server environment, this relative path is interpreted as relative to the path of the STRUTS2 deployment project root directory.

2. Absolute path

The reference format for an absolute path is as follows:

<script src= "/resources/scripts/jquery-ui-1.8.custom.min.js" type= "Text/javascript" ></script>

An absolute path has a "/" at the front, a reference to an absolute path in HTML, and in a server environment for an ordinary HTML static page, a path relative to the root of the modified project (typically webroot in a Web server such as Apache or Tomcat configuration file).

However, in a STRUTS2 server environment, it is directly interpreted as the server's top-level directory.

It should be noted that regardless of the relative path or absolute path, you currently contain the "<script src="/resources/scripts/jquery-ui-1.8.custom.min.js "type=" text/ The path to the JSP page file for JavaScript "></script>" is irrelevant. This is different from the relative and absolute paths of static HTML files in a normal server environment.

For example, you will be able to understand the above two situations in a simpler way.

For example, I now use Eclipse to create a Web project called struts. The JSP page file I am using is placed under the webroot/module/gjtj/admin/jsp directory under the Struts project. I have two references in this JSP page:

<script src= "Module/gjtj/admin/jsp/resources/scripts/smooth.dialog.js" type= "Text/javascript" ></script >
<script src= "/module/gjtj/admin/jsp/resources/scripts/smooth.autocomplete.js" type= "Text/javascript" ></script>

The first one will be interpreted by the server as the following URL:

Localhost:8080/struts/module/gjtj/admin/jsp/resources/scripts/smooth.dialog.js

The second will be interpreted by the server as the following URL:

Localhost:8080/module/gjtj/admin/jsp/resources/scripts/smooth.autocomplete.js

The middle difference is a name for the project struts. Also, the two interpreted paths have no association with the path of the current JSP page.

Tip tips:

If you want to use an existing page template or style in the Struts2 page view (my example here is the smooth admin template used), you need to be aware that these third party samples or styles use relative paths for style and script inclusion. But these are interpreted as a path to the project name in the STRUTS2 environment, and the problem is that the style and script path are incorrect. It is recommended that you put these resource files under the root directory under the project file Webroot, so that you can use the so-called "relative path"

Resources/scripts/smooth.dialog.js
You can load the resource file correctly.






Related Article

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.