【发布时间】:2015-08-03 13:02:51
【问题描述】:
我有一个小问题。我想从我正在输入的输入区域中获取值,但它没有正确更新。字符串值始终为空。
xhtml:
<!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"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:p="http://primefaces.org/ui">
<h:head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta http-equiv="Pragma" content="No-cache" />
<meta http-equiv="Cache-Control"
content="no-store,No-cache,must-revalidate,post-check=0,pre-check=0,max-age=0" />
<meta http-equiv="Expires" content="-1" />
<link type="text/css" rel="stylesheet"
href="#{request.contextPath}/resources/css/styles.css" />
<link rel="shortcut icon" href="#{resource['Icon.png']}"
type="image/x-icon" />
<title>#{msg['title.index']}</title>
</h:head>
<h:body style="background:#f5f5f5;">
<h:form>
<p:commandButton value="#{bean.button}"
style="margin-left:10px;margin-top:10px;" />
<p:inputTextarea value="#{bean.text}" autoResize="true"
placeholder="#{msg['label.placeholder']}" rows="3" cols="90"
style="margin-top:250px;margin-left:5px;">
</p:inputTextarea>
</h:form>
</h:body>
</html>
豆子:
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
@ManagedBean(name = "bean")
@SessionScoped
public class Controller {
private String text;
private String button = "Click";
public String getText() {
System.out.println(text);
return text;
}
public void setText(String text) {
this.text = text;
}
public String getButton() {
return button;
}
public void setButton(String button) {
this.button = button;
}
}
他似乎找不到我的 bean,因为我给按钮的值在浏览器中正确显示。但是输入不起作用。 我已经使用过 jsf,并且在通过 bean 传递值时从来没有遇到过问题。我的另一个项目正在使用与此处相同的方法并且它有效。但在这种情况下,我的输入不会进入 bean。请帮忙。
【问题讨论】:
-
使用调试器,是否调用了
setText? -
不在按键上。但是他应该在按下键时自动注意到并更新 getter 和 setter。它没有。
-
为什么它要在按键上做任何事情,你根本没有 AJAX 监听器?
-
并将您的 commandButton 与您的其他项目进行比较。有什么主要区别吗?
-
不,没有区别。我认为当我在输入框中输入一些文本时,jsf 会自动在 bean 中设置 setter 和 getter 方法是正常的……在我的另一个项目中,输入框内的文本直接发送到 bean,而我一个小费,控制台直接显示我的文字。但是在这种情况下,getter 甚至没有得到输入值!也许有一些属性?
标签: jsf jsf-2 primefaces