JSF index.xhtml 和重定向到面 Action

标签 jsf redirect jsf-2 myfaces

我认为拥有一个索引页(在我的例子中是 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/

相关文章:

java - 菜单系统教程

jquery - 未捕获的语法错误 : Unexpected token e

redirect - Symfony 在用户条件下登录后重定向

java - JSF 2.0。无法在 preRenderView 事件处理程序中获取 POST 参数和 cookie

forms - JSF 2.0 使用 IncludeViewParams 提交表单(忽略空参数)

jsf - RichFaces 3 到 RichFaces 4 的迁移

java - 是否可以降级 JSF 中的 NamingContainer?

apache - 重定向不适用于 htaccess 中的 mod_rewrite

java - 使用 System.setOut() 重定向 Runtime.getRuntime().exec() 输出;

attributes - 复合组件属性中的枚举值