Today, when SpringMVC forwards the page, I find that the jump page is indeed successful, but static resources such as JS and CSS do not work:
Control layer code:
/** * Forwarded to the page to view the details of the training program * @return */ @RequestMapping("/2TrainSchemeDatail") public String forward2TrainSchemeDetail(@RequestParam(defaultValue = "1") String trainSchemeId, Model model){ model.addAttribute("trainSchemeId",trainSchemeId); return "pages/trainingScheme/trainingScheme"; }
View resolver configuration:
<!-- 3.视图解释器 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/" /> <property name="suffix" value=".jsp" /> </bean>
Effect:
Finally, after analysis, it is the path problem of JS and CSS: the relative path is used, so the resource cannot be found
Solution:
- The first
Add in the JSP header
<% 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:
<%=basePath %>js/jquery-1.9.1.min.js
-
- The second (recommended)
JSP sets a variable that records the project name: ${baseurl} is equivalent to /project name as follows
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%> <%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%> <c:set var="baseurl" value="${pageContext.request.contextPath}"></c:set> <script type="text/javascript"> <%--to record pronect Name (contextPath=/jwxt)--%> contextPath = "${pageContext.request.contextPath}"; </script>
Change the JS and CSS paths to:
<link rel="stylesheet" href="${baseurl}/css/font.css"> <link rel="stylesheet" href="${baseurl}/css/xadmin.css"> <script type="text/javascript" src="${baseurl}/js/jquery.min.js"></script> <script type="text/javascript" src="${baseurl}/lib/layui/layui.js" charset="utf-8"></script> <script type="text/javascript" src="${baseurl}/js/xadmin.js"></script>