在构造函数或 @PostConstruct 方法中设置消息 - 可能吗?
是否可以在 Managed-Bean 的构造函数或 @PostConstruct 方法中设置消息?
例如:
public class Example {
@ManagedProperty(value = "#{index.facade}")
private PersistenceFacade pf;
public Example() {
}
@PostConstruct
public void doExample() {
try {
pf.disconnect();
((HttpSession) FacesContext.getCurrentInstance()
.getExternalContext().getSession(false)).invalidate();
setMessage("Successful.");
} catch (DAOException e) {
e.printStackTrace();
setMessage("Error: " + e.toString());
}
}
public void setPf(PersistenceFacade pf) {
this.pf = pf;
}
private void setMessage(String message) {
FacesMessage fm = new FacesMessage(message);
FacesContext.getCurrentInstance().addMessage(null, fm);
}
public String back() {
return "/index.xhtml";
}
example.xhtml:
<?xml version="1.0" encoding="UTF-8"?>
<!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:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html">
<h:head>
<title>Example</title>
</h:head>
<h:body>
<h1>
<h:outputText value="Example" />
</h1>
<h:form>
<p>
<h:commandButton id="back" value="Back"
action="#{example.back}"></h:commandButton>
</p>
<h:messages globalOnly="true" />
</h:form>
</h:body>
</html>
我可以在 Eclipse-Console 中看到该消息,但在页面上看不到,为什么?有什么办法可以告知用户结果吗?
is it possible to set a message in constructor or @PostConstruct method of a Managed-Bean?
For example:
public class Example {
@ManagedProperty(value = "#{index.facade}")
private PersistenceFacade pf;
public Example() {
}
@PostConstruct
public void doExample() {
try {
pf.disconnect();
((HttpSession) FacesContext.getCurrentInstance()
.getExternalContext().getSession(false)).invalidate();
setMessage("Successful.");
} catch (DAOException e) {
e.printStackTrace();
setMessage("Error: " + e.toString());
}
}
public void setPf(PersistenceFacade pf) {
this.pf = pf;
}
private void setMessage(String message) {
FacesMessage fm = new FacesMessage(message);
FacesContext.getCurrentInstance().addMessage(null, fm);
}
public String back() {
return "/index.xhtml";
}
example.xhtml:
<?xml version="1.0" encoding="UTF-8"?>
<!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:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html">
<h:head>
<title>Example</title>
</h:head>
<h:body>
<h1>
<h:outputText value="Example" />
</h1>
<h:form>
<p>
<h:commandButton id="back" value="Back"
action="#{example.back}"></h:commandButton>
</p>
<h:messages globalOnly="true" />
</h:form>
</h:body>
</html>
I can see the message in the Eclipse-Console, but not on the page, why? Is there any way to inform the user about the result?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试在放置 h:messages 之前放置一些访问 jsf beans 属性的 EL
,而不是像这样
try to put some EL that access jsf beans properties before putting the h:messages
and not like this
我发现如果我以这种方式添加/更改托管 Bean 和 XHTML 文件,将会显示该消息:
在 Example.java 中:
在 example.xhtml 中:
I found out that the message will be shown if I add/change the Managed-Bean and the XHTML-file in this way:
In Example.java:
In example.xhtml: