是JSF的新手。我在我的应用程序中使用JSF 2和primefaces 4.0。如标题中所述,xhtml页面中给出的输入值不会将值设置为ManagedBean。我尝试了所有可能的组合。
growlMessage.xhtml
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:p="http://primefaces.org/ui">
<h:head>
</h:head>
<h:body>
<h:form>
<p:growl id="growl" showDetail="true" sticky="true" />
<p:panel id="panelID" header="Growl">
<h:panelGrid columns="2" cellpadding="5">
<h:outputLabel for="msg" value="Message:" />
<p:inputText id="msg" value="#{growlView.message}" required="true" />
</h:panelGrid>
<p:commandButton value="Save" actionListener="#{growlView.saveMessage}"/>
</p:panel>
</h:form>
</h:body>
</html>
` 的 GrowlView.java:
import java.io.Serializable;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
@ManagedBean
@ViewScoped
public class GrowlView implements Serializable{
private String message;
public GrowlView() {
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public void saveMessage(){
System.out.println("@@@@@ hello");
System.out.println("@@@@@"+ getMessage());
FacesContext context = FacesContext.getCurrentInstance();
context.addMessage(null, new FacesMessage("Successful", "Your message: "+message));
context.addMessage(null, new FacesMessage("Second message", "Additional Message details"));
}
}
答案 0 :(得分:0)
你有充分的理由使用JSF 2.0代替2.2吗?您应该使用CDI而不是JSF托管bean,这些bean或多或少已被弃用。所以,使用
@Named
@ViewScoped
public class GrowlView implements Serializable
确保ViewScoped注释来自javax.faces.view。 xhtml的开头应该使用新的命名空间:
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:p="http://primefaces.org/ui">
答案 1 :(得分:0)
你的commandButton应该是这样的(根据PrimeFaces展示):
<p:commandButton value="Save" actionListener="#{growlView.saveMessage}" update="growl"/>
您是否尝试过更大的范围设置到托管bean,例如@SessionScoped
?
(仅用于测试目的)。因此,您可以排除可能的范围问题。
答案 2 :(得分:0)
尝试以下代码:使用process
和partialSubmit
属性:
<p:commandButton value="Save" actionListener="#{growlView.saveMessage}" update="growl" process="@form" partialSubmit="true"/>
答案 3 :(得分:-1)
HY,
更改
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:p="http://primefaces.org/ui">
<h:head>
</h:head>
<h:body>
<h:form>
<p:growl id="growl" showDetail="true" sticky="true" autoUpdate="true"/>
<p:panel id="panelID" header="Growl">
<h:panelGrid columns="2" cellpadding="5">
<h:outputLabel for="msg" value="Message:" />
<p:inputText id="msg" value="#{pageView.message}" required="true" />
</h:panelGrid>
<p:commandButton value="Save" action="#{pageView.saveMessage}" update="growl"/>
</p:panel>
</h:form>
</h:body>
</html>
并替换
@ManagedBean
与
@ManagedBean(name="pageView")