无法在Spring MVC中使用CSS/JS之类的资源 [英] unable to use resources like css/js in spring mvc

查看:61
本文介绍了无法在Spring MVC中使用CSS/JS之类的资源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

CSSJS没有在我的应用程序中呈现-

CSS and JS are not being render in my application-

这是我的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-

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

I tried this by various way,some of them are-

<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遇到了类似的问题,而修复它的方式是....

I had a similar problem with my css and the way in which I fixed it was....

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

如果要对其进行测试以查看是否正在读取css,则可以尝试使用以下URL或类似于

If you want to test it to see if your css is being read, you can try to access the css file through your browser, by using the following url or something similar to http://localhost:8080/spring/resources/css/main.css. This should show your css file in your browser

这篇关于无法在Spring MVC中使用CSS/JS之类的资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆