如何在jsf中填充注入的bean

时间:2017-03-06 21:28:24

标签: jsf dependency-injection model cdi

我有一个简单的表格:

<h:form>
  <h:inputText value="#{storyController.story.author}"/>
  <h:commandButton action="#{storyController.save}" value="save"/>
</h:form>

和这样的bean:

@Model
public class Story {

    private String author;

    //getter and setter...
}

和控制器:

@Model
public class StoryController {

    private StoryService storyService;

    private Story story;

    @Inject
    public StoryController(StoryService storyService, Story story) {
        this.storyService = storyService;
        this.story = story;
    }

    public String save() {
        System.out.println(story.getAuthor());

        return "index.xhtml";
    }

    public Story getStory() {
        return story;
    }

    public void setStory(Story story) {
         this.story = story;
    }
}

现在当我填写作者字段并单击“保存”按钮时,不会填充bean(正确注入bean,它不是null)。 如果我在构造函数中执行story = new Story(),则正确填充bean。我想避免这样做,我认为injecte bean是一种更好的做法吗? 我试图将输入值设置为:#{story.author}但仍未填充。

我做错了什么?

0 个答案:

没有答案