java 字符串生成器转成html表格格式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15842683/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
String builder into html table format
提问by Curious_Bop
I'm creating a servlet to display a front end of a little program I've produced, In this program I have a LinkedList call Execution Queue that I place in a string builder.
我正在创建一个 servlet 来显示我制作的一个小程序的前端,在这个程序中,我有一个 LinkedList 调用执行队列,我把它放在一个字符串生成器中。
public String getJobsForPrint() {
Iterator<JobRequest> it = ExecutionQueue.iterator();
StringBuilder result = new StringBuilder();
String NEW_LINE = System.getProperty("line.separator");
while (it.hasNext()) {
JobRequest temp = it.next();
result.append(this.getClass().getName()).append(" Object {").append(NEW_LINE);
result.append(" User ID: ").append(temp.getUserID());
result.append(" Start Date: ").append(temp.getStartDate());
result.append(" End Date: ").append(temp.getEndDate());
result.append(" Deadline Date: ").append(temp.getDeadDate());
result.append(" Department: ").append(temp.getDepartment());
result.append(" Project Name: ").append(temp.getProjectName());
result.append(" Project Application: ").append(temp.getProjectApplication());
result.append(" Priority: ").append(temp.getPriority());
result.append(" Cores: ").append(temp.getCores());
result.append(" Disk Space: ").append(temp.getDiskSpace());
result.append(" Analysis: ").append(temp.getAnaylsis()).append(NEW_LINE);
result.append("}");
}
return result.toString();
on my servlet side I call the string by:
在我的 servlet 端,我通过以下方式调用字符串:
protected void processExecutionQueue(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
PrintWriter out = response.getWriter();
out.println("<!DOCTYPE html>");
out.println("<html>");
out.println("<head>");
out.println("<title>Execution Queue</title>");
out.println("</head>");
out.println("<body>");
out.println("<div>");
out.println("<div style='position:absolute; top:20px; right: 20px;'><a href='/ProjectAndBackend/index.jsp'>LOGOUT</a></div>");
out.println("</div>");
out.println("<p>Queue:</p>");
out.println("Execution Queue:" + SystemServlet.getScheduler().getJobsForPrint());
out.println("</body>");
out.println("</html>");
}
So now I display strings after each other with all the data taken from the linkedlist. I want to on the webpage side, be able to take that data and put it into a table so that it looks neater and not just strings tossed onto the webpage.
所以现在我用从链表中获取的所有数据一个接一个地显示字符串。我希望在网页方面,能够获取该数据并将其放入表格中,使其看起来更整洁,而不仅仅是将字符串扔到网页上。
How would I implement a html to show the specific elements of the string in certain aspects So with example below I have the headers then where the data is take the element from the string and keep writing it out until all the data from the iterator displayed
我将如何实现一个 html 以在某些方面显示字符串的特定元素所以在下面的示例中,我有标题然后数据从字符串中获取元素并继续写出它直到显示迭代器中的所有数据
<table border="1">
<tr>
<th>User ID</th>
<th>Start Date</th>
</tr>
<tr>
<td>User ID DATA</td>
<td>Start Date DATA</td>
</tr>
Or if anyone can direct me to an example as I can't find any with my current searches.
或者,如果有人可以指导我举个例子,因为我目前的搜索找不到任何例子。
回答by Joseph Selvaraj
When you build the StringBuilder use HTML tags to place each in a row.
当您构建 StringBuilder 时,使用 HTML 标记将每个标记放在一行中。
Code should be something like this
代码应该是这样的
StringBuilder result = new StringBuilder();
result.append("<tr><td>").append(User ID DATA).append("</td><td>").append(Start Date DATA).append("</td></tr>");
Note:
笔记:
1.You need to create the base html and table header columns inside processExecutionQueue()
method.
2. Only the data row needs to be created in getJobsForPrint()
method.
1.您需要在方法中创建基本的 html 和表格标题列processExecutionQueue()
。2.方法中只需要创建数据行getJobsForPrint()
。
So when the result is passed from getJobsForPrint()
it will be embedded into other HTML files.
所以当结果从getJobsForPrint()
它传递过来时,它会被嵌入到其他 HTML 文件中。
Hope you can complete the code with this suggestion.
希望你能用这个建议完成代码。