如何在 JSF 2x 中使用结果并在地址栏中保持相同的 url



我在用JSF和PrimeFaces开发网站方面很陌生,我花了很多时间研究我的问题,然后再在这里发布。感谢所有花时间阅读我的问题的人。

好吧,我在我的菜单模板页面中使用它:

<h:link value="Manage Examination" outcome="/backend/examination/index" />
...
<h:link  value="List Examinations..." outcome="/WEB-INF/include/backend/examination/List.xhtml" />
<h:link value="Add Examination..." outcome="/WEB-INF/include/backend/examination/Create.xhtml" />

我的 WEB-INF 文件夹具有如下结构:

WEB-INF
--include
\-----backend
\------'entity name'
\-------'create,read,update,delete.xhtml'

WEB-INF 之外是我的根网页文件夹,我这里有后端文件夹,它的结构:

webpages
--WEB-INF
--backend
\---'entity name'
\---index.xhtml

在每个 html 中,我都放置了以下代码:

<?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">
<ui:composition xmlns:ui="http://java.sun.com/jsf/facelets"
                xmlns:h="http://java.sun.com/jsf/html"
                xmlns:f="http://java.sun.com/jsf/core"
                xmlns:p="http://primefaces.org/ui"
                template="/WEB-INF/include/templates/backend.xhtml">
    <ui:define name="title">
        <h:outputText value="#{appBundle.ExaminationTitle}"/>
    </ui:define>
    <ui:define name="body">
        <ui:include src="/WEB-INF/include/backend/examination/List.xhtml"/>
    </ui:define>
</ui:composition>

我的问题是:

  1. 我没有在人脸配置中配置任何内容.xml所以 JSF 可以知道我是否单击链接吗?
  2. 即使我单击"列出考试"
  3. 或"添加考试"链接,我如何才能始终保持地址栏中的网址始终/backend/examination/index结果/WEB-INF/include/backend/examination/*

NNToan

您滥用了<h:link>

"result"属性的值应绑定到根是主面上下文路径的路径。JSF 框架将使用 <a/> 标记呈现您的 h:link,因此保持相同的地址是毫无疑问的。

<h:link value="Page 1" outcome="page1.xhtml" />

实际上将翻译成:

<a href="/faces/page1.xhtml">Page 1</a>

如果要在没有用户重定向的情况下执行操作,则应使用命令链接或命令按钮,返回字符串(还要注意不要在该字符串中包含faces-redirect参数)。

例如:

<h:commandLink value="Click here" action="#{YourBean.myAction}"/>

在您的背豆中:

public String myAction()
{
  // do your stuff here
  return ""; // in general return the view you want to be redirected on, "" means "here"
}

1.你不需要。h:link 呈现为 HTML 元素。因此,单击该链接会向指定的 URL 发出简单的 HTTP GET 请求,没有 JSF 回发,没有操作侦听器和没有动态导航,因此在这种情况下与 faces-config 无关.xml因此。

2.你不能。由于这是一个HTTP GET请求,浏览器将无法显示目标URL。若要保留 URL,必须进行 JSF 回发,并使用返回导航处理程序使用的结果的操作侦听器方法进行动态导航。JSF 始终回发到同一页面,因此尽管呈现了新视图,但 URL 仍会保留。

<h:form>
   <!--When you click the button you navigate to the display Examinations view -->
   <!--but URL in the browser address bar does not change--> 
   <h:commandButton value="Navigate" action="#{bean.displayExaminationsList()}"/>
</h:form>
@ManagedBean
public class Bean {
  public String displayExaminationsList() {
    //The view that maps to this viewID shall be defined in the faces-config.xml
    return "examinationsListViewID";
  }
}

最新更新