SPRINGMVC controller jump to JSP page CSS img js file does not work do not show __java

Source: Internet
Author: User

Through the browser's developer tools to view the Web page source code found that the path of resources such as css,js,img error, the path to the front of a more than one controller path, similar to:

http://localhost:8080/Project root/Controller name/js/jquery-1.9.1.min.js

Workaround: In the JSP page, set the path of the resource to ... /js/jquery-1.9.1.min.js This will return the controller's previous path, and the resulting path is:

http://localhost:8080/Project root directory/js/jquery-1.9.1.min.js

Above this solution is not good, assuming that there is a scenario: I started the project in MyEclipse, the first run is a landing page, the path is

http://localhost:8080/Project root directory/login.jsp

Login.jsp inside the css,js,img and other resources such as the relative path such as./js/jquery-1.9.1.min.js.
After I login, login successfully entered a new page, in this new page has an exit Login button, this button click to jump to the controller, the path of this controller is:

http://localhost:8080/Project root/Controller name/controller inside method name

In this controller inside I do some exit login operation, after these operations, I jump to the login.jsp page again, this time login.jsp inside css,js,img and other resources of the path to change again to

http://localhost:8080/Project root/Controller name/js/jquery-1.9.1.min.js

Still can't load resources, so the final solution is: referencing Css,js,img and other resource files, and in the JSP page to jump to other pages or controllers when using absolute path.

Add this code to the header of the JSP page:

<%
    String Path = Request.getcontextpath ();
    String basepath = request.getscheme () + "://"
            + request.getservername () + ":" + request.getserverport ()
            + path + " /";
%>

Change the relative path of resource files such as css,js,img and access controllers to absolute paths:

./js/jquery-1.9.1.min.js => <%=basepath%>js/jquery-1.9.1.min.js

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.