views:

201

answers:

2

Hi folks,

I have got Controller-ManagedBeans and Model-ManagedBeans (like MVC-Pattern).

Here my Controller:

   @ManagedBean
   @RequestScoped
    public class ProjectController {

 private ProjectData projectBean;   //The Model-ManagedBean (see below)!
 private IProjectService projectService;    //My Service-Layer

 public ProjectController(){
  init();
 }

 private void init(){
  projectBean = new ProjectData();
  projectService = new ProjectService();
 }

 public String saveProject() {
  projectService.addProject(projectBean.getProjectDTO());

  return ("");
 }
    }

...and her you can see my model, which gets its values out of a facelet...

@ManagedBean(name="ProjectData")
@SessionScoped
public class ProjectData {

 private ProjectDTO projectDTO;

 public ProjectData(){
  projectDTO = new ProjectDTO();
 }

 public ProjectDTO getProjectDTO() {
  return projectDTO;
 }

 public void setProjectDTO(ProjectDTO projectDTO) {
  this.projectDTO = projectDTO;
 }

 public String getTitle() {
  return projectDTO.getTitle();
 }

 public void setTitle(String title) {
  projectDTO.setTitle(title);
 }
}

My problem is now that it doesn't work. I think I need to inject the dependency between those two classes. But how? Is there a JSF-pro that can help me. I would like to use annotations. Thanks


Update after BalusC's post

I changed my code a little bit, but it still doesn't work. Any further advice, please.

@ManagedBean(name="projectData")
@SessionScoped
public class ProjectData {

...

@ManagedProperty(value="#{projectData}")
private ProjectData projectData;

The error com.sun.faces.mgbean.ManagedBeanCreationException says that the controller bean can not be created because projectData (the model-bean) is not found..

+2  A: 

This should work:

@ManagedProperty(value="#{ProjectData}")
private ProjectData projectBean

That said, the normal practice is to start instance names with a lowercase. You also don't do in real

ProjectData ProjectData = new ProjectData();

but rather

ProjectData projectData = new ProjectData();

If you omit the (name="ProjectData") from the @ManagedBean, then it will automatically default to projectData.


As per your update:

The error com.sun.faces.mgbean.ManagedBeanCreationException says that the controller bean can not be created because projectData (the model-bean) is not found.

It's actually more telling that property projectData cannot be found. The property is not writable. In other words, the setter is missing. In the future please don't rephrase error messages and just copypaste them.

BalusC
What do you mean with that? I started all the instances with a lowercase. Do you want me to start with an uppercase? Sry, it's not clear to me...Sven
Sven
Just start instance names with lower case, or omit the `(name="XXX")`. This is not the cause of your problem, it's just a practice.
BalusC
all right master BalusC, I wonder if I can book you like a personal coach ;-)
Sven
A: 

Okay guys I have found the answer:

there are missing get and set methods in my controller:

/*
 * Getter and Setter
 */
public ProjectData getProjectData() {
    return projectData;
}

public void setProjectData(ProjectData projectData) {
    this.projectData = projectData;
}

Reference

Sven