【问题标题】:Adding css and js to Spring project在 Spring 项目中添加 css 和 js
【发布时间】:2015-10-15 09:11:41
【问题描述】:

我知道这个问题在 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>

感谢您的帮助。

【问题讨论】:

  • 您检查过服务器日志是否有任何异常?部署失败很奇怪。请检查 Your_GlassFish_Location\glassfish\domains\domain1\logs\server.log

标签: java css xml spring spring-mvc


【解决方案1】:
  1. 您已将所有可用于调度程序 servlet 的命名空间映射为资源。 将它从 /** 减少到 /jsp/website/**

    &lt;mvc:resources mapping="/resources/**" location="/jsp/website/" /&gt;

  2. 我建议你不要在/jsp/下保留静态资源,这里应该是jsp的=) 在路径中使用“静态”或“资源”

    &lt;mvc:resources mapping="/resources/**" location="/resources/" /&gt;

【讨论】:

  • 是的,第二个选项是推荐的。
  • 谢谢我的问题已经解决我现在可以构建我的项目了,当我尝试访问图像时,我又遇到了一个问题,我得到 HTTP 状态 404 - Not Found.. 有帮助吗?
猜你喜欢
  • 1970-01-01
  • 2014-03-29
  • 1970-01-01
  • 1970-01-01
  • 2018-03-28
  • 2023-03-26
  • 1970-01-01
  • 2019-09-14
  • 2016-03-31
相关资源
最近更新 更多