在Java Server Faces中使用带有CommandButton的托管属性 [英] Using managed-property with CommandButton in Java Server Faces
问题描述
除了我的问题在带有Facelets的Java Server Faces中创建编辑我的项目页面我会帮助解决这个问题。
In addition to my question "Creating an "Edit my Item"-page in Java Server Faces with Facelets" I would liek to cover an issue that this provided.
我按下commandButton,ID = 100被删除,页面刷新,这是之前它甚至运行方法,对,这意味着当我按下按钮时我没有ID 。
When I press the commandButton the ID=100 is removed and the page is refreshed, and this is Before it even runs the method, right, so this means that I do not have the ID when i press the button.
你如何解决这个问题?
拥有这个Managed Bean
By having this Managed Bean
public class BeanWithId implements Serializable {
private String id;
private String info;
private void populateInfo() {
info = "Some info from data source for id=" + id;
}
public String getId() { return id; }
public void setId(String id) {
this.id = id;
populateInfo();
}
public String getInfo() { return info; }
public void setInfo(String info) { this.info = info; }
public String save() {
System.out.println("Saving changes to persistence store");
return null; // no navigation
}
}
并添加
<p><h:commandButton action="#{beanWithId.save}" value="Save" /></p>
到我的facelet页面。现在我在faces-config.xml中也有正确的信息,当我使用?ID = 100访问我的页面时,我确实得到了正确的项目。
To my facelet-page. Now I also have the correct information in my faces-config.xml and when I access my page using ?ID=100 I do get the correct Item returned.
推荐答案
有几种方法可以保留原始GET URL的ID。我并不是想要全面。
There are several ways to preserve the ID from the original GET URL. I'm not attempting to be comprehensive.
向 commandLink
添加一个参数
Add a param to a commandLink
<h:commandLink action="#{beanWithId.save}" value="Save">
<f:param name="ID" value="#{param.ID}" />
</h:commandLink>
每次点击链接时,ID都将从参数设置。
Any time the link is clicked, the ID will be set from parameter.
使用隐藏字段
<h:form>
<h:inputHidden value="#{beanWithId.id}" />
<p>ID: <h:outputText value="#{beanWithId.id}" /></p>
<p>Info: <h:inputText value="#{beanWithId.info}" /></p>
<p><h:commandButton action="#{beanWithId.save}" value="Save" /></p>
</h:form>
任何时候发布表格,都会从表格中设置ID。
Any time the form is posted, the ID will be set from the form.
保留网址
由于表单网址不包含原始查询,帖子将从浏览器栏中的URL中删除ID。执行操作后,可以通过使用服务器端重定向来解决此问题。
Since the form URL does not include the original query, a post will remove the ID from the URL in the browser bar. This can be rectified by use of a server-side redirect after the action has been performed.
public String save() {
System.out.println("Saving changes to persistence store: id=" + id);
redirect();
return null; // no navigation
}
private void redirect() {
FacesContext context = FacesContext.getCurrentInstance();
ExternalContext ext = context.getExternalContext();
UIViewRoot view = context.getViewRoot();
String actionUrl = context.getApplication().getViewHandler().getActionURL(
context, view.getViewId());
try {
// TODO encode id value
actionUrl = ext.encodeActionURL(actionUrl + "?ID=" + id);
ext.redirect(actionUrl);
} catch (IOException e) {
throw new FacesException(e);
}
}
这篇关于在Java Server Faces中使用带有CommandButton的托管属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!