我正在 Netbeans 中使用 Glassfish 为我正在做的 Java EE 类(class)做一个小项目。我遇到一个问题,Bean 的“getter”方法返回空值,因此没有任何内容提交到数据库。该项目的前端是一个 JSF 页面,这可能是问题的一部分。我怀疑是 bean、JSF 页面或我的配置(什么?我不知道!)才是问题所在
我对这个主题还很陌生,所以请原谅我在这个主题上缺乏行话和天真!
我们的类(class)讲师上传了一个与我们需要创建的项目类似的项目,因此我们都使用它作为指南。他的工作完美,而我的,对我来说是相同的,却不行。
为了遵守我们机构的规定,我无法发布任何广泛的代码,但我可以发布一些片段。如果有具体的事情需要发布,我会尽力而为。
bean :
@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 小时内一直如此),因此非常感谢任何帮助!
非常感谢
最佳答案
您的分辨率是正确的。原因 - 您使用的 bean 是 CDI bean,而不是 JSF Managed bean。 CDI bean 在 JSF 中受支持,形式为 JSR 299支持。 CDI bean 的使用范围是 javax.enterprise.context.SessionScoped。所以你所做的一切都很好。
关于java - Bean 'getters' 未返回值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5249941/