【问题标题】:Spring MVC how to load a js from a jspSpring MVC如何从jsp加载js
【发布时间】:2016-11-18 12:03:10
【问题描述】:

我使用 Spring MVC。我有一个动态 web 项目 Eclipse

结构文件夹是

我有以下jsp actualizarCorreo.jsp

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>
<!DOCTYPE html>
<html>
<head>
<title>EusurveyAdmin menu princiapl</title>

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

<script type="text/javascript">
function pruebaB()
{
   alert('en prueba');
}
</script>
</head>
<body>
    <c:set var="pageNumber" value="${pageNumber}" />
    <table class="actualizarCorreos">

        <tbody>
        <a href="#" onclick="prueba()"; return false;">
            <input type="radio" onclick="prueba()" class="check" id="id1"
                name="id1" value="true" />
            <spring:message code="label.ModificarCorreos" />



        </tbody>
    </table>

</body>

在这个jsp中我加载了另一个js

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

这个js是路径WebContent/resources/js。

properties.js 是

alert('cargado properties');
function checkConfirmationPage()
{       
    alert('en check');
    if ($("#conflink").is(":checked"))
    {} else {};
}

function prueba()
{
    alert('en prueba');
}

单击单选按钮时,我从actualizarCorreo.jsp 调用函数prueba

<input type="radio" onclick="prueba()" class="check" id="id1"
                name="id1" value="true" />
            <spring:message code="label.ModificarCorreos" />

我没有看到警报消息函数prueba。

怎么了?

【问题讨论】:

    标签: javascript spring-mvc jspinclude


    【解决方案1】:

    试试这个

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

    或使用以下内容作为参考并找到确切的路径:

       /   = Root directory
       .   = This location
       ..  = Up a directory
       ./  = Current directory
       ../ = Parent of current directory
       ../../ = Two directories backwards
    

    或者直接获取上下文路径来获取.js

    <script type="text/javascript" src="<%=request.getContextPath()%>/resources/js/properties.js" >
    </script>
    

    【讨论】:

    • 我尝试了其他路径并收到消息错误 我认为路径是正确的
    • 我提出了另一种选择。请尝试告诉我。
    【解决方案2】:

    在 mvc 中使用 Spring 框架时,您必须在 dispatcher-servlet.xml 中提供如下资源路径。

    < mvc:resources mapping="/resources/**" location="/resources/" />
    

    然后该文件夹将被允许用于资源。你可以称它们为

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

    【讨论】:

    • 我已经将此行添加到我的调度员。如何检查我的 js 是否已加载?
    • 使用浏览器手动加载网址。要么 url 错误,要么文件不在该路径中。
    • 使用工具->网页开发者->网页控制台查看js错误。
    • 我从 Eclipse 中执行了我的程序。如何从 Eclipse 浏览器执行此操作?
    • 在 Eclipse 中将浏览器更改为 mozilla(如果已安装),在首选项 -> 常规 -> Web 浏览器中,有使用内部 Web 浏览器。并且您可以检查“FireFox”。
    猜你喜欢
    • 1970-01-01
    • 2017-10-19
    • 2017-03-08
    • 2015-05-25
    • 2011-12-18
    • 2016-04-29
    • 1970-01-01
    • 1970-01-01
    • 2014-12-04
    相关资源
    最近更新 更多