Richfaces 4 rich:inputNumberSpinner:未获得“设置"在支持豆 [英] Richfaces 4 rich:inputNumberSpinner: not getting "set" in backing bean

查看:116
本文介绍了Richfaces 4 rich:inputNumberSpinner:未获得“设置"在支持豆的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我以前做过JSF工作,但对于RichFaces来说是新手.为了测试我的设置和环境等,我试图制作一个简单的小应用程序,用户输入两个数字,单击一个按钮,然后该应用程序返回数字的总和和乘积.

I have done JSF work before, but am new to RichFaces. To test my set up and environment, etc. I have tried to make just a simple little app where the user enters two numbers, clicks a button, and the app returns the sum and product of the numbers.

下面是代码摘要:

web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
    http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">

 <display-name>eSPAR</display-name>

 <welcome-file-list>
   <welcome-file>index.html</welcome-file>
 </welcome-file-list>

 <servlet>
    <servlet-name>Faces Servlet</servlet-name>
    <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>Faces Servlet</servlet-name>
    <url-pattern>*.jsf</url-pattern>
</servlet-mapping>

<context-param>
    <param-name>javax.faces.DEFAULT_SUFFIX</param-name>
    <param-value>.xhtml</param-value>
</context-param>

<!-- Plugging the skin into the project -->
<context-param>
    <param-name>org.richfaces.SKIN</param-name>
    <param-value>deepMarine</param-value>
</context-param>
<!-- Making the RichFaces skin spread to standard HTML controls -->
<context-param>
    <param-name>org.richfaces.CONTROL_SKINNING</param-name>
    <param-value>enable</param-value>
</context-param>

</web-app>

下一步:

faces-config.xml
<?xml version="1.0" encoding="UTF-8"?>

<faces-config xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
        http://java.sun.com/xml/ns/javaee/web-facesconfig_2_0.xsd"
    version="2.0">

<application></application>

</faces-config>

换句话说,除了版本声明外,基本上为空.接下来是视图页面:

In other words, basically empty except for the version declarations. Next comes the view page:

 addmult.xhtml
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<f:view xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"  
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:a4j="http://richfaces.org/a4j"
xmlns:rich="http://richfaces.org/rich">

<h:head>
</h:head>

<h:body>

<rich:panel header="Sample Add and Multiply App">
<h:form id="addmultForm">

    <p>Enter one number here: 
        <rich:inputNumberSpinner id="num1" minValue="0"
                            value="# {calcBean.number1}" /></p>
    <p>Enter another number here: 
        <rich:inputNumberSpinner id="num2" minValue="0"
                           value="#{calcBean.number2}" /></p>

    <a4j:commandButton value="Go" action="#{calcBean.go}" immediate="true">
        <f:ajax event="click" render="results" />
    </a4j:commandButton>

    <br /><br />

    <h:panelGroup id="results">
    <h:panelGroup rendered="#{calcBean.calcDone}">
        <p>The sum of your two numbers is: #{calcBean.sum}</p>
        <p>The product of your two numbers is: #{calcBean.product}</p>
    </h:panelGroup>
    </h:panelGroup>


</h:form>
</rich:panel>   

</h:body>   
</f:view>

现在终于是豆子了:

 CalcBean.java
import javax.faces.bean.ManagedBean;

@ManagedBean
public class CalcBean {

private Integer number1 = 3;
private Integer number2 = 7;
private int sum;
private int product;
private boolean calcDone = false;

public Integer getNumber1() {
    System.out.println("I am number1: " + number1);
    return number1;
}
public void setNumber1(Integer number1) {
    System.out.println("Change in number1");
    this.number1 = number1;
}
public Integer getNumber2() {
    return number2;
}
public void setNumber2(Integer number2) {
    this.number2 = number2;
}

public int getSum() {
    return sum;
}
public int getProduct() {
    return product;
}
public boolean isCalcDone() {
    System.out.println("Returning calcDone: " + calcDone);
    return calcDone;
}


public String go() {
    System.out.println("Going!");
    sum = number1 + number2;
    product = number1 * number2;
    calcDone = true;
    return null;
}

}

我的WEB_INF库包含:commons-beanutils-1.8.3,commons-collections-3.2.1,commons-digester-2.1,commons-logging-1.1.1,cssparser-0.9.5,guava-r08,jhighlight- 1.0,jsf-api(mojarra 2.0版本),jsf-facelets-1.1.15,jsf-impl(再次是mojarra 2.0),richfaces-components-api-4.0.0-FINAL,richfaces-components-ui-4.0.0-最终版,richfaces-core-api-4.0.0-FINAL,richfaces-core-impl-4.0.0-FINAL,sac-1.3,标准.

My WEB_INF lib contains: commons-beanutils-1.8.3, commons-collections-3.2.1, commons-digester-2.1, commons-logging-1.1.1, cssparser-0.9.5, guava-r08, jhighlight-1.0, jsf-api (mojarra 2.0 version), jsf-facelets-1.1.15, jsf-impl (again mojarra 2.0), richfaces-components-api-4.0.0-FINAL, richfaces-components-ui-4.0.0-FINAL, richfaces-core-api-4.0.0-FINAL, richfaces-core-impl-4.0.0-FINAL, sac-1.3, standard.

无论我启动整数1和2的什么值,这些都是页面加载时最初在输入中的值.下部文本最初是隐藏的.单击开始"按钮时,将出现下面的面板,但是无论用户输入了什么值,总和始终为10,乘积始终为21(如图所示).

Whatever values I initiate the Integers number1 and number2, those are the values initially in the inputs when the page loads. The lower text is initially hidden. When the "Go" button is clicked, the lower panel appears, but no matter what values the user has entered, the sum is always 10 and the product always 21 (as shown).

设置器中的sysout永远不会显示.页面加载时,getter中的一个显示一次.单击执行"后,系统将显示执行!".显示一次,然后"Returning calcDone:true"显示六次.

The sysout in the setter never displays. The one in the getter displays once when the page loads. When "Go" is clicked, the sysout "Going!" shows once, then "Returning calcDone: true" shows six times.

我尝试过的方法:更改bean的范围.在结果面板中的<h:outputText>中包装表达式.在web.xml中添加FaceletViewHandler(实际上会引起更多问题).从lib中删除facelet jar文件.

What I have tried: Changing the scope of the bean. Wrapping the expressions in the results panel in <h:outputText>. Adding the FaceletViewHandler in web.xml (actually causes more problems). Removing the facelet jar file from the lib.

我需要在输入上使用值更改侦听器吗?我还应该尝试什么?

Do I need a value change listener on the inputs? What else should I try?

推荐答案

<a4j:commandButton value="Go" action="#{calcBean.go}" immediate="true">
    <f:ajax event="click" render="results" />
</a4j:commandButton>

删除immediate="true".这将导致在处理过程中完全跳过具有immediate="true"的输入.还要删除<f:ajax>,因为<a4j:commandButton>已经使用了ajax.为了更好地理解immediate的功能以及所需的功能,建议您阅读以下文章:

Remove immediate="true". It will cause the inputs which do not have immediate="true" to be completely skipped in processing. Also remove <f:ajax> as <a4j:commandButton> already uses ajax. To get a better understanding what immediate does and what you need it for, I suggest you to get yourself through this article : Debug JSF lifecycle (yes, it's JSF 1.2 targeted, but general concept applies on JSF 2.x as well)

以下是相关性摘要:

好,我什么时候应该使用即时属性?

如果还不清楚,这里是一个总结,并附有一些可能有用的现实使用示例:

Okay, when should I use the immediate attribute?

If it isn't entirely clear yet, here's a summary, complete with real world use examples when they may be beneficial:

  • 如果仅在UIInput中进行设置,则将在应用请求值"阶段中进行过程验证阶段.使用它可以优先考虑所讨论的UIInput组件的验证.当其中任何一个的验证/转换失败时,将不对非立即组件进行验证/转换.

  • If set in UIInput(s) only, the process validations phase will be taken place in apply request values phase instead. Use this to prioritize validation for the UIInput component(s) in question. When validation/conversion fails for any of them, the non-immediate components won't be validated/converted.

如果仅在UICommand中设置,则对于任何UIInput组件,都会跳过应用请求值阶段,直到具有更新模型值阶段为止.使用它可以跳过表单的整个处理过程.例如. 取消"或返回"按钮.

If set in UICommand only, the apply request values phase until with update model values phases will be skipped for any of the UIInput component(s). Use this to skip the entire processing of the form. E.g. "Cancel" or "Back" button.

如果在UIInputUICommand组件中均设置,则对于任何不具有此属性的UIInput组件,将跳过应用请求值阶段,直到具有更新模型值阶段为止放.使用此选项可跳过对某些字段(立即数)的整个表单期望的处理.例如.登录表单中的忘记了密码"按钮,其中包含必填但非立即的密码字段.

If set in both UIInput and UICommand components, the apply request values phase until with update model values phases will be skipped for any of the UIInput component(s) which does not have this attribute set. Use this to skip the processing of the entire form expect for certain fields (with immediate). E.g. "Password forgotten" button in a login form with a required but non-immediate password field.

因此,您最终想要这个:

So, you ultimately want this:

<a4j:commandButton value="Go" action="#{calcBean.go}" render="results" />


无关:与具体问题无关:您的配置中存在更严重的问题.我的印象是,您正在阅读针对JSF 1.x和RichFaces 3.x的教程/文档,而不是针对JSX 2.x和RichFaces 4.x的教程/文档.第一个<context-param>仅对JSF 1.x Facelets是必需的,而剩余部分全部特定于RichFaces3.x.全部删除.您还应该删除有问题的JSF 1.x jsf-facelets.jar文件,并且JSF 2.x已经捆绑了JSF 2.x Facelets实现.


Unrelated to the concrete problem: there are more pretty serious problems in your configuration. I have the impression that you're reading JSF 1.x and RichFaces 3.x targeted tutorials/documentation instead of JSX 2.x and RichFaces 4.x targeted ones. The first <context-param> is mandatory for JSF 1.x Facelets only and the remnant is all specific to RichFaces 3.x. Remove them all. You also have an offending JSF 1.x jsf-facelets.jar file which should be removed as well as JSF 2.x already bundles a JSF 2.x Facelets implementation.

这篇关于Richfaces 4 rich:inputNumberSpinner:未获得“设置"在支持豆的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆