开发者

How to Inject ManagedBeans in JSF2.0

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 f开发者_Python百科acelet...

@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..


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.


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

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜