2017-09-24 69 views
2

当我加载我的JSP时,出现了一个奇怪的错误。 Spring是一个Java框架,所以为什么我需要一个JavaScript文件的映射?我的页面中的JavaScript不起作用。Spring框架 - 没有为JavaScript文件找到映射

org.springframework.web.servlet.PageNotFound.noHandlerFound No mapping found for HTTP request with URI [/ghs1986/js/javascript.js] in DispatcherServlet with name 'ghs1986' 

为什么它的价值,这里是我的applicationContext.xml。

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework/schema/context" 
    xmlns:mvc="http://www.springframework.org/schema/mvc" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
     http://www.springframework.org/schema/beans/spring-beans-4.3.xsd 
     http://www.springframework.org/schema/context 
     http://www.springframework.org/schema/context/spring-context-4.3.xsd 
     http://www.springframework.org/schema/mvc 
     http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd"> 
    <mvc:annotation-driven /> 

    <mvc:resources location="WEB-INF/pages/images/" mapping="/images/**" /> 

    <mvc:view-controller path="/" view-name="home" /> 

    <bean id="viewResolver" 
     class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
     <property name="prefix" value="/WEB-INF/pages/" /> 
     <property name="suffix" value=".jsp" /> 
    </bean> 

这是如何我试图访问的JavaScript在我的JSP。

<script type="text/javascript" src="js/javascript.js"></script> 

这里是我的web.xml。

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE xml> 
<web-app version="4.0" 
     xmlns="http://java.sun.com/xml/ns/javaee" 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee 
      http://java.sun.com/xml/ns/javaee/web-app_4_0.xsd"> 
    <display-name>Granada High School Class of 1986</display-name> 

    <servlet> 
     <servlet-name>ghs1986</servlet-name> 
     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
     <load-on-startup>1</load-on-startup> 
    </servlet> 

    <servlet-mapping> 
     <servlet-name>ghs1986</servlet-name> 
     <url-pattern>/</url-pattern> 
    </servlet-mapping> 

    <listener> 
     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
    </listener> 

    <welcome-file-list> 
     <welcome-file>home.jsp</welcome-file> 
    </welcome-file-list> 
</web-app> 

回答

2

在几乎相同的方式,你有一个映射,以允许弹簧控制器知道images

<mvc:resources location="WEB-INF/pages/images/" mapping="/images/**" /> 

你需要添加一个类似的映射,让它知道你的JavaScript文件。喜欢的东西,

<mvc:resources location="WEB-INF/pages/js/" mapping="/js/**" /> 

的问题是在这两种情况下是相同的,春的前端控制器不知道如何找到你的js文件,但不认为。

+0

这工作。感谢您的帮助。 –

+1

@FrankSerkland,如果可行,那么你应该接受并且赞成答案。 –

+0

堆栈溢出让我立即开始。 –