Processing of reference js and other static files in spring mvc and html

Source: Internet
Author: User

Processing of reference js and other static files in spring mvc and html

I encountered some problems on the development page recently. Record them here.

Generally, some images, js, css, and other files are required for html pages or jsp pages. Therefore, you need to enter the corresponding file path, which can be written in two ways: relative path and absolute path, when writing a relative path, if the corresponding html or jsp page is returned through the controller, the relative path may not be what we want, for example, on the server side, if the path of a js file is/js/xx. js, the html file is/html/xx.html, and write the relative path reference code in html <script src = ".. /js/jquery. min. js "> </script>, there are two results. Here we assume that the filter does not intercept access, and If the access path is http: // 127.0.0.1: 8080/(project name)/html/xx.html, the result is normal, however, when we return the corresponding html page through the controller ing, an error may occur.

For example, our controller ing is as follows:

@Controller@RequestMapping("/download")public class DownloadController {@RequestMapping(value="/develop/*",method=RequestMethod.GET,produces={"text/html"})public String developPage()  {        return "xx"; }  }
The path of our example xx.html can be written as http: // 127.0.0.1: 8080/(project name)/download/develop /......

In this case, the relative path is .. /js/xx. after js is parsed on the browser side, it will be: http: // 127.0.0.1: 8080/(project name)/download/js/xx. js, so the miserable thing happened, and the corresponding js file will not be properly referenced, the reason is very simple, the path is incorrect, naturally cannot be found, the information found on the internet is explained: the imported js is relative to the current request path, rather than the directory where your files are stored on the server. Therefore, the explanation on the internet is confirmed ,.. /js/xx. after js is parsed by the browser, the address is http: // 127.0.0.1: 8080/(project name)/download/js/xx. js.

So how can we solve the relative path problem caused by the controller?

If controller ing is not passed, you do not need to worry about this problem. You can write the relative path boldly because the directory structure of your request address is consistent with that of the server. If controller ing is required, the method is as follows:

For a jsp page, you can use the following code to obtain the absolute path address:

<%  String path = request.getContextPath();  String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";  %><script type="text/javascript" src="<%=basepath%>/js/xx.js"></script>  
If it is html code, this is also a problem I encountered. Because it is not very long to be exposed to web development, I am saddened to be here, because all my pages use the html + ajax method, therefore, there is no chance to use some jsp methods,
In fact, the solution is relatively simple. You can directly write the absolute path:/(project name)/js/xx. js.

Speaking of this, the question I want to talk about is basically over. If there is a better solution, I hope you can give me some advice.

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.