Unable to call jsp function from jsp using Spring MVC

0

I have tried various ways to run a javascript from a jsp in a project using Spring MVC, all have failed:

  • Spring MVC - How to include JS or CSS files in a JSP page
  • This is the structure of the project:

    The index.jsp (specifically I want to call the hello () function that is located in an external js file):

    <%@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>
    

    Here is the 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>
    

    The 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>
    

    Finally scripts.js:

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

    When I press the button, the following errors appear:

    • Failed to load resource: the server responded to a status of 404 (Not Found)
    • ReferenceError: Can not find variable: hello
    asked by Oliver Soria 22.10.2017 в 06:02
    source

    1 answer

    0

    I got the answer from Stack Overflow in English, the problem was that it declared the path to a resource folder:

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

    However, I never used it. This line had to be changed:

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

    For this one and that's it:

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

    This is the link to the answer original in English.

        
    answered by 22.10.2017 / 22:08
    source