2

我正在开发一个 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 文件。

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

4

2 回答 2

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>

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

于 2013-04-05T12:05:50.107 回答
0

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>
于 2013-04-05T12:48:39.417 回答