什么时候应该使用f:viewAction
or preRenderView
事件来初始化页面的数据而不是使用@PostConstruct
注释?基于支持bean的范围类型使用一个或另一个的基本原理例如,如果支持bean是@RequestScoped
,那么在呈现视图之前选择使用f:viewAction
或preRenderView
覆盖@PostConstruct
初始化支持bean是不相关的,因为两者会结果是一样的吗?
f:viewAction或preRenderView
<f:metadata>
<f:viewAction action="#{myBean.initialize}" />
</f:metadata>
Run Code Online (Sandbox Code Playgroud)
<f:metadata>
<f:event type="preRenderView" listener="#{myBean.initialize}"/>
</f:metadata>
Run Code Online (Sandbox Code Playgroud)
要么
@PostConstruct
public class MyBean
{
@PostConstruct
public void initialize()
{
}
}
Run Code Online (Sandbox Code Playgroud) 是否有可能告诉Guice在定时给定类型的对象后调用某个方法(即init())?
我在EJB 3中寻找类似于@PostConstruct注释的功能.
我在页面上使用datatable并使用绑定属性将其绑定到我的支持bean.这是我的代码: -
<?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:h="http://java.sun.com/jsf/html"
xmlns:p="http://primefaces.prime.com.tr/ui">
<h:head>
<title>Facelet Title</title>
</h:head>
<h:body>
<h:form prependId="false">
<h:dataTable var="item" value="#{testBean.stringCollection}" binding="#{testBean.dataTable}">
<h:column>
<h:outputText value="#{item}"/>
</h:column>
<h:column>
<h:commandButton value="Click" actionListener="#{testBean.action}"/>
</h:column>
</h:dataTable>
</h:form>
</h:body>
</html>
Run Code Online (Sandbox Code Playgroud)
这是我的豆子: -
package managedBeans;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.component.html.HtmlDataTable;
@ManagedBean(name="testBean")
@ViewScoped
public class testBean implements Serializable {
private List<String> stringCollection;
public List<String> getStringCollection() {
return stringCollection;
}
public void …
Run Code Online (Sandbox Code Playgroud) 在@PostConstruct文档中,它描述了带注释的方法:
"该方法绝不能抛出已检查的异常."
如何处理例如可以在这种方法中抛出的IOException?只需将其包装在RuntimeException中,让用户担心对象的错误初始状态?或者@PostConstruct是错误的地方来验证和初始化注入了依赖项的对象?
这似乎不对.我正在清理我的代码,我只是注意到了这一点.每个ajax请求都会触发构造函数和@PostConstruct
我的@ViewScoped
bean.即使是简单的数据库分页也会触发它.
我知道这@ViewScoped
比@RequestScoped
任何请求都要重建并且不应该重建.只有在通过GET重新加载完整页面之后.
如果我有一个带有@PostConstruct方法的类,我如何使用JUnit和Spring测试其构造函数及其@PostConstruct方法?我不能简单地使用新的ClassName(param,param),因为它不使用Spring - @PostConstruct方法没有被触发.
我错过了一些明显的东西吗?
public class Connection {
private String x1;
private String x2;
public Connection(String x1, String x2) {
this.x1 = x1;
this.x2 = x2;
}
@PostConstruct
public void init() {
x1 = "arf arf arf"
}
}
@Test
public void test() {
Connection c = new Connection("dog", "ruff");
assertEquals("arf arf arf", c.getX1();
}
Run Code Online (Sandbox Code Playgroud)
我有类似的东西(虽然稍微复杂一些)并且@PostConstruct方法没有被击中.
我有一个原型范围bean,我希望通过@Autowired注释注入它.在这个bean中,还有@PostConstruct方法,Spring没有调用它,我不明白为什么.
我的bean定义:
package somepackage;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
@Component
@Scope("prototype")
public class SomeBean {
public SomeBean(String arg) {
System.out.println("Constructor called, arg: " + arg);
}
@PostConstruct
private void init() {
System.out.println("Post construct called");
}
}
Run Code Online (Sandbox Code Playgroud)
我想要注入bean的JUnit类:
package somepackage;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@ContextConfiguration("classpath*:applicationContext-test.xml")
public class SomeBeanTest {
@Autowired
ApplicationContext ctx;
@Autowired
@Value("1")
private SomeBean someBean;
private SomeBean someBean2;
@Before
public void setUp() throws Exception { …
Run Code Online (Sandbox Code Playgroud) 在一个支持bean的@PostConstruct方法中,我调用了一个EJB,它可能会返回一些我希望通过p:messages在页面上显示的消息.但是,即使我添加FacesMessages,例如FacesContext.getCurrentInstance().addMessage(...),也不会使用FacesMessages更新p:messages.
如果我不是调用从页面的动作调用EJB(比如用户点击它调用调用EJB的方法在页面上的一个按钮,然后添加的FacesMessage(S)),那么messags显示用对:消息按预期.
如何在@PostConstruct期间添加Faces消息并在最初呈现页面时显示它们?
码:
Page1Controller.java:
@ManagedBean
public class Page1Controller
{
@PostConstruct
public void init()
{
FacesContext.getCurrentInstance().addMessage(null,
new FacesMessage("Test Message from @PostConstruct"));
}
public String getValue()
{
return "Some Value";
}
public void triggerMessage(ActionEvent event)
{
FacesContext.getCurrentInstance().addMessage(null,
new FacesMessage("Test Message from Trigger Button"));
}
}
Run Code Online (Sandbox Code Playgroud)
page1.xhtml
<h:form>
<p:messages showDetail="true" showSummary="true" autoUpdate="true"/>
<h:outputText value="#{page1Controller.value}"/>
<br/>
<p:commandButton value="Trigger Message"
actionListener="#{page1Controller.triggerMessage}"/>
</h:form>
Run Code Online (Sandbox Code Playgroud) 我正在使用JSF 2.0和GlassFish 3.0.
我有以下Managed Bean:
@ManagedBean
@RequestScoped
public class OverviewController{
private List<Event> eventList;
@PostConstruct
public void init(){
System.out.println("=> OverviewController - init() - enter");
System.out.println("=< OverviewController - init() - exit");
}
}
Run Code Online (Sandbox Code Playgroud)
在overview.xhtml文件中,我从OverviewController中调用了不同的属性或方法.
<ui:repeat var="event" value="#{overviewController.eventList}">
...
</ui:repeat>
Run Code Online (Sandbox Code Playgroud)
一切正常,但问题出在日志文件中:
INFO: Enter : RESTORE_VIEW 1
INFO: Exit : RESTORE_VIEW 1
INFO: Enter : RENDER_RESPONSE 6
INFO: => OverviewController - init() - enter
INFO: => Overview Controller - updateSelectedTab() - enter
INFO: =< Overview Controller - updateSelectedTab() - exit
INFO: =< …
Run Code Online (Sandbox Code Playgroud) postconstruct ×10
java ×5
jsf ×5
jsf-2 ×3
spring ×2
view-scope ×2
constructor ×1
facelets ×1
guice ×1
init ×1
java-ee ×1
javadoc ×1
junit ×1
message ×1
pageload ×1
unit-testing ×1
viewaction ×1