javax.el.PropertyNotFoundException:在类型 java.lang.String 上找不到属性“tname”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10855288/
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
javax.el.PropertyNotFoundException: Property 'tname' not found on type java.lang.String
提问by Maninder
I was using scriptlets earlier, but now I switched to the mvc. I am not able to retrieve values on to the JSP page and getting errors:
我之前在使用 scriptlet,但现在我切换到 mvc。我无法在 JSP 页面上检索值并出现错误:
javax.el.PropertyNotFoundException: Property 'tname' not found on type java.lang.String
Code of the Bean:
豆子代码:
public class regForm extends org.apache.struts.validator.ValidatorForm implements Iprafunctions {
private String tname = null;
private String tfee = null;
public String getTfee() {
return tfee;
}
public void setTfee(String tfee) {
this.tfee = tfee;
}
public String getTname() {
return tname;
}
public void setTname(String tname) {
this.tname = tname;
}
public regForm() {
super();
}
}
Action controller:
动作控制器:
public ActionForward mvc(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
regForm reg = (regForm) form;
String sql = "Select tname,tfee from addtest order by tname";
ResultSet rs = SQLC.getData(sql, null);
Collection myBeans = new ArrayList();
while (rs.next()) {
String testname = rs.getString("tname");
String testfee = rs.getString("tfee");
reg.setTname(testname);
reg.setTfee(testfee);
myBeans.add(reg.getTname());
myBeans.add(reg.getTfee());
}
request.setAttribute("myBeans", myBeans);
return mapping.findForward(SUCCESS);
}
Access in JSP Page
在 JSP 页面中访问
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<table>
<tr><td>Name</td><td>Fee</td></tr>
<c:forEach var="reg" items="${myBeans}">
<tr>
<td><c:out value="${reg.tname}"></c:out></td>
<td><c:out value="${reg.tfee}"></c:out></td>
</tr>
</c:forEach>
</table>
采纳答案by raddykrish
I think you are adding the names and the fee directly to the arraylist, but you should be adding the whole regForm
object in the arraylist.
我认为您是将名称和费用直接添加到regForm
数组列表中,但您应该将整个对象添加到数组列表中。
Instead of the below code
而不是下面的代码
myBeans.add(reg.getTname());
myBeans.add(reg.getTfee());
you need to do like
你需要像
myBeans.add(reg);
moreover dont use the same object that you got from the form. Try to create new objects and add in the arraylist and try to use generics.
此外,不要使用从表单中获得的相同对象。尝试创建新对象并添加到数组列表中并尝试使用泛型。
EDIT:
编辑:
while (rs.next()) {
String testname = rs.getString("tname");
String testfee = rs.getString("tfee");
regForm beanObject = new regForm();
beanObject.setTname(testname);
beanObject.setTfee(testfee);
myBeans.add(beanObject);
}
回答by Jigar Joshi
Actually you are adding strings in your Collection
and you are trying to invoke
实际上,您正在添加字符串,Collection
并且您正在尝试调用
getTName()
by ${reg.tname}
getTName()
经过 ${reg.tname}
Either add whole bean to your collection or just replace JSTL with ${reg}
要么将整个 bean 添加到您的集合中,要么将 JSTL 替换为 ${reg}