2015-08-03 73 views
0

我有一个小问题。我想从我的输入区域获取价值,我正在投入,但它没有正确更新。字符串值始终为空。Primefaces获取输入值

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; 
} 
} 

它没有看起来像他找不到我的豆,因为i的值给该按钮在浏览器中正确显示。但是输入不起作用。 我已经和jsf一起工作过,并且从来没有通过bean传递值的问题。我的另一个项目是使用像这里一样的方法,它的工作原理。但在这种情况下,我的输入不会转到bean。请帮助。

+0

使用调试器,调用'setText'吗? – Smutje

+0

不在按键。但他应该自动注意何时按下键并更新getter和setter。它没有。 –

+2

它为什么要在按键上做任何事情,你根本没有AJAX听众? – Smutje

回答

0

您必须提交您的表单才能获取该值。否则表单永远不会发送。

因此,添加一个动作到你的命令按钮,这是按钮被按下时要调用的方法。

<p:commandButton value="#{bean.button}" 
     style="margin-left:10px;margin-top:10px;" action="#{bean.submit}" /> 

写在bean

public void submit(){} 

你的方法就可以包含任何你想要的。

有不错的jsf tutorial here这些是我以前开始的(但我没有完成)。

+0

是的,这看起来是正确的方式,但在我的项目中,我编程的同一方式之前,我的问题不是字符串的提交,而是bean中的getter和setter方法的更新。在我的另一个项目中,我正在向输入框中添加一些东西,我的控制台直接更新了我的小费。但现在它没有。它甚至不采用getter或setter方法。 –

+0

顺便提一句,在我的commandButton中是一个actionListener,他应该得到inputvalue并且用它做一些事情。但是按钮甚至可以获得输入值,因为它始终为空。我也尝试了绑定方法,它总是为我工作,但在这种情况下是同样的事情。输入值始终为空。我认为有一些属性,在我的日食中是错误的。 –

+0

我认为你记错了,或者你没有发布整个代码。动作按钮中没有actionListener。如果你想更新而不刷新页面,请看f:ajax。 – Ced