【问题标题】:Running script files and use of Css files with Spring MVC?使用 Spring MVC 运行脚本文件和使用 Css 文件?
【发布时间】:2013-04-05 10:29:48
【问题描述】:

我正在开发一个 Spring MVC 应用程序,我有我的登录页面 (login.jsp),它应该调用脚本并链接一些 css 文件。问题是应用程序不运行脚本。

这是我的文件;

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>/ressources/js/*</url-pattern>
 </servlet-mapping>

我的 mvc 调度器 spring 文件:

    <mvc:resources mapping="/resources/**" location="/ressources/" /> 
    <mvc:resources mapping="/scripts/**" location="/ressources/js/" /> 

   <bean      class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />
     <bean
    class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
     </bean>
        <bean
          class="org.springframework.web.servlet.view.InternalResourceViewResolver">
          <property name="prefix">
            <value>/WEB-INF/pages/</value>
          </property>
          <property name="suffix">
            <value>.jsp</value>
          </property>
        </bean>
    </beans>

Login.jsp 调用脚本文件和 css 文件

<% String init="<c:url value='/resources/js/init.js'>";%>
<% String grid="<c:url value='/resources/css/5grid/init.js?use=mobile,desktop,1000px&mobileUI=1&mobileUI.theme=none&mobileUI.titleBarOverlaid=1&viewport_is1000px=1060'>";%>
<% String query="<c:url value='/resources/js/jquery-1.8.3.min.js'>";%>
<% String drop="<c:url value='/resources/js/jquery.dropotron-1.2.js'>";%>

<link href="http://fonts.googleapis.com/css?family=Source+Sans+Pro:300,400,700,900,300italic" rel="stylesheet" />
<script src="http://code.jquery.com/jquery-1.8.3.min.js"></script>
<script src="<%=grid%>"></script>
<script src="<%=drop%>"></script>
<script src="<%=init%>"></script>
<noscript>
    <link rel="stylesheet" href="<c:url value='/resources/css/5grid/core.css' />"/>
    <link rel="stylesheet" href="<c:url value='/resources/css/5grid/core-desktop.css' />"/>
    <link rel="stylesheet" href="<c:url value='/resources/css/5grid/core-1200px.css' />"/>
    <link rel="stylesheet" href="<c:url value='/resources/css/5grid/core-noscript.css' />"/>
    <link rel="stylesheet" href="<c:url value='/resources/css/style.css' />"/>
    <link rel="stylesheet" href="<c:url value='/resources/css/style-desktop.css' />"/>
</noscript>

显示页面时,没有使用样式,只显示组件,因为没有脚本,也没有 css 文件。

任何人都可以解决我的问题吗? 谢谢你

【问题讨论】:

    标签: java css jsp spring-mvc scripting


    【解决方案1】:

    在您的资源映射中,您拼写错误的资源。它不包含两个 s 字符。

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

    相对于:

      <mvc:resources mapping="/resources/**" location="/ressources/" /> 
      <mvc:resources mapping="/scripts/**" location="/ressources/js/" />
    

    请注意,当您加载 css 和脚本时,它们位于 resource 目录下,而不是 ressources

    <link rel="stylesheet" href="<c:url value='/resources/css/style-desktop.css' />"/> 
    
    <% String drop="<c:url value='/resources/js/jquery.dropotron-1.2.js'>";%>
    

    调度程序 servlet 将尝试处理此请求,而不是让它们通过。

    同时更改 web.xml 文件中的调度程序 servlet 映射。

      <servlet-mapping>
        <servlet-name>mvc-dispatcher</servlet-name>
        <url-pattern>/*</url-pattern>
      </servlet-mapping>
    

    映射到/ 将导致调度程序处理所有请求,而映射到/* 将导致调度程序处理所有未映射到资源的请求。

    【讨论】:

    • 不,这不是问题 :( 显示图片并且它们与加载 .js 文件和 css 文件的问题相同。谢谢
    • 我认为这个问题肯定会成为一个问题,因为脚本位于资源目​​录下并且调度程序不允许请求通过。我有另一个预感,但你能发布你的 web.xml 文件吗?
    • 是的,我会将其作为答案发布
    • 注意到web.xml中的resourcefilter在拼写资源时使用了两个s字符?
    • 是的,我修复了这个问题,我只用 s 重命名了文件夹以避免此类问题
    【解决方案2】:

    web.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
    
    http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    
    id="WebApp_ID" version="2.5">
      <display-name>Spring MVC Application</display-name>
      <servlet>
        <servlet-name>mvc-dispatcher</servlet-name>
        <servlet-class>
                        org.springframework.web.servlet.DispatcherServlet
                    </servlet-class>
        <load-on-startup>1</load-on-startup>
      </servlet>
      <servlet-mapping>
        <servlet-name>mvc-dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
      </servlet-mapping>
      <listener>
        <listener-class>
                      org.springframework.web.context.ContextLoaderListener
                    </listener-class>
      </listener>
      <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
                /WEB-INF/mvc-dispatcher-servlet.xml,
                /WEB-INF/spring-security.xml
            </param-value>
      </context-param>
      <filter>
        <filter-name>springSecurityFilterChain</filter-name>
        <filter-class>
                      org.springframework.web.filter.DelegatingFilterProxy
                    </filter-class>
      </filter>
      <filter-mapping>
        <filter-name>springSecurityFilterChain</filter-name>
        <url-pattern>/*</url-pattern>
      </filter-mapping>
    
    <servlet>
    <servlet-name>Resource Servlet</servlet-name>
    <servlet-class>org.springframework.js.resource.ResourceServlet</servlet-class>
    </servlet>
    <servlet-mapping>
    <servlet-name>Resource Servlet</servlet-name>
    <url-pattern>/resources/*</url-pattern>
    </servlet-mapping>
    </web-app>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-02-19
      • 2016-05-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-17
      相关资源
      最近更新 更多