2013-04-08 44 views
1

我是JSP,外部的jquery文件在春季MVC中不工作?

<%@ page language="java" contentType="text/html; charset=UTF-8" 
    pageEncoding="UTF-8"%> 
<%-- <%@include file="include.jsp"%> --%> 

<%@ taglib prefix="form" uri="../tld/spring-form.tld" %> 
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
<html> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
<title>Insert title here</title> 
    <script type="text/javascript" src="/jquery/jquery-1.9.1.js"></script> 
    <script type="text/javascript" src="/jquery/hello.js"></script> 

</head> 
<body> 
<%out.println("Hello") ;%> 
<form:form commandName="person"> 
     Select A or B : 
     <form:input path="option" id="option" /> 
     <input type="button" value="Ajax Submit" onclick="hello();"> 

     <p id = "result"></p> 

</form:form> 
</body> 
</html> 

out.println("Hello")打印在浏览器和形式也displayed.Only外部jQuery是没有工作做的JavaScript调用。

我的调度员的Servlet会,

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

我的外部hello.js包含,

function hello(){ 
    alert("Hello 99"); 
    var val = $("#option").val(); 
    alert("Option : "+val); 
} 

我的项目结构,

enter image description here

当我点击按钮我在控制台

中得到这个
Apr 8, 2013 9:03:08 PM org.springframework.web.servlet.DispatcherServlet noHandlerFound 
WARNING: No mapping found for HTTP request with URI [/SampleChat/jquery/hello.js] in DispatcherServlet with name 'dispatcher' 
Apr 8, 2013 9:03:09 PM org.springframework.web.servlet.DispatcherServlet noHandlerFound 
WARNING: No mapping found for HTTP request with URI [/SampleChat/jquery/jquery-1.9.1.js] in DispatcherServlet with name 'dispatcher' 

我需要调用JavaScript函数来获取警报。

好的答案是肯定赞赏的。

+0

显示我没有看到你的JSP任何'' Givi 2013-04-08 15:31:51

+0

我添加了外部脚本。请参阅'和 '' – 2013-04-08 15:32:35

回答

0

你在哪里madeAjaxCall()在js文件中?

我认为你正在尝试做这个

<input type="button" value="Ajax Submit" onclick="hello();"> 

你的代码看起来不错。

+0

对不起,错误地编码和改变,但仍然不工作 – 2013-04-08 15:38:25

+0

你能看到我的项目结构吗? – 2013-04-08 15:40:27

+0

添加$(document).ready(function(){alert(“test”)}) ;在你的js文件中,如果alert alert你会知道js是包含在你的jsp中的 – commit 2013-04-08 15:42:28

0

检查,如果这能帮助你(把它放在你的调度XML):

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

采用c标记库

<%@ taglib prefix='c' uri='http://java.sun.com/jstl/core_rt'%> 

和脚本标记如下

<script type="text/javascript" src='<c:url value="/jquery/jquery-1.9.1.js"></c:url>'></script> 
<script type="text/javascript" src='<c:url value="/jquery/hello.js"></c:url>'></script> 

尝试检查你是否已经配置你的调度servlet到韩DLE使用以下URL模式的所有请求

<servlet-mapping> 
    <servlet-name>dispatcher</servlet-name> 
    <url-pattern>/*</url-pattern> 
</servlet-mapping> 

,那么你需要添加由@Dani