我正在研究一個小型消息傳遞Web應用程序以學習jsp和servlet。我有一個具有以下屬性的MessageModel類:即使出現getter時也會出現PropertyNotFoundException
String toAddress ;
String fromAddress;
String messageSubject;
String messageContent;
Timestamp messageTime;
int messageDraft;
以及它們對應的setter,供jsp訪問。
我有一個方法,查詢數據庫的所有消息,收到消息接收到特定的地址顯示。然後它創建一個新的對象並將其存儲在ArrayList中。
public static ArrayList<MessageModel> getReceivedMessages(String toAddress) throws SQLException, ClassNotFoundException{
// creates a arraylist.
ArrayList<MessageModel> msgList = new ArrayList<MessageModel>();
// Database connection code..
// The query which gets the required messages from the database and adds them to the list.
String query = "SELECT * FROM messages WHERE msg_to='" + toAddress +"' ORDER BY msg_date DESC";
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(query);
if(rs.next()){
while(rs.next()){
String msgTo = rs.getString("msg_to");
String msgFrom = rs.getString("msg_from");
String msgSub = rs.getString("msg_subject");
String msgCnt = rs.getString("msg_matter");
Timestamp msgTime = rs.getTimestamp("msg_date");
MessageModel model = new MessageModel(msgTo,msgFrom,msgSub,msgCnt,msgTime);
msgList.add(model);
}
}
return msgList;
}
我打電話從JSP此方法,並通過使用
String userName = (String) session.getAttribute("userId");
ArrayList<MessageModel> list = MessageModel.getReceivedMessages(userName);
pageContext.setAttribute("messageList", list);
列表保存爲一個屬性,但是當我通過<c:forEach>
標籤訪問該列表中的MesasgeModel對象的FROMADDRESS財產,我出現以下錯誤:
javax.el.PropertyNotFoundException: Property 'fromAddress' not found on type com.email.system.MessageModel
這是訪問打印出每個MessageModel
對象的內容的HTML的一部分。
<c:forEach items="${messageList}" var="message">
<li><c:out value="${message.toAddress}"/>
<c:out value="${message.messageSubject}"/> <c:out value="${message.messageTime}"/>
<c:out value="${message.messageContent}"/> <c:out value="${message.fromAddress}"/> </li>
</c:forEach>
每當我嘗試訪問toAddress
字段時都會發生錯誤。 toAddress屬性被存儲在messageSubject
字段中,即使當我在mySql控制檯中嘗試相同的查詢時,我也會在它們各自的列中獲取正確的字段。
的exapmle結果到我刪除了內容列的查詢,
msg_id msg_to msg_from msg_subject msg_date msg_is_draft
4 bigb remember *subject* 2014-10-07 11:01:53 0
2 bigb remember *subject* 2014-10-07 10:48:43 0
1 bigb remember *subject* 2014-10-07 10:48:31 0
編輯:這是MessageModel
類我。
public class MessageModel {
String toAddress ;
String fromAddress;
String messageSubject;
String messageContent;
Timestamp messageTime;
int messageDraft;
public String getToAddress() {
return toAddress;
}
public String getFromAddress() {
return fromAddress;
}
public String getMessageSubject() {
return messageSubject;
}
public String getMessageContent() {
return messageContent;
}
public Timestamp getMessageTime() {
return messageTime;
}
public int getMessageDraft() {
return messageDraft;
}
public MessageModel(String toAddress,String fromAddress, String messageSubject, String messageContent,Timestamp messageTime){
this.toAddress = toAddress;
this.messageSubject = messageSubject;
this.messageContent = messageContent;
this.messageTime = messageTime;
this.fromAddress = fromAddress;
}
public void sendMessage(MessageModel model){
//Gets the related properties from the objects and stores it in the database
}
public static ArrayList<MessageModel> getReceivedMessages(String toAddress){
//Gets the messages sent to 'toAddress'
}
}
發佈您的'MessageModel'類。這些屬性來自getter/setters而不是來自field的名稱。所以你應該有'toAddress'屬性的'getToAddress()'方法。 – 2014-10-07 06:19:38
我使用intelliJ,我使用代碼插入工具來創建類的setter。我理解一個jsp如何根據另一個SO問題的JavaBeans規範來要求這些。 – Bhargav 2014-10-07 06:26:56
一如既往的這些問題,嘗試一個完整的清理,重建,重新部署。 – Magnilex 2014-10-07 06:31:29