簡體   English   中英

在Spring項目中添加CSS和JS

[英]Adding css and js to Spring project

我知道這個問題在stackoverflow上已經回答了很多次,但是我不能按照答案的步驟來解決。

首先,我要添加一些靜態文件(css js img)到我的項目中,以便從html標簽訪問它們。

我的項目結構是:

http://i.stack.imgur.com/5bfVu.jpg

我的項目在添加CSS之前工作正常,但是我讀了其他文章,應該將此行添加到調度程序文件中:

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

但是,當我添加它時,我無法再部署並得到此錯誤:

GlassFish Server, deploy, null, false

注意 :

我更改了配置文件中的某些內容,以從URL中刪除.htm擴展名。

我的調度員:

<mvc:resources mapping="/**" location="/" />
<bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"/>

<!--
Most controllers will use the ControllerClassNameHandlerMapping above, but
for the index controller we are using ParameterizableViewController, so we must
define an explicit mapping for it.
-->
<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
    <property name="mappings">
        <props>
            <prop key="/">indexController</prop>
        </props>
    </property>
</bean>

<bean id="viewResolver"
      class="org.springframework.web.servlet.view.InternalResourceViewResolver"
      p:prefix="/WEB-INF/jsp/"
      p:suffix=".jsp" />

<!--
The index controller.
-->
<bean name="indexController"
      class="indexContr"/>

我的web.xml文件:

<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
    <servlet-name>dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>
<session-config>
    <session-timeout>
        30
    </session-timeout>
</session-config>
<welcome-file-list>
    <welcome-file>/</welcome-file>
</welcome-file-list>

謝謝你的幫助。

  1. 您已將可用於調度程序servlet的所有名稱空間映射為資源。 將其從/ **減少到/ jsp / website / **

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

  2. 我建議您不要將靜態資源保留在/ jsp /下,這應該是jsp的=)在路徑中使用“ static”或“ resources”

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM