Problems with spring MVC and HTML handling of static files such as JS

Source: Internet
Author: User

Recent development page encountered some problems, here to record.

Usually, our HTML page or JSP page will inevitably need to introduce some pictures, JS, CSS and other files, so you need to fill in the corresponding file path, there are relative paths and absolute path two ways, write relative path, if through the controller and then return to the corresponding HTML or JSP page, The relative path may not be what we want, for example, on the server side, if a JS file path is:/js/xx.js,html file is/html/xx.html, and write a relative path reference code in HTML <script src= ". /js/jquery.min.js "></SCRIPT>, there will be two results, assuming that filter does not intercept access, and configured <mvc:resources location="/js/"mapping="/ js/** "/> if we have direct access to the path: http://127.0.0.1:8080/(project name)/html/ Xx.html, the results are all normal, but when we return the corresponding HTML page through the controller mapping, we may get an error.

For example, our controller mapping is as follows:

@Controller @requestmapping ("/download") public class Downloadcontroller {@RequestMapping (value= "/develop/*", method =requestmethod.get,produces={"text/html"}) public String developpage ()  {        return ' xx ';}  }
From the controller above, we access the xx.html path can be written as: http://127.0.0.1:8080/(project name)/download/develop/...

So this time the relative path: /js/xx.js after the browser-side parsing will be: http://127.0.0.1:8080/(project name)/download/js/ Xx.js, so sad things happened, the corresponding JS file will not get the normal reference, the reason is very simple, the path is not, nature can not find, the information found on the Internet is interpreted as: The import of JS is relative to the current request of the path, and not relative to your server file placement directory. So, it just confirms the explanation on the Internet. /js/xx.js is parsed by the browser and the address is http://127.0.0.1:8080/(project name)/download/js/xx.js.

So how to solve the relative path problem caused by controller?

Without the controller mapping, then this problem does not have to worry about, the bold write relative path is, because your request address directory structure and server consistency, if you want to pass controller mapping, the method is as follows:

Assuming it is a JSP page, you can get the absolute path address using the following code:

<%<span><span>  String Path = Request.getcontextpath ();</span></span><span>   string basepath = Request.getscheme () + <span class= "String" > "://" </span><span> + Request.getservername () + </span><span class= "string" > ":" </span><span> </span></  span><span>+ request.getserverport () + path + <span class= "string" > "/" </span><span>; </span></span>%><script type= "Text/javascript" src= "<%=basepath%>/js/xx.js" ></ Script>  
    If it is the HTML code, this is also my problem, because the contact web development time is not too long, it is sad to be here, because all my pages are used Html+ajax way, so there is no chance to use some of the JSP methods,
In fact, the solution is also relatively simple, you can directly write absolute path:/(project name)/js/xx.js.

Speaking here, the question is basically finished, if there is a better plan, hope the great God guidance.

Problems with spring MVC and HTML handling of static files such as JS

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.