【问题标题】:absolute path in jsf not workingjsf中的绝对路径不起作用
【发布时间】:2013-02-20 02:00:07
【问题描述】:

我是 jsf 的新手。

我基本上有 4 个 jsf 文件。

1) 菜单.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>

内容.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 的绝对路径不起作用。如果我用相对路径替换绝对路径,那么它工作正常。有人可以帮我理解为什么会这样吗?这是预期的行为还是我做错了什么。

谢谢

【问题讨论】:

  • 绝对路径工作正常。您的绝对路径是否以斜杠开头(例如/expresse/pe/include1.xhtml)?绝对路径从webapp 目录开始,对于存储在src/main/webapp/a/b/c.xhtml 中的文件(我假设您使用Maven),绝对路径将为/a/b/c.xhtml

标签: java jsf jsf-2 primefaces


【解决方案1】:

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

相应地修复它。替换

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

通过

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

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

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

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

另见:

【讨论】:

  • 是的,你是对的。这行得通。这是您对 stackoverflow 中先前问题的解决方案的扩展。感谢您的帮助
猜你喜欢
  • 2013-07-06
  • 1970-01-01
  • 2020-12-23
  • 2018-03-28
  • 2015-08-29
  • 1970-01-01
  • 1970-01-01
  • 2013-10-23
  • 1970-01-01
相关资源
最近更新 更多