Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/.htaccess/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在应用程序启动(JSF)中调用操作方法_Jsf - Fatal编程技术网

在应用程序启动(JSF)中调用操作方法

在应用程序启动(JSF)中调用操作方法,jsf,Jsf,我们需要在调用应用程序的第一页时调用一个action方法。例如,第一个页面是index.jsp,当我们直接调用此页面时,不会调用action方法。为了实现这一点,我们编写了另一个页面,其中它使用java脚本单击按钮并调用操作方法,导航到index.jsp 我觉得JSF中应该有适当的方法来完成这项任务。赌法是什么?我已经告诉团队,我们可以在加载页面时调用构造函数中的action方法。这是正确的方法吗?可能的解决办法是什么 只需在@PostConstruct方法中执行应用程序范围bean的工作,该b

我们需要在调用应用程序的第一页时调用一个action方法。例如,第一个页面是
index.jsp
,当我们直接调用此页面时,不会调用action方法。为了实现这一点,我们编写了另一个页面,其中它使用java脚本单击按钮并调用操作方法,导航到index.jsp


我觉得JSF中应该有适当的方法来完成这项任务。赌法是什么?我已经告诉团队,我们可以在加载页面时调用构造函数中的action方法。这是正确的方法吗?可能的解决办法是什么

只需在
@PostConstruct
方法中执行应用程序范围bean的工作,该bean是
eager
ly构造的,或者至少绑定到页面

@ManagedBean(eager=true)
@ApplicationScoped
public class Bean {

    @PostConstruct
    public void init() {
        // Here.
    }

}
或者,如果JSF(阅读:
FacesContext
)在实际作业中没有相关角色,您也可以使用

如果您还没有使用Servlet3.0,请在
web.xml
中注册它,如下所示

<listener>
    <listener-class>com.example.Config</listener-class>
</listener>

com.example.Config
另见:
使用JSF2.0

如果您想在应用程序启动时(即使尚未访问)采取一些操作,您可以使用SystemEventListener并将其订阅到PostConstructApplicationEvent

侦听器示例:

package listeners;

import javax.faces.application.Application;
import javax.faces.event.AbortProcessingException;
import javax.faces.event.ListenerFor;
import javax.faces.event.PostConstructApplicationEvent;
import javax.faces.event.SystemEvent;
import javax.faces.event.SystemEventListener;

public class MySystemListener implements SystemEventListener{

    @Override
    public void processEvent(SystemEvent event) throws AbortProcessingException {
        System.out.println("started");
    }

    @Override
    public boolean isListenerForSource(Object source) {
        return source instanceof Application;
    }

}
要订阅,您必须在faces-config.xml中包含此片段

<application>
    <system-event-listener>
        <system-event-listener-class>
            listeners.MySystemListener
        </system-event-listener-class>
        <system-event-class>
            javax.faces.event.PostConstructApplicationEvent
        </system-event-class>
    </system-event-listener>
</application>


在JSF 1.2中,我认为您可以通过PhaseListener接收通知,并检查当前呈现的视图的id。

它不仅仅在启动时被调用,而是在每个视图上被调用。我必须进去。否则bean调用对我来说就不起作用了。我只想澄清一下,如果你在做依赖项注入,你需要用@PostConstruct注释一个方法,而不是把它放在构造函数中。
<application>
    <system-event-listener>
        <system-event-listener-class>
            listeners.MySystemListener
        </system-event-listener-class>
        <system-event-class>
            javax.faces.event.PostConstructApplicationEvent
        </system-event-class>
    </system-event-listener>
</application>
...
<h:body>
   <f:event type="preRenderView" listener="#{bean.action}"/>
   <h:form>
      <!--components-->
   </h:form>
</h:body>
...