SPRINGMVC controller jump to JSP page css img js etc file does not work not show

Source: Internet
Author: User

  

Today, when Springmvc forwarded the page, it was found that the jump page did succeed, but static resources such as JS,CSS did not work:

Control Layer Code:

    /**      * Forward to view the Culture Program details page      @return     */    @RequestMapping ("/ 2TrainSchemeDatail ")    public String forward2trainschemedetail (@RequestParam ( DefaultValue = "1") String Trainschemeid, model model) {        Model.addattribute ("Trainschemeid", Trainschemeid);         return "Pages/trainingscheme/trainingscheme";    }

View Parser configuration:

    <!--3. View Interpreter -    <Beanclass= "Org.springframework.web.servlet.view.InternalResourceViewResolver">        < Propertyname= "prefix"value="/" />        < Propertyname= "suffix"value= ". jsp" />    </Bean>

Effect:

The last analysis is the path problem of JS and CSS: the relative path is used, so the resource cannot be found

  

Workaround:

    • First Kind

In the JSP header plus

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

Change the relative path of the resource file, such as Css,js,img, and the access controller to an absolute path:

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

      • The second type (recommended)

JSP sets a variable that records the name of the item: the following ${baseurl} is equivalent to/project name

<%@ taglibURI= "Http://java.sun.com/jsp/jstl/core"prefix= "C"%><%@ taglibURI= "Http://java.sun.com/jsp/jstl/fmt"prefix= "FMT"%><%@ taglibprefix= "FN"URI= "Http://java.sun.com/jsp/jstl/functions"%><C:setvar= "BaseURL"value= "${pagecontext.request.contextpath}"></C:set><Scripttype= "Text/javascript">    <%--torecord Pronect Name (ContextPath=/jwxt)--%>ContextPath= "${pagecontext.request.contextpath}"; </script>

JS and CSS path changed to:

    <Linkrel= "stylesheet"href= "${baseurl}/css/font.css">    <Linkrel= "stylesheet"href= "${baseurl}/css/xadmin.css">    <Scripttype= "Text/javascript"src= "${baseurl}/js/jquery.min.js"></Script>    <Scripttype= "Text/javascript"src= "${baseurl}/lib/layui/layui.js"CharSet= "Utf-8"></Script>    <Scripttype= "Text/javascript"src= "${baseurl}/js/xadmin.js"></Script>

SPRINGMVC controller jump to JSP page css img js etc file does not work not show

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.