【问题标题】:Ajax not supported for viewscope in jsfjsf 中的 viewscope 不支持 Ajax
【发布时间】:2013-09-19 14:15:41
【问题描述】:

我是 jsf 的新手。我在使用 ajax 和 viewscope bean 时遇到了麻烦。

这是我的代码:

index.xhtml:

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"         
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:f="http://java.sun.com/jsf/core">
    <h:head>
        <title>Facelet Title</title>
    </h:head>
    <h:body>
    <h:form>

        <h:inputText id ="text" value="#{bean2.str1}" />
        <h:inputText id ="text2" value="#{bean2.str1}" />
        <h:commandButton value="Test">
            <f:ajax execute="text" render="text2"/>
        </h:commandButton>

        <h:commandButton value="Home" action="home"/>
    </h:form>        
</h:body>
</html>

Bean2.java

package com.hem.beans;

import java.io.Serializable;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;

@ManagedBean
@ViewScoped
public class Bean2 implements Serializable {

public String str1;

public String getStr1() 
{
    return str1;
}
public void setStr1(String str) 
{
    str1 = str;
}
}

页面显示有 2 个输入框和一个命令按钮“测试”。 当我在第一个输入框中输入值并单击测试时,没有任何反应(也没有错误)。

但是当我将 bean 设置为 Sessionscoped 时,它工作正常。

请指导我完成这个。

【问题讨论】:

  • 您是否暗示删除&lt;f:ajax&gt; 时它可以工作?如果不是,请修改您的标题。
  • 使用相同的代码对我来说效果很好。

标签: jsf managed-bean view-scope


【解决方案1】:

它应该与@ViewScoped 一起正常工作。但是,您可以通过@form 更改execute 属性的值以提交所有表单:

<h:form>

    <h:inputText id ="text" value="#{bean2.str1}" /><br />
    <h:outputText id ="text2" value="#{bean2.str1}" /><br />
    <h:commandButton value="Test">
        <f:ajax execute="@form" render="text2"/>
    </h:commandButton>

    <h:commandButton value="Home" action="home"/>
</h:form>  

另外,您最好将结果显示在&lt;h:outputText /&gt; 标记组件中,而不是另一个&lt;h:inputText /&gt;

【讨论】:

  • 换句话说,您确认这根本不能解决 OP 的具体问题?
  • 不确定,因为我试过这个例子,它运行良好。让我们等待反应的问题所有者。
  • 感觉网豆有问题。当我尝试我的原始程序时,它在尝试您的更改后运行良好。但是当我重新启动 NetBeans 时,我的原始程序无法运行。所以我将war文件复制到tomcat 6的webapps文件夹并运行程序(原始)。它与 execute="text" 一起工作得很好。无论如何,非常感谢。
猜你喜欢
  • 2013-03-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-02-19
  • 2011-07-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多