Java h:commandLink 在 dataTable 中不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9653078/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
h:commandLink not working inside dataTable
提问by Fahim Parkar
In my JSF file I have below at the start.
在我的 JSF 文件中,我的开头如下。
<h:form><h:commandLink value="Create New Staff Account" action="adminCreateStaffMember"/></h:form>
.
<h:form><h:commandLink value="Create New Staff Account" action="adminCreateStaffMember"/></h:form>
.
By use of this, when I create on Create New Staff Account
I get re-directed to the page where I have form to create new account.
通过使用这个,当我创建时,Create New Staff Account
我被重定向到我有表单来创建新帐户的页面。
BUT, When I use the same inside dataTable
, NO ACTIONis taken. I am still on same page :(
但是,当我用同样的内部dataTable
,NO ACTION取。我还在同一页上:(
<h:dataTable var="c" value="#{newStaffMemberServiceBean.newStaffMemberDataBeanList}"
styleClass="order-table"
headerClass="order-table-header"
rowClasses="order-table-odd-row,order-table-even-row"
border="1" id="staffListDataTable" width="100%">
<h:column>
<f:facet name="header">
Staff Member Name
</f:facet>
<h:form><h:commandLink value="Create New Staff Account" action="adminCreateStaffMember"/></h:form>
</h:column>
</h:dataTable>
Actually what I wanted to print is details of respective staff member where I would be using f:setPropertyActionListener
. But as above is not working, I won't go ahead.
实际上,我想打印的是我将使用的各个工作人员的详细信息f:setPropertyActionListener
。但由于上述不起作用,我不会继续。
Please suggest me where I am going wrong.
请建议我哪里出错了。
Update 1
更新 1
My newStaffMemberServiceBean
is in RequestScoped
我newStaffMemberServiceBean
的在RequestScoped
@ManagedBean(name = "newStaffMemberServiceBean")
@RequestScoped
public class NewStaffMemberServiceBean {
// some code
}
Update 2
更新 2
HTML Generated are as below
生成的 HTML 如下
Outside of dataTable
在外面 dataTable
<div align="right">
<form id="j_idt35" name="j_idt35" method="post" action="/adminManageStaffMember" enctype="application/x-www-form-urlencoded">
<input type="hidden" name="j_idt35" value="j_idt35" />
<a href="#" onclick="mojarra.jsfcljs(document.getElementById('j_idt35'),{'j_idt35:j_idt36':'j_idt35:j_idt36'},'');return false">Create New Staff Account</a><input type="hidden" name="javax.faces.ViewState" id="javax.faces.ViewState" value="2664682277023387375:-3250423983171933030" autocomplete="off" />
</form>
Inside dataTable
里面 dataTable
<form id="staffListDataTable:0:j_idt43" name="staffListDataTable:0:j_idt43" method="post" action="/adminManageStaffMember" enctype="application/x-www-form-urlencoded">
<input type="hidden" name="staffListDataTable:0:j_idt43" value="staffListDataTable:0:j_idt43" />
<a href="#" onclick="mojarra.jsfcljs(document.getElementById('staffListDataTable:0:j_idt43'),{'staffListDataTable:0:j_idt43:j_idt45':'staffListDataTable:0:j_idt43:j_idt45'},'');return false">Create New Staff Account</a><input type="hidden" name="javax.faces.ViewState" id="javax.faces.ViewState" value="2664682277023387375:-3250423983171933030" autocomplete="off" />
</form>
采纳答案by prajeesh kumar
I would suggest a few modifications for your original code:
我建议对您的原始代码进行一些修改:
don't use
h:form
for each of yourcommandLink
, instead use one form for yourdataTable
.the
action
will get executed only when it finds the component that triggered the aciton. If you use a request scoped list for yourdatatable
then that list won't be there when the view is restored or may not match the original one which was used to create the view. You may have to change the managed bean from request scope to view scope or higher.as a suggestion, you can use
h:outputLink
orh:link
instead of thecommandLink
and provide thef:param
to simplify this, in which case you don't need the form also.
不要
h:form
为您的每commandLink
一个使用,而是为您的dataTable
.在
action
只有当它发现所触发aciton组件将得到执行。如果您使用请求范围列表,datatable
那么当视图恢复时该列表将不存在,或者可能与用于创建视图的原始列表不匹配。您可能必须将托管 bean 从请求范围更改为查看范围或更高范围。作为建议,您可以使用
h:outputLink
或h:link
代替commandLink
并提供f:param
来简化这一点,在这种情况下,您也不需要表单。
回答by Daniel
Is your table being wrapped by another form ?
你的桌子被另一种表格包裹了吗?
if that's the case and you got form inside form...
如果是这样的话,你得到了表格里面的表格......
remove the form tags that wraps the <h:commandLink value="Create New Staff Account" action="adminCreateStaffMember"/>
删除包装的表单标签 <h:commandLink value="Create New Staff Account" action="adminCreateStaffMember"/>
something like this
像这样的东西
<h:column>
<f:facet name="header">
Staff Member Name
</f:facet>
<h:commandLink value="Create New Staff Account" action="adminCreateStaffMember"/>
</h:column>
回答by Luiggi Mendoza
I guess your commandLink action method is wrong setted. You have this code:
我猜您的 commandLink 操作方法设置错误。你有这个代码:
<h:commandLink value="Create New Staff Account" action="adminCreateStaffMember"/>
You should have something like this:
你应该有这样的事情:
<h:commandLink value="Create New Staff Account"
action="#{newStaffMemberServiceBean.adminCreateStaffMember"/>
Besides, like @Daniel has posted, you must have only 1 form to wrap your dataTable
and the commandLink
s inside the dataTable.
此外,就像@Daniel 发布的那样,您必须只有 1 个表单才能将您dataTable
的commandLink
s和s包装在数据表中。
UPDATE:
更新:
There is an important things you must know:
有一个重要的事情你必须知道:
If you have a list as an attribute of your managed bean, this list is used to display info (i.e. using a dataTable) and you want to include some button or link inside the datatable, your managed bean MUST have a reference of the list in the creator. This can be achieved by at least 2 ways:
如果你有一个列表作为你的托管 bean 的一个属性,这个列表用于显示信息(即使用数据表)并且你想在数据表中包含一些按钮或链接,你的托管 bean 必须在创造者。这可以通过至少两种方式实现:
- Save and load the list from session (very bad design, and it pretty hard to maintain the code).
- Set the bean to ViewScoped (JSF2.x perspective. If you're with JSF 1.2, you can try the
a4j:keepAlive
tag from RichFaces ort:saveState
from MyFaces).
- 从会话中保存和加载列表(非常糟糕的设计,并且很难维护代码)。
- 将 bean 设置为 ViewScoped(JSF2.x 透视图。如果您使用的是 JSF 1.2,您可以尝试使用
a4j:keepAlive
RichFaces 或t:saveState
MyFaces 中的标记)。
Having said this, and since you're using JSF 2, you can use the ViewScope on your bean. With this, the bean won't be recreated after doing a request on the same view, so the list will be savedautomatically inside the bean. The bean should look like this:
话虽如此,由于您使用的是 JSF 2,您可以在 bean 上使用 ViewScope。这样,在同一视图上执行请求后将不会重新创建 bean,因此列表将自动保存在 bean 中。bean 应如下所示:
@ManagedBean(name = "newStaffMemberServiceBean")
@ViewScoped
public class NewStaffMemberServiceBean implements Serializable {
private List<StaffMember> lstStaffMember;
//getters and setters...
public NewStaffMemberServiceBean() {
//populate the list...
lstStaffMember = new ArrayList<StaffMember>();
}
//managing the navigation using the id as a parameter
//additional, this can (and MUST BE) improved.
public String viewStaffMemberDetails() {
//get the id parameter from the link
int id = Integer.valueOf(FacesContext.getCurrentInstance()
.getExternalContext().getRequestParameterMap().get("id"));
//searching that id in our staff member list
for(StaffMember sm : lstStaffMember) {
if (sm.getId() == id) {
//setting the staff member object in session
FacesContext.getCurrentInstance().getExternalContext()
.getSessionMap().put("staffMember", sm);
//returning the name of the page we want to navigate
return "adminCreateStaffMember";
}
}
return "";
}
}
Now, let's check the xhtml. It's pretty simple:
现在,让我们检查 xhtml。这很简单:
<h:form>
Welcome
<br />
<h:dataTable
value="#{newStaffMemberServiceBean.lstStaffMember}"
var="staffMember">
<h:column>
<f:facet name="header">
Name
</f:facet>
<h:outputText value="#{staffMember.name}" />
</h:column>
<h:column>
<f:facet name="header">
View Details
</f:facet>
<h:commandLink value="Show details"
action="#{newStaffMemberServiceBean.viewStaffMemberDetails}">
<f:param name="id" value="#{staffMember.id}" />
</h:commandLink>
</h:column>
</h:dataTable>
</h:form>
And that's it! You can navigate to adminCreateStaffMember.xhtml with no problems, and use the staffmember object in the adminCreateStaffMember managed bean to show the data (details). I've tried this code myself (I'm in a new pc so I have to check the code before posting, install software, etc...).
就是这样!您可以毫无问题地导航到 adminCreateStaffMember.xhtml,并使用 adminCreateStaffMember 托管 bean 中的员工对象来显示数据(详细信息)。我自己试过这个代码(我在一台新电脑上,所以我必须在发布、安装软件等之前检查代码......)。