【发布时间】:2013-07-25 17:46:56
【问题描述】:
真正的问题:我试图了解我发现的 javascript sn-p 和 Primefaces commandButton 之间的交互。 javascript sn-p 分配给 JSF h:form 标记的 onkeypress 属性。 我有这个 sn-p 在我的一些页面上工作,但在其他页面上没有。当我试图去除不必要的东西来问那个问题时,javascript 完全停止了工作。
似乎有些东西我不明白,因为这段代码不起作用。请注意,如果用户实际单击按钮,代码确实有效。当他们按下 Enter 键时,我希望它能够工作。我已经尽可能地剥离了样式指南并对其进行了精简以使其更短。
<!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:p="http://primefaces.org/ui">
<h:head>
<title>Tray Report</title>
<h:outputStylesheet library="css" name="postalreports.css" />
</h:head>
<h:body>
<h:form id="thisform" onkeypress="if (event.keyCode == 13) { document.getElementById('thisform:btn').click(); return false; }" >
<p:panel id="panel" header="Report For Days">
<p:messages id="messages" />
<p:inputText id ="days" value="#{trayBean.days}" >
</p:inputText>
</p:panel>
<p:commandButton id="btn" value="RUN" actionListener="#{trayBean.run}"/>
</h:form>
</h:body>
</html>
import java.io.Serializable;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
@ManagedBean
@ViewScoped
public class TrayBean implements Serializable {
private Integer days = 1;
public TrayBean() {}
public void run() {
System.out.println("do something here");
}
public Integer getDays() { return days; }
public void setDays(Integer days) { this.days = days; }
}
附:我也试过 if (event.keyCode == 13) { this.submit(); }
在额外研究后添加: 令我懊恼的是,我找到了一个我应该早点找到的 Primefaces 特定解决方案。事实证明,只需添加 p:focus 标签,就根本不需要 JavaScript。这是实际工作的 XHTML 文件。支持 bean 没有变化。
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:p="http://primefaces.org/ui">
<h:head>
<title>Tray Report</title>
</h:head>
<h:body>
<h:form id="thisform">
<p:focus/>
<p:panel id="panel" header="Report For Days">
<p:messages id="messages" />
<p:inputText id ="days" value="#{trayBean.days}" >
</p:inputText>
</p:panel>
<p:commandButton id="btn" value="RUN" actionListener="#{trayBean.run}"/>
</h:form>
</h:body>
</html>
这将允许我完成对原始问题的分析(因为我在尚未工作的页面上有一个 p:focus 标签),我将单独发布。
【问题讨论】:
标签: javascript jsf-2 primefaces commandbutton