简体   繁体   English

如何从JSP文件中的对象提取每个字段?

[英]How to extract each field from the object in JSP file?

I have a class which contains three list string as mentioned below - 我有一个包含三个列表字符串的类,如下所述-

public class Response {

private List<String> hash = new LinkedList<String>();
private List<String> name = new LinkedList<String>();
private List<Integer> count = new LinkedList<Integer>();

// getters and setters
}

Now I have a servlet from which I putting values in my Response object as shown below - 现在,我有了一个servlet,可以将其值放入响应对象,如下所示-

Response rr = new Response();

rr.setName(Arrays.asList("Test1", "test2", "Test3"));
rr.setHash(Arrays.asList("Test4", "Test5", "Test6"));
rr.setCount(Arrays.asList(0, 1, 2));

Now from that same servlet only, I am passing the above object values to my JSP page as mentioned below - 现在,仅从相同的servlet,将上述对象值传递到我的JSP页面,如下所述-

req.setAttribute("data", rr);
final RequestDispatcher dispatcher = req.getRequestDispatcher("/admin/testing.jsp");
dispatcher.forward(req, resp);

And below is my JSP page in which I am getting the value of that same object successfully. 下面是我的JSP页面,在该页面中我成功地获取了同一对象的值。 In the below JSP page, I am trying to extract the value of each field and putting it in its respective columns in the table - 在下面的JSP页面中,我试图提取每个字段的值并将其放在表格的相应列中-

Meaning, I have three columns in the below table in my JSP, first is Hash , second is Name and third is Count . 意思是,我的JSP下表中有三列,第一列是Hash ,第二列是Name ,第三列是Count Now I need to extract all the value of hash from that object in Hash column, and similarly extract all the value of name from that object in Name column and etc. 现在,我需要从“哈希”列中的该对象提取所有哈希值,并类似地从“名称”列等中的该对象提取所有name值。

<p>The data from servlet: ${data}</p>

<TABLE BORDER="1" CELLPADDING="3" CELLSPACING="1">
<TR>
<TH>Hash</TH>
<TH>Name</TH>
<TH>Count</TH>
</TR>
<% for (int i = 0; i < 3; i++) { %>
<TR>
<TD>
${data.getHash().get(<%=i%>)}
</TD>
<TD>
${data.getName().get(<%=i%>)}
</TD>
<TD>
${data.getCount().get(<%=i%>)}
</TD>
</TR>
<% } %>
</TABLE>

But everytime, it is giving me this exception - 但是每次都会给我这个例外-

interpolated runtime values are forbidden by
the JSP spec at '${data.getHash().get(<%=i%>)}'

Any idea what wrong I am doing? 知道我在做什么错吗? And how can I extract all the fields values from that object in my table? 以及如何从表中的该对象提取所有字段值?

Currently, all my three list in Response class will have only 3 elements. 目前,我在Response类中的所有三个列表都只有3个元素。

Final output will look like - 最终输出看起来像-

In the table, Hash column will have three data inside it, Name column will have three data inside it, Count column will have three data inside it.. 在表中,Hash列中将包含三个数据,Name列中将包含三个数据,Count列中将包含三个数据。

Use JSTL <c:forEach> in the presentation layer. 在表示层中使用JSTL <c:forEach>
I'd suggest you to change your structure like this: 我建议你这样改变你的结构:

public class Response {

private String hash;
private String name;
private Integer count;

// getters and setters
}

In the servlet 在servlet中

List<Response> list = new ArrayList<Response>();
Response rr = new Response();
rr.setHash("hash1");
rr.setName("name1");
rr.setCount(1);
list.add(rr);
Response rr2 = new Response();
rr2.setHash("hash2");
rr2.setName("name2");
rr2.setCount(2);
list.add(rr2);
req.setAttribute("data", list);

In JSP 在JSP中

<TABLE BORDER="1" CELLPADDING="3" CELLSPACING="1">
<TR>
   <TH>Hash</TH>
   <TH>Name</TH>
   <TH>Count</TH>
</TR>
<c:forEach var="rr" items="${data}">
<TR>
   <TD>${rr.hash}</TD>
   <TD>${rr.name}</TD>
   <TD>${rr.count}</TD>
</TR>
</c:forEach>
</TABLE>

You are mixing the el and scriplets in your jsp .It is recommended that use JSTL and el not scriplets . 您将jsp中的el和scriplets混合在一起。建议使用JSTL而不是el scriplets。

 <c:forEach var="i" begin="0" end="3">
    <TR>
     <TD>
        ${data.getHash().get(i)}
     </TD>
     <TD>
       ${data.getName().get(i)}
     </TD>
     <TD>
       ${data.getCount().get(i)}
     </TD>
   </TR>
</c:forEach>

There is a curious and confusing inconsistency in JSP syntax when non-String tag attributes are the results of JSP expressions. 当非String标记属性是JSP表达式的结果时,JSP语法中会有一个奇怪而令人困惑的不一致之处。 Let's suppose we want to pass an object of class examples.Values (a kind of list) to a tag extension. 假设我们想将一个类examples.Values(一种列表)的对象传递给标签扩展。 The syntax: 语法:

 <wrox:list values=”<%=values%>”

is problematic, because we know from the JSP specification that an expression “is evaluated and the result is coerced to a String which is subsequently emitted into the current out JspWriter object”. 这是有问题的,因为我们从JSP规范中知道,表达式“被求值,结果被强制为String,随后将其发送到当前的JspWriter对象中”。 In the case of the custom tag above, however, the value of the expression is not coerced to a String, but passed to the tag handler as its original type. 但是,在上述自定义标签的情况下,表达式的值不强制转换为String,而是作为其原始类型传递给标签处理程序。

Either iterate through scriptlet as, 要么通过scriptlet进行迭代,

<%for (int i = 0; i < 3; i++){ %>
<TR>
    <TD>
        <%=data.getHash().get(i) %>
    </TD>
    <TD>
        <%=data.getName().get(i) %>
    </TD>
    <TD>
        <%=data.getCount().get(i) %>
    </TD>
</TR>
<%} %>

or use JSTL as, 或使用JSTL作为,

<c:forEach var="i" begin="0" end="3">
<TR>
 <TD>
    ${data.getHash().get(i)}
 </TD>
 <TD>
   ${data.getName().get(i)}
 </TD>
 <TD>
   ${data.getCount().get(i)}
 </TD>
</TR>
</c:forEach>

But don't intermix them. 但是不要将它们混在一起。

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

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