jsf中的绝对路径不起作用

时间:2013-02-20 02:00:07

标签: java jsf jsf-2 primefaces

我是jsf的新手。

我基本上有4个jsf文件。

1)menu.xhtml

<h:html xmlns="http://www.w3.org/1999/xhtml" 
xmlns:c="http://java.sun.com/jsp/jstl/core"
xmlns:h="http://java.sun.com/jsf/html"    
xmlns:f="http://java.sun.com/jsf/core"
xmlns:ui="http://java.sun.com/jsf/facelets" 
xmlns:p="http://primefaces.org/ui">

 <h:head>

<h:body>

  <p:layout fullPage="true">
   <p:layoutUnit position="north" style="min-width: 300px;" gutter="0">
    <ui:insert name="header">
     <ui:include src="../../layout/header.xhtml" />
    </ui:insert>
   </p:layoutUnit>

   <p:layoutUnit position="center" style="border:none;" gutter="0">
    <p:panel id="content" style="background : transparent; border : 0;">
     <ui:include src="content.xhtml" />
    </p:panel>
   </p:layoutUnit>

   <p:layoutUnit position="west" collapsible="true" gutter="6"  >
   <h:panelGroup id="menu" layout="block">
   <h:form>
    <ul>
     <li><p:commandLink value="goto-include1" action="#{bean.doAction1}" update=":mainContent" ajax="true" /></li>
     <li><p:commandLink value="goto-include2" action="#{bean.doAction2}" update=":mainContent" ajax="true" /></li>
    </ul>
   </h:form>
  </h:panelGroup>
   </p:layoutUnit>

   <p:layoutUnit position="south" style="min-width: 300px;" gutter="0">
    <div id="footer" class="ui-widget ui-widget-header">
     <ui:include src="../../layout/footer.xhtml"></ui:include>
    </div>
   </p:layoutUnit>

  </p:layout>`enter code here`


 </h:body>
</f:view>
</h:html>

Include1.jsp

<ui:composition xmlns="http://www.w3c.org/1999/xhtml"
    xmlns:c="http://java.sun.com/jsp/jstl/core"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:p="http://primefaces.org/ui">
        <h:outputText value="Include1"/>
</ui:composition>

Include2.jsp

<ui:composition xmlns="http://www.w3c.org/1999/xhtml"
    xmlns:c="http://java.sun.com/jsp/jstl/core"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:p="http://primefaces.org/ui">
        <h:outputText value="Include1"/>
</ui:composition>

Include3.jsp

<ui:composition xmlns="http://www.w3c.org/1999/xhtml"
    xmlns:c="http://java.sun.com/jsp/jstl/core"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:p="http://primefaces.org/ui">
        <h:outputText value="Include1"/>
</ui:composition>

Content.jsp

<ui:composition xmlns="http://www.w3c.org/1999/xhtml"
    xmlns:c="http://java.sun.com/jsp/jstl/core"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:p="http://primefaces.org/ui">
    <h:panelGroup id="mainContent" layout="block">
        <ui:include src="#{bean.page}.xhtml" />
    </h:panelGroup>
</ui:composition>

我的bean类是

package com.assia.dslo.expresse.gui.component.pe;

import java.io.File;
import java.io.Serializable;

public class Bean implements Serializable {

    private String page = "include3";
    public String getPage() {
        return page;
    }
    public void setPage(String page) {
        this.page = page;
    }
    private static final long serialVersionUID = -802772877129109794L;

    public void doAction1() {
        this.page = "expresse/pe/include1.xhtml";
    }

    public void doAction2() {
        this.page = "include2";
    }
}

预期的行为是,当我单击Include1链接时,它会在中心框架中显示include1.jsf,对于Include2链接也是如此。

但是,Include1的绝对路径不起作用。如果我用相对路径替换绝对路径,那么它工作正常。有人可以帮我理解为什么会这样吗?这是预期的行为,还是我做错了什么。

由于

1 个答案:

答案 0 :(得分:2)

绝对路径以斜杠/开头,它会将您带到根目录。您的路径不是以斜杠/开头,因此根本不是绝对路径。

相应地修复它。取代

this.page = "expresse/pe/include1.xhtml";

通过

this.page = "/expresse/pe/include1.xhtml";

对具体问题

无关,建议在/WEB-INF文件夹中放置不应直接公开访问的文件。否则,当在浏览器的地址栏中输入/猜测其路径时,最终用户将获得部分/损坏的页面。

this.page = "/WEB-INF/expresse/pe/include1.xhtml";

对所有其他包含和模板文件执行相同操作。不要忘记用/WEB-INF开头的绝对路径替换它们的相对路径。

另见: