开发者

Bean 'getters' not returning a value

开发者 https://www.devze.com 2023-02-16 07:20 出处:网络
I\'m doing a little project in Netbeans using Glassfish for a course I\'m doing on Java EE. I\'m having a problem where my \'getter\' methods for the Bean are returning a null value and therefore noth

I'm doing a little project in Netbeans using Glassfish for a course I'm doing on Java EE. I'm having a problem where my 'getter' methods for the Bean are returning a null value and therefore nothing gets submitted to the database. The front-end to the project is a JSF page, which could be part of the problem. My suspicion it it's the bean, the JSF page or my configuration (of what? I don't know!) that is the problem

I'm very new to this topic so please forgive my lack of jargon and naivety on the subject!

Our course instructor has upload a project which is similar to what we need to create so we're all using that as a guide. His works perfectly whereas mine, which to me is identical, doesn't.

I can't post any extensive code, in order to comply with our institution's regulations but I can post a few snippets. If there's something specific that needs to be posted up, I'll do my best.

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 page:

<?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/c开发者_开发技巧ore">
 <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>

Any questions, then please by all means ask! I'm completely stuck with this (and have been for the past 24 hours) so any help is greatly appreciated!

Many thanks


Your resolution is correct. Reason- the bean you are using is a CDI bean and not JSF Managed bean. CDI beans are supported in JSF as a JSR 299 support. The scope to be used in case of CDI bean is javax.enterprise.context.SessionScoped. So what you have done is perfectly fine.

0

精彩评论

暂无评论...
验证码 换一张
取 消