简体   繁体   English

显示jsp后,在jsp中显示java列表数据

[英]Display java list data in jsp after jsp is displayed

I am new to Java EE programming. 我是Java EE编程的新手。 Following my understanding on jsp. 根据我对jsp的理解。 Corret me if I am wrong 如果我错了,请告诉我
- JSP pages are converted to servlet first then to html and resulted html page is displayed in browser. - 首先将JSP页面转换为servlet,然后转换为html,并在浏览器中显示生成的html页面。

Now suppose jsp page is displayed in browser ie now it is html page and I have a java List which have names or some sort of data that I want to print on the currently loaded page. 现在假设jsp页面显示在浏览器中,即现在它是html页面,我有一个java列表,其中包含我想要在当前加载的页面上打印的名称或某种数据。 I can get the List object using ajax but the how will I display it on html as html cant render java collections. 我可以使用ajax获取List对象但是我将如何在html上显示它,因为html无法呈现java集合。

Correct me wherever I misunderstood the flow or basic concepts. 在我误解流程或基本概念的任何地方纠正我。

Thanks. 谢谢。

You could use ajax (using jQuery would be easy) to make a call to your Servlet 你可以使用ajax(使用jQuery很容易)来调用你的Servlet

function callMe(){
    $.ajax({
      type: "POST",
      url: "/someServlet",
      data: { param1: "val1" , param2: "val2" }
    }).done(function( data) {
       //TODO
    });
}

Now on Servlet, in doPost() , Use Gson to generate JSON representation for your collection 现在在Servlet上,在doPost() ,使用Gson为您的集合生成JSON表示

String parameter1 = request.getParameter(param1);
String parameter2 = request.getParameter(param2);
//call to service to generate the collection
//for example List<Employee>
List<Employee> employees  = someService(parameter1, parameter2);
//using google's gson
Gson gson = new Gson();
String json = new Gson().toJson(employees);
response.setContentType("application/json");
response.setCharacterEncoding("UTF-8");
response.getWriter().write(json);

Now we have response in javascript function as a array of javascript objects, So modify it to 现在我们在javascript函数中将响应作为javascript对象的数组,因此将其修改为

    }).done(function( data) {
        //some processing for display
        var len = data.length
        for (var i=0; i<len; ++i) {
               var employeeFirstName =  data[i].firstName;
               var employeeLastName =  data[i].lastName;
               //set it to some DIV, or do the processing you want
        }
      }

    });

Also See 另见

You need to send the contents of your list as text to the user's browser (which is what normally happens). 您需要将列表中的内容作为文本发送到用户的浏览器(通常会发生这种情况)。

A convenient format for transferring the contents of the list between the browser and the server is JSON , due to its simple readability with JavaScript and easy generation on the server. 用于在浏览器和服务器之间传输列表内容的便捷格式是JSON ,因为它具有JavaScript的简单可读性并且易于在服务器上生成。

You can then display the returned text in whatever way you like using JavaScript. 然后,您可以使用JavaScript以任何您喜欢的方式显示返回的文本。

A JSP is compiled into a java servlet class, which can handle HTTP requests. JSP被编译成java servlet类,可以处理HTTP请求。 When the servlet is deployed to an application server HTTP requests are passed to the servlet for handling: an HTTP response is generated which normally contains some HTML, a status code, etc. 当servlet部署到应用程序服务器时,HTTP请求被传递给servlet进行处理:生成HTTP响应,通常包含一些HTML,状态代码等。

So it's the java code in the servlet which loops over your list and presumably generates the appropriate HTML to render that list in a browser. 因此,servlet中的java代码循环遍历列表,并且可能生成适当的HTML以在浏览器中呈现该列表。

Whether or not it's an AJAX request or not doesn't really matter. 它是否是一个AJAX请求并不重要。 Rather than rendering a full HTML page, the AJAX request would probably be handled by a different servlet, which generates only a partial page - perhaps just the <ul><li>...</li></ul> to render your list. AJAX请求可能由不同的servlet处理,而不是呈现完整的HTML页面,该servlet只生成部分页面 - 可能只是<ul><li>...</li></ul>来呈现名单。 The javascript in your HTML page can then take care of updating the user interface by replacing the old version of the list. 然后,HTML页面中的javascript可以通过替换旧版本的列表来更新用户界面。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM