2017-10-22 61 views
0

我已经为了调用使用Spring MVC一个javascript表单JSP尝试不同的方法来调用JSP JavaScript中,所有的失败:无法使用Spring MVC的

  1. Spring MVC – How to include JS or CSS files in a JSP page
  2. Spring MVC 4.2.2 – Best way to Add/Integrate JS, CSS and images into JSP file using ‘mvc:resources mapping’
  3. StackOverflow: How to include js and CSS in JSP with spring MVC
  4. StackOverflow: Spring MVC - include static files/javascript , css
  5. StackOverflow: How include an external JS file in a JSP page
  6. StackOverflow: STS Spring MVC: How to include a JS file in a JSP

这是我的项目结构:

Project structure

而且index.jsp的:

<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> 
<!DOCTYPE html> 
<html> 
<head> 
<script src="<c:url value="/WEB-INF/resources/scripts.js" />"></script> 
</head> 
<body> 
<button onclick="hello()">Click here...</button> 
</body> 
</html> 

这里是调度员servlet.xml中:

<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:mvc="http://www.springframework.org/schema/mvc" 
    xmlns:context="http://www.springframework.org/schema/context" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation=" 
     http://www.springframework.org/schema/beans 
     http://www.springframework.org/schema/beans/spring-beans.xsd 
     http://www.springframework.org/schema/mvc 
     http://www.springframework.org/schema/mvc/spring-mvc.xsd 
     http://www.springframework.org/schema/context 
     http://www.springframework.org/schema/context/spring-context.xsd"> 

    <context:component-scan base-package="com.javatechig.controller" /> 

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

    </bean> 

    <mvc:resources mapping="/resources/**" location="/WEB-INF/resources/" /> 
    <mvc:annotation-driven /> 

</beans> 

网.xml就像休闲游戏一样:

<!DOCTYPE web-app PUBLIC 
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" 
"http://java.sun.com/dtd/web-app_2_3.dtd" > 

<web-app> 
    <display-name>Archetype Created Web Application</display-name> 
    <context-param> 
     <param-name>contextConfigLocation</param-name> 
     <param-value>/WEB-INF/dispatcher-servlet.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>1</load-on-startup> 
    </servlet> 

    <servlet-mapping> 
     <servlet-name>dispatcher</servlet-name> 
     <url-pattern>/</url-pattern> 
    </servlet-mapping> 
</web-app> 

的pom.xml:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> 
    <modelVersion>4.0.0</modelVersion> 
    <groupId>Test</groupId> 
    <artifactId>test</artifactId> 
    <packaging>war</packaging> 
    <version>0.0.1-SNAPSHOT</version> 
    <name>test Maven Webapp</name> 
    <url>http://maven.apache.org</url> 

    <properties> 
     <spring.version>4.2.1.RELEASE</spring.version> 
     <java.version>1.8</java.version> 
    </properties> 

    <dependencies> 
     <dependency> 
      <groupId>org.springframework</groupId> 
      <artifactId>spring-core</artifactId> 
      <version>${spring.version}</version> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework</groupId> 
      <artifactId>spring-webmvc</artifactId> 
      <version>${spring.version}</version> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework</groupId> 
      <artifactId>spring-web</artifactId> 
      <version>${spring.version}</version> 
     </dependency> 
     <dependency> 
      <groupId>junit</groupId> 
      <artifactId>junit</artifactId> 
      <version>3.8.1</version> 
      <scope>test</scope> 
     </dependency> 
     <dependency> 
      <groupId>javax.servlet</groupId> 
      <artifactId>jstl</artifactId> 
      <version>1.2</version> 
     </dependency> 
    </dependencies> 

    <build> 
     <finalName>test</finalName> 
    </build> 
</project> 

最后scripts.js中:

function hello(){ 
    alert('Hola'); 
} 

当我按下按钮,我得到这些错误:

  • 无法加载资源:服务器响应状态为404 (不是Fo UND)
  • 的ReferenceError:找不到变量:你好
+0

你有成功列入你的JSP的js脚本? –

回答

1

您添加的映射资源文件夹:

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

但你不会在视图中使用这个映射。请尝试更改此

<script src="<c:url value="/WEB-INF/resources/scripts.js" />"></script> 

<script src="<c:url value="/resources/scripts.js" />"></script>