代码之家  ›  专栏  ›  技术社区  ›  Roy

如何使用document.ready函数调用servlet类

  •  1
  • Roy  · 技术社区  · 11 年前

    我创建了一个名为roleManagement.jsp的jsp文件:

    <%@page import="com.sun.org.apache.xalan.internal.xsltc.compiler.sym"%>
    <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
        pageEncoding="ISO-8859-1"%>
    <!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=ISO-8859-1">
            <title>Role Management</title>
            <script type="text/javascript" src="js/jquery-2.0.3.js"></script>
        </head>
        <body>
            <script type="text/javascript">
                $(document).ready(function() { 
                    alert("entered in trial button code");
    
                    $.ajax({
                        type: "GET",
                        url:"/jquery2xdemo/RoleManagementContoller",
                        dataType: "json",
                        success: function (data) {
                            $.each(data.aaData,function(i, obj) {
                                alert(obj.value + ":" + obj.text);
                                var div_data = "<option value=" + obj.value + ">" + obj.text + "</option>";
                                alert(div_data);
                                $(div_data).appendTo('#ch_user1'); 
                            });  
                        }
                    });
                });
            </script>
            <div id="div_source1">
                <select id="ch_user1" >
                    <option value="select"></option>
                </select>
            </div>
            <input type="button" id="id_trial" name="btn_trial" value="Trial Button..">
        </body>
    </html>
    

    在上面的代码中,我试图在 $(document).ready 作用但我无法做到这一点。我将web.xml中的servlet信息映射为:

    <servlet>
        <description></description>
        <display-name>RoleManagementContoller</display-name>
        <servlet-name>RoleManagementContoller</servlet-name>
        <servlet-class>com.programmingfree.controller.RoleManagementContoller</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>RoleManagementContoller</servlet-name>
        <url-pattern>/RoleManagementContoller</url-pattern>
    </servlet-mapping>
    

    但是,当我运行jsp页面时,它根本没有进入document.ready函数。我做错了什么。请帮忙。提前感谢

    2 回复  |  直到 11 年前
        1
  •  0
  •   AppLend    11 年前

    检查jQuery是否正确连接到页面。 以下建议可能有用:

    1. 尝试从google api中包含jQuery,例如。

    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.js"></script>

    1. 使用url标记库:

    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>

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

        2
  •  0
  •   Stunner    11 年前

    您需要将ajax调用url仅用作RoleManagementContoller,而不是/jquery2xdemo/RoleManagementConteller,因为您将url模式配置为/RoleManagementController