亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

春天的MVC。為什么從 JSP 頁面看不到資源?

春天的MVC。為什么從 JSP 頁面看不到資源?

MMTTMM 2021-11-11 15:50:45
我有簡單的 spring mvc 應用程序。我嘗試向我的應用程序添加類似靜態資源的 CSS,但我的 jsp 頁面找不到它。我的jsp頁面是這樣的:    <html>    <head>        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">        <title><fmt:message key="app.title"/></title>        <link rel="stylesheet" href="/resources/css/style.css">    </head>    <body>         ..................................................................    </body>    </html>另外我的 spring-mvc 上下文是這樣的:<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   xmlns:context="http://www.springframework.org/schema/context"   xmlns:p="http://www.springframework.org/schema/p"   xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns="http://www.springframework.org/schema/beans"   xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd   http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd   http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"><mvc:annotation-driven/><mvc:resources mapping="/resources/**" location="/resources/"/><context:component-scan base-package="ru.javawebinar.**.web"/><bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"      p:viewClass="org.springframework.web.servlet.view.JstlView"      p:prefix="/WEB-INF/jsp/"      p:suffix=".jsp"/>我的資源位于:
查看完整描述

2 回答

?
皈依舞

TA貢獻1851條經驗 獲得超3個贊

你在使用 JSTL 嗎?如果是這樣,你可以包括這樣。

 <link href="<c:url value="/resources/css/style.css" />" rel="stylesheet">

另一種方法是:

<link href="${pageContext.request.contextPath}/resources/css/style.css" rel="stylesheet" >



查看完整回答
反對 回復 2021-11-11
?
吃雞游戲

TA貢獻1829條經驗 獲得超7個贊

嘗試以下更改:


在 spring-web-config.xml 中:


<mvc:resources mapping="/resources/**" location="/resources/css/"/>

在您的 jsp 文件中:


<link href="<c:url value="/resources/css/style.css" />" rel="stylesheet">

更新:


對于 Spring,您可以在 *.jsp 文件中嘗試:


<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>

<!DOCTYPE html>

<html>

  <head>

    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

    <title><fmt:message key="app.title"/></title>

    <spring:url value="/resources/css/style.css" var="mainCss" />

    <link href="${mainCss}" rel="stylesheet" />

  </head>

  <body>

    ..................................................................

  </body>

</html>


查看完整回答
反對 回復 2021-11-11
  • 2 回答
  • 0 關注
  • 188 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號