SpringMvc + jsp static resources not found

Source: Internet
Author: User

Today, I used springMvc + jsp to create a project and found that static resource files are always unavailable from time to time.


Of course, google Baidu found that some of the above methods have been mentioned, so that these static files are not processed by springDispatcher, and the idea of using tomcat default servlet to process

After doing so, I found it was okay.

A

@ RequestMapping (value = "/cardtool", method = RequestMethod. GET)
Public String cardTool (){
Return "/cardtool ";
}


Find cardtool. jsp

The following are static resources referenced on this jsp interface.

<Script src = "assets/js/card. js" type = "text/javascript"> </script>
<Script src = "assets/js/json. js" type = "text/javascript"> </script>

Http: // 127.0.0.1: 8010/project name/assets/js/card. js

So I want to try to change the value of RequestMapping.

Code changes

B

@ RequestMapping (value = "/cardtool/test/dev", method = RequestMethod. GET)
Public String cardTool (){
Return "/cardtool ";
}


I cannot find any strange static files in my requests. Why are these files introduced by relative paths?

I saw Error 404.

Http: // 127.0.0.1: 8010/project name/cardtool/assets/js/card. js

How can I find a cardtool?

So try

<Script src = "../assets/js/json. js" type = "text/javascript"> </script>

Yes, but an error is returned when A is used.

This is the beginning and end of the story. How does spring get rid of my webapp path?


It's better to be sad and helpless. forget to use the jsp tag.

<%
String path = request. getContextPath ();
String basePath = request. getScheme () + ": //" + request. getServerName () + ":" + request. getServerPort () + path + "/";
%>




<Script src = "assets/js/card. js" type = "text/javascript"> </script>
<Script src = "assets/js/json. js" type = "text/javascript"> </script>




After the rows are complete, try the B method for the request. I do not like this method, because it is necessary to execute the basePath code for computing every page, but there is no good solution either.

Hope springMVC can solve this problem early


Paste the core code below so that everyone will not be the same as me.

Web. xml



SpringDispatcher
Org. springframework. web. servlet. DispatcherServlet

ContextConfigLocation

Classpath *:/spring/spring-framework.xml
Classpath: // spring/spring-base.xml
Classpath: // spring/spring-web.xml


2


SpringDispatcher
/


Default
/Assets /*


The jsp page will not be pasted. See the preceding parsing.



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.