Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jsf-2/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jsf 二传手不';当我使用视图参数时,不会被调用_Jsf_Jsf 2 - Fatal编程技术网

Jsf 二传手不';当我使用视图参数时,不会被调用

Jsf 二传手不';当我使用视图参数时,不会被调用,jsf,jsf-2,Jsf,Jsf 2,我正在使用JSF2.1.1。我有一个示例JSF页面,用于发布国家评论。我使用f:viewparam标记选择国家/地区页面。代码如下: country.xhtml: <f:metadata> <f:viewParam name="country" value="#{countryBean2.selectedCountry}" converter="countryConverter" /> </f:metadata> <h:

我正在使用JSF2.1.1。我有一个示例JSF页面,用于发布国家评论。我使用
f:viewparam
标记选择国家/地区页面。代码如下:

country.xhtml

<f:metadata>
        <f:viewParam name="country" value="#{countryBean2.selectedCountry}" converter="countryConverter" />
    </f:metadata>

    <h:head>
        <title>Country</title>
    </h:head>

    <h:body>
        <h:form id="form">
            <h:outputText value="#{countryBean2.selectedCountry.countryName}" />
            <br/><br/>
            <h:outputText value="Comment:" />
            <h:inputText value="#{countryBean2.comment}" />
            <br/>
            <h:commandButton value="Send">
                <f:ajax  listener="#{countryBean2.sendComment}" render="form" />
            </h:commandButton>
        </h:form>
    </h:body>
CountryConverter.java

@Named("countryBean2")
@SessionScoped
public class CountryBean2 implements Serializable {
    private EntityCountry selectedCountry;
    private String comment;

    public EntityCountry getSelectedCountry() { return selectedCountry; }
    public void setSelectedCountry(EntityCountry newValue) { selectedCountry = newValue; }

    public String getComment() { return comment; }
    public void setComment(String newValue) { comment = newValue; }

    EntityManagerFactory emf = Persistence.createEntityManagerFactory("testPU");

    public void sendComment() {
        EntityManager em = emf.createEntityManager();
        try {
            FacesMessage msg = null;
            EntityTransaction entr = em.getTransaction();
            boolean committed = false;
            entr.begin();
            try {
                EntityCountryComment c = new EntityCountryComment();
                c.setCountry(selectedCountry);
                c.setComment(comment);
                em.persist(c);
                committed = true;
                msg = new FacesMessage();
                msg.setSeverity(FacesMessage.SEVERITY_INFO);
                msg.setSummary("Comment was sended");
            } finally {
                if (!committed) entr.rollback();
            }
        } finally {
            em.close();
        }
    }
}
public class CountryConverter implements Converter {
    public static EntityCountry country = new EntityCountry();

    EntityManagerFactory emf = Persistence.createEntityManagerFactory("testPU");


    @Override
    public EntityCountry getAsObject(FacesContext context, UIComponent component, String value) {
        EntityManager em = emf.createEntityManager();
        Query query = em.createQuery("SELECT c FROM EntityCountry c WHERE c.countryName = :countryName")
                .setParameter("countryName", value);
        country = (EntityCountry) query.getSingleResult();
        return country;
    }


    @Override
    public String getAsString(FacesContext context, UIComponent component, Object value) {
        EntityCountry c = (EntityCountry) value;
        return c.getCountryName();
    }
}

我可以成功打开国家/地区页面(例如
http://localhost:8080/test/faces/country.xhtml?country=england
),但当我尝试使用
commandButton
发布注释时,不会调用
setComment
setter,并且
comment
变量保持
null
。我试图在
inputText
commandButton
上设置
immediate=“true”
,但都不起作用。

execute
属性默认为当前组件
@this
。如果要提交整个表单,则需要使用
@form
。在
渲染中也使用此选项

<h:commandButton value="Send">
    <f:ajax execute="@form" listener="#{countryBean2.sendComment}" render="@form" />
</h:commandButton>

很抱歉,我尝试在没有ajax和immediate=“true”属性的情况下提交表单。我用的是漂亮的脸。当我使用没有ajax和immediate=“true”属性的commandbutton时,漂亮的URL会变得难看。我想我可以用这些信息来超越。非常感谢你。