我正在NetBeans中使用Glassfish做一個小項目,這是我在Java EE上學習的一門課程。我有一個問題,我的Bean的getter方法返回一個空值,因此沒有提交到數據庫。該項目的前端是一個JSF頁面,這可能是問題的一部分。我懷疑它是bean,JSF頁面還是我的配置(什麼?我不知道!)這就是問題Bean'getters'沒有返回值
我對這個主題很新,所以請原諒我對行話和天真的缺乏主題!
我們的課程講師上傳了一個類似於我們需要創建的項目,所以我們都使用它作爲指導。他的作品非常完美,而我的作品完全相同,但我的作品卻沒有。
我不能發佈任何廣泛的代碼,以符合我們機構的規定,但我可以發佈一些片段。如果有什麼具體的東西需要發佈,我會盡我所能。
豆:
@Named(value="secure")
@SessionScoped
public class Post implements Serializable {
private String post;
private String recipient;
@EJB private PostLocal posts; //local interface
public Post() {
}
public String getRecipient() {
return recipient;
}
public void setRecipient(String recipient) {
this.recipient= recipient;
}
public List<Post> getPosts() {
return posts.getAllPosts();
}
public String getPost() {
return post;
}
public void setPost(String post) {
this.post = post;
}
public String submit() {
Post p = new Post();
byte[] encryptedMsg = p.encrypt(getPost(), "password"); //the post is encrypted, that's why it's stored as a byte array. getMessage returns null..
p.setRecipient(getRecipient()); //getRecipient returns null
p.setMessage(encryptedMsg);
posts.add(s);
return "index";
}
JSF頁面:
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core">
<h:head>
<title>Project</title>
</h:head>
<h:body>
<h:form>
<table>
<tr>
<td>
Recipient:
</td>
<td>
<h:inputText value="#{secure.recipient}"/>
</td>
</tr>
<tr>
<td>
Enter a message to post here:
</td>
<td>
<h:inputText value="#{secure.post}"></h:inputText>
</td>
</tr>
<tr>
<td> <h:commandButton action="#{secure.submit}" value="Submit" /> </td>
</tr>
</table>
<h:dataTable value="#{secure.posts}" var="thePosts">
<h:column>
<f:facet name="header">Name</f:facet>
#{thePosts.recipient}
</h:column>
<h:column>
<f:facet name="header">Comment</f:facet>
#{thePosts.post}
</h:column>
</h:dataTable>
</h:form>
</h:body>
任何疑問的話,請用一切手段請教!我完全堅持這一點(並且已經過去24小時),所以任何幫助都非常感謝!
非常感謝
我的想法是getRecipient和getPost方法可能會被調用,但它們只返回上面幾行聲明的未經實例化的post/recipient變量。所以也許這就是問題所在,但爲什麼會發生,我不知道。在任何人想知道的情況下,加密方法試圖調用getPost方法來獲取「post」(來自JSF表單的文本字符串),然後將返回值+密碼(當前硬編碼爲字符串,其值爲「password 「,然後用DES/MD5加密,並以byte []格式返回加密後的文章 – james246 2011-03-09 17:55:47
想想我自己解決了這個問題,一直在閱讀JSF大師BalusC的博客,並認爲這可能是一個範圍問題。 @SessionScoped導入包是javax.faces.bean.SessionScoped ...我認爲這是正確的,但將其更改爲javax.enterprise.context.SessionScoped時,問題已解決!Getters現在返回值和「p」對象這是不是巧合,它會繼續按預期工作! – james246 2011-03-09 18:18:04
我面臨着同樣的問題 - 你可以請provie我鏈接到該BalusC博客條目? – 2014-06-18 12:48:20