Primefaces 获取输入值
Primefaces Get Input Value
我有个小问题。我想从我输入的输入区域中获取值,但它没有正确更新。字符串值始终为空。
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;
}
}
他似乎没有找不到我的豆子,因为我给按钮的值在浏览器中正确显示。但是输入不起作用。
我已经和 jsf 一起工作过,我从来没有遇到过通过 bean 传递值的问题。我的另一个项目正在使用与这里相同的方法并且它有效。但在这种情况下,我的输入不会进入 bean。请帮忙
您必须提交表单,bean 才能获取值。否则永远不会发送表格。
因此,向您的命令按钮添加一个操作,即按下按钮时将调用的方法。
<p:commandButton value="#{bean.button}"
style="margin-left:10px;margin-top:10px;" action="#{bean.submit}" />
写入你的bean
public void submit(){}
您的方法可以包含任何您想要的内容。
有很好的jsf tutorial here那些是我用来开始的(虽然我没有完成)。
我有个小问题。我想从我输入的输入区域中获取值,但它没有正确更新。字符串值始终为空。
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;
}
}
他似乎没有找不到我的豆子,因为我给按钮的值在浏览器中正确显示。但是输入不起作用。 我已经和 jsf 一起工作过,我从来没有遇到过通过 bean 传递值的问题。我的另一个项目正在使用与这里相同的方法并且它有效。但在这种情况下,我的输入不会进入 bean。请帮忙
您必须提交表单,bean 才能获取值。否则永远不会发送表格。
因此,向您的命令按钮添加一个操作,即按下按钮时将调用的方法。
<p:commandButton value="#{bean.button}"
style="margin-left:10px;margin-top:10px;" action="#{bean.submit}" />
写入你的bean
public void submit(){}
您的方法可以包含任何您想要的内容。
有很好的jsf tutorial here那些是我用来开始的(虽然我没有完成)。