【问题标题】:JSF index.xhtml and redirect to faces actionJSF index.xhtml 并重定向到 faces 动作
【发布时间】:2012-10-10 08:53:44
【问题描述】:

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

我是 JSF 的新手(使用 JSF 2.0),我需要您的建议。从索引页面重定向到控制器中的操作的最佳做​​法是什么?

///新版本

<!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);
    }
}

【问题讨论】:

  • 修改后还能用吗?
  • 为什么你需要 这个?
  • 只需在我的解决方案中删除“list.do”部分并添加您的网址“listItems.xtml”并使用我给出的方法,即 public void methodInManagedBean() 指向

标签: jsf redirect jsf-2 myfaces


【解决方案1】:

您可以使用 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
    }
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-05-25
    • 2013-07-30
    • 1970-01-01
    • 1970-01-01
    • 2016-01-29
    • 2018-08-01
    • 2013-04-24
    • 1970-01-01
    相关资源
    最近更新 更多