java - 无法在spring mvc中使用css/js等资源

标签 java spring maven jsp spring-mvc

CSSJS 未在我的应用程序中渲染-

这是我的Dispatcher.xml-

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
   http://www.springframework.org/schema/beans/spring-beans.xsd
   http://www.springframework.org/schema/mvc
   http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
   http://www.springframework.org/schema/context
   http://www.springframework.org/schema/context/spring-context.xsd">

    <context:annotation-config />
    <context:component-scan base-package="com.ttnd.springdemo.controller" />
    <mvc:resources location="/resources/" mapping="/resources/**" />
    <mvc:annotation-driven />
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix">
            <value>/WEB-INF/views/</value>
        </property>
        <property name="suffix">
            <value>.jsp</value>
        </property>
    </bean>
</beans>

下面是我使用 css/js-

的页面

我通过各种方式尝试过,其中一些是-

<link rel="stylesheet" type="text/css" href="css/bootstrap.css">
<link rel="stylesheet" type="text/css" href="css/bootstrap.min.css">
<link rel="stylesheet" type="text/css" href="css/application.css">
<script src="js/application.js"></script>
<script src="js/bootstrap.js"></script>
<script src="js/bootstrap.min.js"></script>
<script src="js/jquery.min.js"></script>
</head>

或者-

<link rel="stylesheet" type="text/css" href="<spring:url value='resources/css/application.css' />" />
<script src="<spring:url value='resources/js/application.js' />"></script>
<script src="<spring:url value='resources/js/jquery.min.js' />"></script>

最佳答案

我的 css 也有类似的问题,我修复它的方法是......

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

如果您想测试它是否正在读取您的CSS,您可以尝试通过浏览器访问CSS文件,使用以下网址或类似http://localhost:8080/spring/resources/css/main.css的内容。这应该会在浏览器中显示您的 css 文件

关于java - 无法在spring mvc中使用css/js等资源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34786182/

相关文章:

java - Hibernate 找不到现有字符串

spring - Spring Tomcat 应用程序中的 NoSuchMethodError

java - eclipse中的"Source folder is not a Java project"错误

java:输出包含图片和文本的报告

java - 构建项目时的 Maven 配置

java - 在 Google App Engine 数据存储上执行大型查询

java - 类的构造函数参数是否也应该使用 Java Bean Validation API 注释进行注释?

java - 如何以编程方式将 Artifact 添加到 MavenProject?

java - 为什么 Maven 不从测试类生成源代码?

java - 我可以关闭并重新打开套接字吗?