3

I'm working with JSF2.1 and RichFaces 4.1 in JBoss AS 6.1.0.Final. Right now, I'm focused in rewriting the URLs. After trying different approaches I decided to stick to PrettyFaces since it's really intuitive to use (and got it working in a couple of minutes).

There's a problem tough. The relative links to scripts/css got messed up because the URLs changed and the relative paths end up in 404. I can use absolute paths but that would force me to change many of the pages and to expose application's structure in the page's source code.

I'm thinking about a temporal workaround: Giving the backing bean the responsibility of managing the different levels of those relative links but re-using beans makes this a delicate matter.

My question is, is there defined way or best practice to manage this relative paths while rewriting the URLs?

EDIT

h:outputStylesheet and h:outputScript worked like a charm. All that remains is solving a little issue with CSSs referencing images in a relative way. Take this structure:

-------/resources
       |
       ---_img
       |
       ---_css
       |
       ---_js

A CSS file in the folder _css references the image image1.png located in the _img folder with the relative path ../_img/image1.png. The problem is that this ends up in 404 because it does not find the image in /myApp/javax.faces.resource/_img/image1.png.

Changing every ../ for #{request.contextPath}/resources inside the CSSs seems to work just fine but I wonder if there is a better way to do it. The relative path approach not working seems strange to me.

4

2 回答 2

5

首先,我不明白他们到底是如何以及为什么搞砸的。你不是很清楚。您很可能出于某种原因硬编码纯 HTML<script><link>(and <img>) 元素,而不是使用 JSF 提供的<h:outputScript><h:outputStylesheet>(and <h:graphicImage>) 组件。这些 JSF 组件可以采用相对于/resources文件夹的资源名称,它们将自动为上下文路径添加前缀,以便它以域相对 URL(带前导斜杠)而不是请求相对 URL(不带前导削减)。

给定以下文件夹结构(/resources文件夹名称是预定义的;您不能更改其名称):

WebContent
 |-- resources
 |    |-- css
 |    |    `-- style.css
 |    |-- img
 |    |    `-- logo.png
 |    `-- js
 |         `-- script.js
 |-- page.xhtml
 :

然后您应该能够让 JSF 自动生成正确的<script>, <link>(and <img>) 元素,如下所示:

<h:head>
    <h:outputStylesheet name="css/style.css" />
    <h:outputScript name="js/script.js" />
</h:head>
<h:body>
    <h:graphicImage name="img/logo.png" />
</h:body>

也可以看看:

于 2012-08-08T19:05:11.790 回答
2

您需要对 CSS 文件使用绝对 URL,例如:

/path/to/style.css

代替:

../style.css

如果可以的话,您还希望避免硬编码 url,并使用 JSF2 资源重定位功能:<h:outputStylesheet><h:outputScript>

以下是一些可能有帮助的链接:

http://ocpsoft.org/support/topic/problem-with-prettyfaces-and-primefaces

于 2012-08-08T19:11:06.657 回答