Spring没有找到资源文件(css,jsp ...) [英] Spring not finding resource files (css, jsp...)

查看:244
本文介绍了Spring没有找到资源文件(css,jsp ...)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用一个jsp文件作为控制器的模型,我想使用css样式和js库

I'm using a jsp file as a model from a Controller and I want to use an css styles and js libraries


  • Proyect

    • Web内容

    • 资产

    • WEB-INF

      • jsp

      在web.xml中:

      <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
      <welcome-file-list>
          <welcome-file>/WEB-INF/jsp/index.jsp</welcome-file>
      </welcome-file-list>
      
      <!-- Processes application requests -->
      <servlet>
          <servlet-name>MyProject</servlet-name>
          <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
          <init-param>
              <param-name>contextConfigLocation</param-name>
              <param-value>/WEB-INF/applicationContext.xml</param-value>
          </init-param>
          <load-on-startup>1</load-on-startup>
      </servlet>
      <servlet-mapping>
          <servlet-name>MyProject</servlet-name>
          <url-pattern>/</url-pattern>
      </servlet-mapping>
      
       </web-app>
      

      在applicationContext.xml中:

      In applicationContext.xml:

      <context:annotation-config />
      <context:component-scan base-package="main.mypack.controller" />
      
      
      
      <bean
          class="org.springframework.web.servlet.view.InternalResourceViewResolver">
          <property name="prefix" value="/WEB-INF/jsp/" />
          <property name="suffix" value=".jsp"/>
      </bean>
      

      在我的jsp文件中:href =/ assets / css / jquery.mobile.fixedToolbar.polyfill .css

      And in my jsp file: href="/assets/css/jquery.mobile.fixedToolbar.polyfill.css"

      不工作,任何帮助?

      编辑:我使用的是2.5 Spring版本,我有错误像:找不到与名称为'MyProject'的DispatcherServlet中的URI [/MyProject/assets/js/jqm-project.js]的HTTP请求

      I'm using 2.5 Spring version, and I have errors like: No mapping found for HTTP request with URI [/MyProject/assets/js/jqm-project.js] in DispatcherServlet with name 'MyProject'

      推荐答案

      问题是,你的CSS和JS文件的请求正在通过Dispatcher Servlet,这是不正确的。因此,Spring将不会找到那些不会加载它们的文件的映射。

      The problem is that your requests for CSS and JS files are going through Dispatcher Servlet, which is not correct. Hence Spring won't find the mapping for those files it will not load them.

      您需要在applicationContext.xml文件中为应用程序添加resourceHandler,如下所示。此配置将绕过Dispatcher Servlet对CSS和JS文件的请求。

      You need to add the resourceHandler for your application in the applicationContext.xml file as follows. This configuration will bypass the requests for CSS and JS files from the Dispatcher Servlet.

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

      希望这有助于你...干杯。

      Hope this helps you... Cheers.

      请将以下代码放在web.xml中

      Please put following code in web.xml

      <servlet-mapping>
        <servlet-name>default</servlet-name>
        <url-pattern>*.css</url-pattern>
       </servlet-mapping>
      
       <servlet-mapping>
          <servlet-name>default</servlet-name>
          <url-pattern>*.js</url-pattern>
       </servlet-mapping>
      

      这篇关于Spring没有找到资源文件(css,jsp ...)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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