繁体   English   中英

PropertyNotFoundException:在类型X上找不到属性“ X”

[英]PropertyNotFoundException: Property 'X' not found on type X

我有viewprofile操作,用于显示用户的详细信息和交易记录。

History.java

public class History {

    private String transactionId;
    private Date transactionDate;
    private String movieTitle;
    private BigDecimal schedulePrice;
    private String mallName;
    private int scheduleCinema;
    private Date scheduleDate;
    private Time scheduleTime;

    // getters and setters
}

ViewProfileAction.java

public class ViewProfileAction extends ActionSupport implements SessionAware, RequestAware {
    private static final long serialVersionUID = 1L;

    private Map<String, Object> session;
    private Map<String, Object> request;

    @Override
    public String execute() throws Exception {      
        if(!session.containsKey("currentUserId")) {
            return "index"; // return to index if not logged in
        }

        String currentUserId = (String) session.get("currentUserId");       

        UserManager um = new UserManager();
        String registeredUserEmail = um.getCurrentUserDetail("user_email", currentUserId);
        Date registeredDate = um.getRegisteredDate(currentUserId);
        int totalTransactions = um.getTotalTransactions(currentUserId);

        List<History> historyList = new DatabaseManipulator().getTransactionHistory(currentUserId);

        request.put("registeredUserEmail", registeredUserEmail);
        request.put("registeredDate", registeredDate);
        request.put("totalTransactions", totalTransactions);
        request.put("historyList", historyList);

        return SUCCESS;
    }

    @Override
    public void setSession(Map<String, Object> session) {
        this.session = session;
    }

    @Override
    public void setRequest(Map<String, Object> request) {
        this.request = request;     
    }   
}

user-profile.jsp

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>

<body>
    <table class="displaytbl">
    <tr>
        <td class="maintitle" colspan="7">TRANSACTION HISTORY</td>
    </tr>
    <tr>
        <!-- column titles -->
    </tr>
    <c:choose>
        <c:when test="${historyList.isEmpty()}">
            <tr>
                <td class="norecord" colspan="7">NO RECORDED TRANSACTIONS</td>
            </tr>
        </c:when>

        <c:otherwise>
            <c:forEach var="history" items="historyList">
            <tr>
                <td>${history.transactionDate}</td>
                <td>${history.movieTitle}</td>
                <td>${history.schedulePrice}</td>
                <td>${history.mallName}</td>
                <td class="center">${history.scheduleCinema}</td>
                <td>${history.scheduleDate}</td>
                <td>${history.scheduleTime}</td>
            </tr>
            </c:forEach>
        </c:otherwise>
    </c:choose>
    </table>
</body>

struts.xml

<action name="viewprofile" class="com.mypackage.action.ViewProfileAction">
    <result>/user-profile.jsp</result>
    <result name="index" type="redirect">/index.jsp</result>
</action>

堆栈跟踪:

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

我不确定在History确实具有上述属性时,为什么会引发PropertyNotFoundException 如何解决这个问题?

似乎在Jsp页面的下一行出现问题。 history只会指向简单的String historyList而不是您的集合对象。

<c:forEach var="history" items="historyList">

下面的行实际上试图从history对象获取transactionDate ,它不过是a String onject

<td>${history.transactionDate}</td>

上面没有下面的内容,您可能需要使用<c:forEach var="history" items="${historyList}">

request.put("historyList", historyList);

您将完全绕过框架机制。 而不是使用request ,将私有属性与getter和setter一起使用:

public class ViewProfileAction extends ActionSupport implements SessionAware {
    private static final long serialVersionUID = 1L;

    private Map<String, Object> session;

    private List<History> historyList;
    private String        registeredUserEmail;
    private Date          registeredDate;
    private int           totalTransactions;

    /* Generate GETTERS AND SETTERS for above fields here */

    @Override
    public String execute() throws Exception {      
        if(!session.containsKey("currentUserId")) {
            return "index"; // return to index if not logged in
        }

        String currentUserId = (String) session.get("currentUserId");

        UserManager um = new UserManager();
        registeredUserEmail = um.getCurrentUserDetail("user_email", currentUserId);
        registeredDate = um.getRegisteredDate(currentUserId);
        totalTransactions = um.getTotalTransactions(currentUserId);

        historyList = new DatabaseManipulator().getTransactionHistory(currentUserId);

        return SUCCESS;
    }

    @Override
    public void setSession(Map<String, Object> session) {
        this.session = session;
    }    
}

另外, 由于安全问题和功能降低(对于JSTL和Struts2标签+ OGNL),也应避免使用JSP EL,这将是Struts2项目中的理想选择。

会话用户检查可以在Interceptor中执行一次,而无需将其置于项目的每个操作中。 阅读如何编写自己的Interceptor进行会话检查

暂无
暂无

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

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