JSF页面中给出的输入不会将该值设置为托管bean变量[duplicate]

问题描述 投票:0回答:4

是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>

` grow了view.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"));
     }
  }
primefaces jsf-2
4个回答
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">

0
投票

你的commandButton应该是这样的(根据PrimeFaces展示):

<p:commandButton value="Save" actionListener="#{growlView.saveMessage}" update="growl"/>

您是否尝试将更大的范围设置为托管bean,例如@SessionScoped? (仅用于测试目的)。因此,您可以排除可能的范围问题。


0
投票

尝试以下代码:使用processpartialSubmit属性:

<p:commandButton value="Save" actionListener="#{growlView.saveMessage}" update="growl" process="@form" partialSubmit="true"/>

-1
投票

他,

为。。改变

 <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")
© www.soinside.com 2019 - 2024. All rights reserved.