【发布时间】:2011-07-12 02:51:47
【问题描述】:
我正在使用 Glassfish 在 Netbeans 中做一个小项目,用于我正在做的 Java EE 课程。我遇到了一个问题,我的 Bean 的“getter”方法返回一个空值,因此没有任何内容提交到数据库。项目的前端是一个 JSF 页面,这可能是问题的一部分。我怀疑问题出在 bean、JSF 页面或我的配置(什么?我不知道!)
我对这个话题很陌生,所以请原谅我在这个话题上缺乏行话和幼稚!
我们的课程讲师上传了一个与我们需要创建的项目类似的项目,因此我们都将其用作指导。他的作品完美,而我的作品,对我来说是相同的,却没有。
为了遵守我们机构的规定,我不能发布任何广泛的代码,但我可以发布一些 sn-ps。如果有什么具体的需要贴出来,我会尽力的。
豆子:
@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 变量。所以也许这就是问题所在,但为什么会这样,我不知道。如果有人想知道, encrypt 方法会尝试调用 getPost 方法来获取“post”(来自 JSF 表单的文本字符串),然后获取返回值 + 密码(当前硬编码为值为“password”的字符串" 然后使用 DES/MD5 加密并以 byte[] 格式返回加密后的帖子
-
认为我自己解决了这个问题。一直在阅读 JSF 大师 BalusC 的博客,并认为这可能是一个范围界定问题。然后我注意到我的@SessionScoped 导入包是 javax.faces.bean.SessionScoped ...我认为这是正确的,但是在将其更改为 javax.enterprise.context.SessionScoped 后,问题就解决了! Getter 现在正在返回值,并且“p”对象被持久化了!触摸木头这不是巧合,它会继续按预期工作!
-
我也遇到了同样的问题 - 能否请您提供一个指向该 BalusC 博客条目的链接?
-
那是 3 年前的事了,恐怕我记不起来了。一个快速的谷歌带来了这个:balusc.blogspot.co.uk/2011/09/communication-in-jsf-20.html——这可能是我当时读到的。我清楚地记得我刚刚导入了错误的包,所以请检查你是否有正确的包。
标签: java jsf netbeans jakarta-ee javabeans