0

I am building a JSF 2.0 Application using primfaces. I have a managed bean that has several objects for different scenarios. In this particular portion of the application I move from a data table/form to a managed bean with an identification number. I pull the information from the database and even have it in the log. However when I make it to the JSF xhtml page the object values are null and I can't seem to figure out why, any help would be great as I am getting no errors, exceptions, or warnings in the logs and all information seems to point to the loading of the data including the log.

JSF Page that I am coming from... jobs.xhtml (I am using jsf 2.2 and primefaces to build this application) From this page the link specifies a job number to be retrieved and the next bean will retrieve that job based on the job number I have it down to just a button that will take you to the primary method and get the information to the next page...

<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.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"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:p="http://primefaces.org/ui">
<body>
    <div style="margin-bottom:350px;">
        <p:column>
            <h:form>
                <h:commandButton id="editProject" action="#{editProjects.getProjectForm()}" value="Edit" />
            </h:form>
        </p:column>
    </div>
</body>
</html>

EditProjects.java the backing bean for the editMailerJob.xhtml which will take in the medium id and the job number the medium id will be used to direct the application to the next page and the job number will be used to retrieve a specific job

::::UPDATED::::

package beans;

import java.io.Serializable;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.RossProjectManagement.rpm.objects.MailerProject;

@ManagedBean
@RequestScoped
public class EditProjects implements Serializable {
/**
 * 
 */
private static final long serialVersionUID = -6680733133634363295L;

private final Log LOG = LogFactory.getLog(this.getClass().getName());

// Editable projects
private MailerProject edit_mailer = new MailerProject();
//Changed this to be a static set value
private String mailerProject = "THIS IS THE MAILER STRING";

public int getProjectForm() {

    loadEditMailer();

    return 1;
}

public void loadEditMailer() {
    this.edit_mailer = new MailerProject("REDJ15061005",
            convertDate("2015-06-29"), false, "RickD",
            "RickD and the Gang", "new", true, true, "SPEC", 1, 1, 1,
            "REDJ15061005", "11X17", "SPOT_COLOR", "20,000",
            convertDate("2015-06-30"), convertDate("2015-07-13"),
            convertDate("2015-07-06"), convertDate("2015-07-06"),
            convertDate("2015-07-15"), true, "BDC",
            "someone@example.com", "STANDARD", 7500.00, "CONQUEST",
            "NONE", "JS DIRECT", "NONE");
    //Commented this out to keep the value static
    //setMailerProject(edit_mailer.toString());
    LOG.debug(":::MAILER PROJECT TO STRING::: " + this.mailerProject);
}

public Date convertDate(String date) {
    DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
    Date date1 = null;
    try {
        date1 = df.parse(date);
    } catch (ParseException e) {
        e.printStackTrace();
    }

    return date1;
}

}

JSF page that I am redirecting to editMailerJob.xhtml

<?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"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:p="http://primefaces.org/ui">
<body>
    <div style="margin-bottom: 350px;">
                <h:outputLabel>#{editProjects.edit_mailer}</h:outputLabel>
                <h:outputLabel>Mailer String::: #{editProjects.mailerProject}        </h:outputLabel>

    </div>
</body>
</html>

The log shows that all of the information has been received and loaded but shows no where that the information has been cleaned or the object made anew. I have tried instantiating the object in different places at different times but that didn't work.

Before I leave the bean the object has value, when I get to the page the object has no value, I don't understand why

I then tried loading the information from different places. I have tried changing the scope of the bean from request to view back to request but no luck, (I thought about doing session but I didn't see the benefit as well as I just couldn't see reasoning to put all of that information into the session).

The problem seems to be revolving directly around the bean itself and the getProjectForm Method because at one point the object is loaded and then it just loses all of its information.

All of my values seem to be correct as far as I can tell but none of the information is loading.

I can't seem to wrap my head around the issue so any help would be greatly appreciated. if any more information is needed just let me know.

I hope all of this helps Let me know if I have forgotten anything.

EDITS:::

I have created just a string to see if at any point it would be delivered either, and so far I am still getting nothing, the .xhtml page is reading that it exists but not that it has value. Still working with this if anyone else has a solution it would be awesome, or if you know the solution and could at the least point me in the right direction that would be great also.

UPDATE #2 So I found that if I set the value outside a method it will properly be displayed so it seems to me like the methods are keeping the value from the xhtml page. What is the deal with that, I have getters and setters for the variables I have them declared and defined properly, but for what ever reason the values when loaded by the method are not kept outside the method, the values are not able to be accessed by the xhtml page.

10:00 pm update Found out that if I instantiate the MailerProject object the same way I don't get the same results I still produce an object full of null attribute values.

Any ideas guys?

  • Please create an mcve. This is to much information for me to wade through. – Kukeltje Jul 30 '15 at 16:23
  • @Kukeltje okay before doing so, I would like to know if you would like to keep the .xhtml info or not if so I can leave it, if you find it unnecessary I will remove it, I wanted to provide it in case anyone wanted to thoroughly redraft the problem based on the pages. I can provide the list of objects as well as the object for the backing bean to the form page and the faces-config.xml, but in creating an mcve or at least attempting to I don't want to leave out valuable information, the beans provided here are at the most minimal that they can be. –  Jul 30 '15 at 16:34
  • @Kukeltje also I can provide the objects that are necessary to run the example also, so that if you needed to you could effectively fire up a mock up of the application, but again that borderlines on the problem of minimization and rather or not it is required –  Jul 30 '15 at 16:36
  • Check [ask] and the info tab when clicking the jsf tag. Lots of info on how to create an mcve – Kukeltje Jul 30 '15 at 17:00
  • @Kukeltje I have edited the question based on what I understood mcve to be from this link http://stackoverflow.com/help/mcve I may be missing a few things or overlooking some of the requirements but see if this is any better for you. I have gotten rid of most of the code that wasn't required, provided the objects and code to reassemble a very base level of the project and instantiated those objects for use. Please let me know if there is anything else I should be doing that I am not, Thank you. –  Jul 30 '15 at 17:11
  • Ok your beans can still be smaller way to many fields ;-) but now it is more clear. You seem to use the wrong combination of the managedbean and viewscoped. With managedbean use the javax.faces.bean instead of the javax.faces.view one. The wrong combination makes it behave as a requestscoped beans – Kukeltje Jul 30 '15 at 19:08
  • What am I misunderstanding ? Your method returns 1. – Ced Jul 30 '15 at 23:01
  • @Ced the method only returns one here for purposes of demonstration and recreation of the issue, otherwise the return is a value passed in for directing the page from one form to another depending on the value, but it is an integer based value so using 1 here made sense. –  Jul 30 '15 at 23:08
  • @Kukeltje okay cool let me update that and see what I get if it is a correct analysis I will let you know so that you can put it into an answer so that the recognition goes to you. –  Jul 30 '15 at 23:10
  • @Kukeltje Tried it your way and no go, so then I tried it by putting the object in a session scoped class still nothing, I am really not sure why this isn't working, this is literally the first class that I have that this load has not worked. –  Jul 30 '15 at 23:20
  • @Kukeltje I have updated the question to the most recent changes and the most recent results, if you have any more information that would be great... –  Jul 31 '15 at 01:31
  • There's just too much going on here; very few people will be interested in reading this thing as it is – kolossus Jul 31 '15 at 02:07
  • @kolossus what do you suggest I do, I have only provided information as I have made discoveries, while working this problem out, the information as far as code is concerned that is provided is only provided in the event that someone wants to recreate the issue. But I am willing to make changes to appeal to the people who can help me, just let me know what it is that I need to do. –  Jul 31 '15 at 02:12
  • I've seen the problem, and all I needed to look at were: `EditProjects` and your two xhtml files. The rest of the stuff: your entities, your faces-config.xml and the verbose explanation didn't factor in - I literally just skipped over most of it. While too little information is pointless, too much is just as bad, and discouraging (at least with too little info, some users might be inclined to ask you to post more information, at the risk of some downvotes). Even the getters and setters in your managed bean were ultimately noise – kolossus Jul 31 '15 at 02:20
  • @kolossus You said you have seen the problem, any advice or direction you can give me here? –  Jul 31 '15 at 02:23
  • I was looking to see if there was a duplicate question that would give a neat answer, as it's quite basic. Hang on a bit, If I don't find one, I'll post an answer – kolossus Jul 31 '15 at 02:25
  • @kolossus ok cool , thank you, in the mean time I have updated the question, when you get a chance please let me know if this is proper so that I know not to make the same mistakes later. –  Jul 31 '15 at 02:27

1 Answers1

0

Start Here - It'll make the rest of this very trivial.

The Problem

You're using a @RequestScoped bean, and what you're experiencing is the expected behaviour for beans of that scope. Between the first and the second page, you should understand by now, that you're dealing with two different instances of the editProjects bean : a field you set on the first page, backed by one instance of editProjects will not be available on the destination page, backed by a new instance of editBean.

To Solve

While it wasn't mentioned in the answer I linked to, what you need here is the FlashScope, that serves the express purpose of being transitory in nature: use it just to transport "stuff" between two pages/beans/scopes etc. Sorta. To use:

  1. Declare your undying love for the flash scope around the point of origination of the data. In so doing, you're storing your data in the flash scope object, as implicitly provided by JSF, after which you can then navigate away:

    Flash fScope = FacesContext.getCurrentInstance().getExternalContext().getFlash();
    fScope.put("editMailer",edit_mailer);
    
  2. You can then refer to the variable you stored in the flash scope, on the destination page:

    <h:outputLabel>#{flash.edit_mailer}</h:outputLabel>
    

    Like I mentioned earlier, the flash scope is a transitory scope: once the destination page has been rendered and the content of the scope displayed, you can consider the data as good as gone. If you would like to hang on to it for just one page redirect longer, you can specify the keep directive:

    <h:outputLabel>#{flash.keep.edit_mailer}</h:outputLabel>
    

Unrelated to the problem

  1. Avoid doing any heavy lifting in a getter/accessor method (like you're doing in getProjectForm). It's bad for business

  2. What's the significance of returning "1" from that navigation method? Why not "destination_page", or "edit_page.xhtml", y'know, something easier to read? That's just a style thing anyway, no evil can come of that.

Community
  • 1
  • 1
kolossus
  • 20,559
  • 3
  • 52
  • 104
  • Absolutely awesome, I can't believe I hadn't heard of this, I love jsf for a lot of reasons, so I am learning as I go this here is pretty awesome and makes a lot of sense. I really appreciate you sticking it out even though I had a poorly written question and showing me where I needed to go. Thank you –  Jul 31 '15 at 03:28