开发者

JSF 2 Submitting a form

I have this Facelet codes:

<h:inputText id="searchtext" value="#{SearchBean.input}" />
<h:commandButton value="#{msg.BUTTON_SEARCH_LABEL}" title="#{msg.BUTTON_SEARCH_LABEL}"
  action="#{SearchBean.Submit}" />

SearchBean has the following:

@ManagedBean(name = "SearchBean")
@RequestScoped
public class SearchBean implements Serializable
{
    private String input = null;
    private Boolean submitted = false; 

    public String getInput() {
       return input;
    }

    public boolean getSubmitted() {
       return submitted;
    }      

    public void setInput(String input) {
       this.input = input;
    }

    public void setSubmitted(Boolean submitted) {
       this.submitted = submitted;
    }

    public String Submit() {
       System.out.println(">>> SUBMITTED <<<"); 
       submitted = true;
       return "";
   }    
}

The SearchBean's method is called in BookService bean as follow:

@Stateless
public class BookService()
{
    @Inject
    private SearchBean searchBean;

    @PostConstruct
    public void init()
    {
       Boolean isSearch = isSubmitted();
       if (isSearch) {
          // some codes
       } else {
          // other codes
       }
    }

    public Boolean isSubmitted() {
       System.out.println("STATUS: " + searchBean.getSubmitted()); 
       return searchBean.getSubmitted();
    }       
}

When I submit the form, the status return by SearchBean's getSubmitted() method always return false. I would expect this to return true when the form is submitted. I don't know what to do.

Is FlashScoped the only usable option wi开发者_开发百科th form submission in JSF 2? Any expert care to comment.


Use @ViewScoped and return null in your submit() method:

public String Submit() {
       submitted = true;
       return null;
}    

Attributes within a bean in view scope will persist until the user navigates to a new view. Returning null from a bean method will reload the same view and keep the bean's state.

UPDATE: First question back: Is you BookService bean called from the same view? If not, the ViewScope will not work. You have to put the submitted variable in session scope instead.

Make sure that your submit method is called (debug with a breakpoint inside the method) and make sure that your form elements are enclosed by <form> ... </form>.


@RequestScoped means that the context is destroyed at the end of the servlet request.

Try @ViewScoped of @FlashScoped. For the difference: what's new in jsf 2

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜