【问题标题】:Unable to call javascript in jsp using spring mvc无法使用spring mvc在jsp中调用javascript
【发布时间】:2018-04-02 20:42:30
【问题描述】:

我尝试了不同的方法来使用 Spring MVC 调用 javascript 表单 jsp,但都失败了:

  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

这是我的项目结构:

还有 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>

这里是dispatcher-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>

web.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 (未找到)
  • ReferenceError:找不到变量:你好

【问题讨论】:

  • 你的jsp中成功包含js脚本了吗?

标签: spring jsp spring-mvc model-view-controller


【解决方案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>

【讨论】:

    猜你喜欢
    • 2013-10-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-15
    • 1970-01-01
    相关资源
    最近更新 更多