如何使用JSF2和RichFaces将行对象传递给支持bean?
我正在使用RichFaces的排序列表向用户显示一个表自定义命令对象。用户使用表单创建新命令,然后将这些命令添加到列表中。以下是orderingList实现:如何使用JSF2和RichFaces将行对象传递给支持bean?,jsf,richfaces,cdi,Jsf,Richfaces,Cdi,我正在使用RichFaces的排序列表向用户显示一个表自定义命令对象。用户使用表单创建新命令,然后将这些命令添加到列表中。以下是orderingList实现: app.xhtml <rich:orderingList id="oList" value="#{commandBean.newBatch}" var="com" listHeight="300" listWidth="350" converter="commandConverter"> <f:facet name=
app.xhtml
<rich:orderingList id="oList" value="#{commandBean.newBatch}" var="com"
listHeight="300" listWidth="350" converter="commandConverter">
<f:facet name="header">
<h:outputText value="New Batch Details" />
</f:facet>
<rich:column width="180">
<f:facet name="header">
<h:outputText value="Command Type" />
</f:facet>
<h:outputText value="#{com.commandType}"></h:outputText>
</rich:column>
<rich:column>
<f:facet name="header">
<h:outputText value="Parameters" />
</f:facet>
<h:outputText value="#{com.parameters}"></h:outputText>
</rich:column>
<rich:column>
<h:commandButton value="Remove #{com.id} : #{com.seqNo}"
action="#{commandBean.remove(com.id,com.seqNo)}"
onclick="alert('id:#{com.id} seqNo:#{com.seqNo}');"/>
</rich:column>
当我试图实现一个remove按钮时,我的问题就开始了,该按钮将命令的ID和seqNo发送给要从列表中删除的支持bean cb。下面是支持bean:
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
@ManagedBean
@SessionScoped
public class CommandBean implements Serializable{
private static final long serialVersionUID = 1L;
private CommandType type;
private String parameters;
private List<Command> newBatch = new ArrayList<Command>();
private Set<Command> commandSet = new HashSet<Command>();
private String msg = "not removed";
public CommandType[] getCommandTypes() {
return CommandType.values();
}
public void addCommand(CommandType type, String parameters) {
newBatch.add(new Command(type, parameters));
}
CommandType getType() {
return type;
}
void setType(CommandType type) {
this.type = type;
}
String getParameters() {
return parameters;
}
void setParameters(String parameters) {
this.parameters = parameters;
}
public List<Command> getNewBatch() {
return newBatch;
}
public void setNewBatch(List<Command> newBatch) {
this.newBatch = newBatch;
}
public Set<Command> getCommandSet() {
return commandSet;
}
public void setCommandSet(Set<Command> commandSet) {
this.commandSet = commandSet;
}
String getMsg() {
return msg;
}
public void remove(Integer id, Integer seqNo) {
for(Command c : newBatch) {
if(c.getId() == id && c.getSeqNo() == seqNo) {
newBatch.remove(c);
msg = "removed " + c;
return;
}
}
msg = String.format("%d : %d", id,seqNo);
}
}
当命令com的id和seqNo通过{cb.removecom.id,com.seqNo}传递时,它们都是0。我还从某个地方读到,空值被转换为0,这就可以解释它了。我还试图通过{cb.removecom}直接传递Command对象,但当bean试图处理它时,该命令为null
我敢打赌范围界定有问题,但我对JSF来说太陌生了,没法弄清楚
更新
我已经消除了冲突的@Named标记,并更新了html以反映bean的新名称,即commandBean。但是仍然存在问题。您试图从循环结束后for循环中使用的变量中获取值。当@var为null时,@操作正在服务器端解析 您可以这样做:
<a4j:commandButton action="#{commandBean.remove()}" … >
<a4j:param assignTo="#{commandBean.idToRemove}" value="#{com.id}"/>
</a4j:commandButton>
a4j:param解析客户端的值,单击按钮后将其发送到服务器。您可以将这两个值作为请求参数传递:
<h:commandButton ... >
<f:param name="id" value="#{com.id}"/>
<f:param name="seqNo" value="#{com.seqNo}"/>
</h:commandButton>
这是一个概念的快速证明,所以它是从一堆例子中结合起来的。有任何链接可以方便地找到正确的方法吗?通过EL向方法传递参数只能从EL-3获得。你应该确定这是你的版本,Elunfortute这样做不起作用。出于某种原因,从未调用过remove方法。@AlexanderAavang您可能需要将a4j:commandButton与execute=@一起使用,以使其正常工作。尽管它看起来有点笨重,但效果很好@AlexanderAavang您可能需要编写一个包装器方法来获取请求对象:
HttpServletRequest request = ((HttpServletRequest)FacesContext.getCurrentInstance().getExternalContext().getRequest());
System.out.println(request.getParameter("id"));
System.out.println(request.getParameter("seqNo"));