【问题标题】:Placing and using css and js files in springspring中放置和使用css和js文件
【发布时间】:2014-12-19 22:52:57
【问题描述】:

我是spring mvc的新手,我已经根据教程点中的教程开发了所有基本的东西,但它没有解释如何链接css文件或js文件。我已经在stackoverflow中解决了其他关于此的问题,但我无法解决我的问题。首先,我将从下面给出的错误开始。

GET http://localhost:8082/Springtest/resources/css/bootstrap.css  

我的 web.xml 如下所示。

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4" 
     xmlns="http://java.sun.com/xml/ns/j2ee" 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> 
<display-name>Spring MVC Application</display-name> 
<servlet> 
    <servlet-name>Springtest</servlet-name> 
    <servlet-class> org.springframework.web.servlet.DispatcherServlet </servlet-class> 
    <load-on-startup>1</load-on-startup> 
</servlet> 
 <servlet-mapping> 
    <servlet-name>Springtest</servlet-name> 
    <url-pattern>/</url-pattern> 
</servlet-mapping>
<session-config>
    <session-timeout>
        30
    </session-timeout>
</session-config>
<welcome-file-list>
    <welcome-file>redirect.jsp</welcome-file>
</welcome-file-list>
</web-app>

applicationContext.xml

<?xml version='1.0' encoding='UTF-8' ?>
<!-- was: <?xml version="1.0" encoding="UTF-8"?> -->
<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns:p="http://www.springframework.org/schema/p"
   xmlns:aop="http://www.springframework.org/schema/aop"
   xmlns:tx="http://www.springframework.org/schema/tx"
   xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
   http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
   http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">

<context:component-scan base-package="Student" /> 
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
    <property name="prefix" value="/WEB-INF/jsp/" /> 
    <property name="suffix" value=".jsp" /> 
</bean>
<resources mapping="/resources/**" location="/resources/" /> 
</beans>

Springtest-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>
<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" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> 
<context:component-scan base-package="Student" /> 
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
    <property name="prefix" value="/WEB-INF/jsp/" /> 
    <property name="suffix" value=".jsp" /> 
</bean>
</beans>

我已经使用下面的语句链接到我的 css

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

使用的文件结构图如下。

我已经标记了包含我的css文件的位置,我没有使用maven或pom.xml。请告诉我我在这里犯了什么错误。我也不知道在 spring-mvc 项目中使用 maven 和 pom.xml,所以一个教程也会有所帮助。

【问题讨论】:

    标签: java html css spring spring-mvc


    【解决方案1】:

    我在 Spring MVC 中使用 TheamLeaf 而不是 Jsp,并且在使用此 CSS 文件之前,我在每个页面中添加了这一行

    <link rel="stylesheet" th:href="@{/frontend/css/myStyle.css}" />
    

    与我自己的自定义 JavaScript 文件类似的情况

    <script th:src="@{/custom/js/myFile.js}" type="text/javascript"></script>
    

    你会试试这个吗?我的意思是你的 Jsp 页面中有这些链接吗?

    【讨论】:

    【解决方案2】:

    我觉得在资源标签里应该是

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

    或者你应该声明它

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

    【讨论】:

    • 我还是有同样的错误,一定是别的原因。
    猜你喜欢
    • 2013-09-27
    • 2012-12-19
    • 1970-01-01
    • 1970-01-01
    • 2020-08-25
    • 2018-07-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多