gpt4 book ai didi

JSF index.xhtml 和重定向到面 Action

转载 作者:行者123 更新时间:2023-12-04 22:48:59 25 4
gpt4 key购买 nike

我认为拥有一个索引页(在我的例子中是 index.xhtml)是一个很好的做法。
我想在索引页面上传递一些操作(例如在 struts: <c:redirect url="list.do" /> 中,我转到没有任何链接和按钮的 struts 操作类)我知道如果我想使用导航,我应该使用 commandLink-s 或按钮)。我可以使用 onclick javascript 函数编写 <h:commandButton>,但我认为这不是最佳选择。

我对 JSF 完全陌生(使用 JSF 2.0),我需要您的建议。从索引页面重定向到 Controller 中的操作的最佳实践是什么?

///新版本

<!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:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core">
<f:view>
<ui:insert name="metadata"/>
<f:viewParam name="action" value="listItems.xtml"/>
<f:event type="preRenderView" listener="#{yourBean.methodInManagedBean}" />
<h:body></h:body>
</f:view>
</html>

public class ForwardBean {

private String action;

// getter, setter

public void navigate(PhaseEvent event) {
FacesContext facesContext = FacesContext.getCurrentInstance();
String outcome = action;
facesContext.getApplication().getNavigationHandler().handleNavigation(facesContext, null, outcome);
}
}

最佳答案

您可以通过以下方式使用 JSF preRenderView 事件重定向到另一个页面,

在你的 index.xhtml 文件中

<!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:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core">
<f:view>
<ui:insert name="metadata"/>
<f:event type="preRenderView" listener="#{yourBean.methodInManagedBean}" />
<h:body></h:body>
</f:view>
</html>

在托管 bean 中,
第一种方式是
    public class yourClass{

FacesContext fc = FacesContext.getCurrentInstance();
ConfigurableNavigationHandler nav = (ConfigurableNavigationHandler)fc.getApplication().getNavigationHandler();

public void methodInManagedBean() throws IOException {
nav.performNavigation("list.do");//add your URL here, instead of list.do
}
}

或者你可以使用 第二种方式
    public class yourClass{ 

public void methodInManagedBean() throws IOException {
FacesContext.getCurrentInstance().getExternalContext().redirect("list.do");//add your URL here, instead of list.do
}
}

关于JSF index.xhtml 和重定向到面 Action ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12815529/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com