Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/81.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用ajax从servlet检索java对象,并使用JSTL打印它_Java_Jquery_Ajax_Jsp_Jstl - Fatal编程技术网

使用ajax从servlet检索java对象,并使用JSTL打印它

使用ajax从servlet检索java对象,并使用JSTL打印它,java,jquery,ajax,jsp,jstl,Java,Jquery,Ajax,Jsp,Jstl,当jsp中的选项框发生更改时,我希望对我的TestServlet进行ajax post调用。当这个servlet从ajax检索到对象时,servlet将创建一个新的person对象,并将其发送回ajax调用的jsp页面 此时,我可以在一个具有uidrespon id的div中打印这个对象,但是如何使用JSTL out标记(如or)读取这个对象呢 从jsp页面到servlet的Ajax调用: 选择下拉列表: Servlet中的Post方法: JSTL仅在服务器端工作。Ajax在客户端工作。您要求的

当jsp中的选项框发生更改时,我希望对我的TestServlet进行ajax post调用。当这个servlet从ajax检索到对象时,servlet将创建一个新的person对象,并将其发送回ajax调用的jsp页面

此时,我可以在一个具有uidrespon id的div中打印这个对象,但是如何使用JSTL out标记(如or)读取这个对象呢

从jsp页面到servlet的Ajax调用:

选择下拉列表:

Servlet中的Post方法:


JSTL仅在服务器端工作。Ajax在客户端工作。您要求的是在ajax请求中从JSTL刷新HTML,这是不可能的

在响应的返回中以JSON格式或类似的格式写入所需的数据,而不是将其存储在ajax调用的请求属性中。然后,读取JavaScript代码中的响应,将其解析为JavaScript对象并使用它

<script> $(document).ready(function() {   
    $("body").on('change',
    '#personbox', function() {
  //get the selected value
  var selectedValue = $(this).val();

  $.ajax({            type    : 'POST',           url     : '${pageContext.request.contextPath}/secure/TestController.do',
        data  : { "ajaxCallBack" : selectedValue },
        success : function(response) {   
          $('#uidrespon').html(response); 
        },
        error : function(error) {
            alert(error);
        }
    });

  alert(selectedValue);   }); }); </script>
<select class="form-control" id="rulebox" name="selectPerson" onchange="Change()">
     <option value="">Select rule</option>
     <option value="${dePerson.name}">${dePerson.name}</option>
     <option value="${dePerson.name}">${dePerson.name}</option>
</select>
public void doPost(HttpServletRequest req, HttpServletResponse resp)
            throws IOException, ServletException {

        String name = req.getParameter("ajaxCallBack");

        Person nwePerson = new Person();
        nwePerson.setThePerson((List<Person>) getServletContext().getAttribute("allPersons"));

        Persons src = nweFact.findPerson(name);

        req.setAttribute("src", src); 
        resp.setCharacterEncoding("UTF-8"); 
        resp.getWriter().print(src);
    }