javax.el.PropertyNotFoundException: Property 'tname' not found on type java.lang.String

时光总嘲笑我的痴心妄想 提交于 2019-12-08 01:16:32

问题


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:

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

<%@ 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>

回答1:


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.

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);
    }



回答2:


Actually you are adding strings in your Collection and you are trying to invoke

getTName() by ${reg.tname}

Either add whole bean to your collection or just replace JSTL with ${reg}



来源:https://stackoverflow.com/questions/10855288/javax-el-propertynotfoundexception-property-tname-not-found-on-type-java-lang

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!