JSF Bean 无法设置
这很令人困惑。我正在为一个类开发一个简单的应用程序,并按照示例进行操作,但是该死的 Bean 属性无法设置。代码如下:
Bean 代码:
import java.io.Serializable;
import javax.faces.bean.*;
import javax.faces.event.*;
@ManagedBean(name="dist") // or @Named("dist")
@SessionScoped
public class DistanceConvertBean implements Serializable{
private static final double MILESTOKM = 1.609344;
private static final double KMTOMILES = 0.62137;
private double input;
private String convertTo;
private String outputString;
Eclipse generated getters and setters
Index.xhtml 代码:
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html">
<h:body>
<h:panelGrid columns="3">
<h:outputText value="Distance:"/>
<h:inputText id="inDist" value="#{dist.input}" required="true">
<f:convertNumber maxFractionDigits="2"/>
<f:validateDoubleRange maximum="10000"/>
</h:inputText>
<h:message for="inDist"/>
<h:outputText value="Convert To:"/>
<h:form>
<h:selectOneRadio value="#{dist.convertTo}" required="true">
<f:selectItem itemValue="km" itemLabel="Kilometer"/>
<f:selectItem itemValue="miles" itemLabel="Miles"/>
</h:selectOneRadio>
</h:form>
<h:form>
<h:commandButton value="Convert" action="blah"/>
</h:form>
</h:panelGrid>
</h:body>
</html>
Blah.xhtml 代码:
<h:body>
<h:outputText value="#{dist.input}"/>
</h:body>
因此,我在 Index.xhtml 的 InputText 字段中输入 10,然后按命令按钮转到 blah.xhtml。 blah 中的值仍然是 0.0。
我正在使用 Tomcat 7 和 Eclipse Heilos 并创建了一个动态 Web 项目。我确实在 WEB-INF/libs 中拥有所有 JSF jar。我不明白为什么没有设置 bean 属性。这看起来与您在 COREJSF 示例中找到的新手代码一模一样。 Web.xml 使用 2.5 作为 servlet,所以我不需要 Face-config.xml。
This is very confusing. I'm working on a simple app for a class and followed the examples but the damn Bean property just won't set. Code follows:
Bean code:
import java.io.Serializable;
import javax.faces.bean.*;
import javax.faces.event.*;
@ManagedBean(name="dist") // or @Named("dist")
@SessionScoped
public class DistanceConvertBean implements Serializable{
private static final double MILESTOKM = 1.609344;
private static final double KMTOMILES = 0.62137;
private double input;
private String convertTo;
private String outputString;
Eclipse generated getters and setters
Index.xhtml code:
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html">
<h:body>
<h:panelGrid columns="3">
<h:outputText value="Distance:"/>
<h:inputText id="inDist" value="#{dist.input}" required="true">
<f:convertNumber maxFractionDigits="2"/>
<f:validateDoubleRange maximum="10000"/>
</h:inputText>
<h:message for="inDist"/>
<h:outputText value="Convert To:"/>
<h:form>
<h:selectOneRadio value="#{dist.convertTo}" required="true">
<f:selectItem itemValue="km" itemLabel="Kilometer"/>
<f:selectItem itemValue="miles" itemLabel="Miles"/>
</h:selectOneRadio>
</h:form>
<h:form>
<h:commandButton value="Convert" action="blah"/>
</h:form>
</h:panelGrid>
</h:body>
</html>
Blah.xhtml code:
<h:body>
<h:outputText value="#{dist.input}"/>
</h:body>
So I put in 10 in the InputText field in Index.xhtml and press the command button to go to blah.xhtml. The value in blah is still 0.0.
I'm using Tomcat 7 and Eclipse Heilos and created a dynamic web project. I do have all the JSF jars in WEB-INF/libs. I don't understand why the bean properties are not being set. This look exactly like those newbie codes you find in COREJSF examples. Web.xml is using 2.5 for servlet so I shouldn't need a face-config.xml.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的
不在
元素内。将
放在 相同
内作为命令按钮:这同样适用于您的
。将其放在
内commandButton
所在的位置。Your
<h:inputText>
is not inside a<h:form>
element.Put the
<h:inputText>
inside the same<h:form>
as the command button:The same applies to your
<h:selectOneRadio>
. Put it inside the<h:form>
where thecommandButton
is.