3

I've some unique problem, but I think this is not problem, I just want to optimize the url that my website have.

so I want to change all

index.html

on every folder as example

culture

folder, so normally if we want access

index.html

in culture folder we can just type

culture/index.html

right? but i want to access by just type

culture/

Is it possible to do that? If so, how we produce? I'm using jsf 2 as programming language.

Kukeltje
  • 12,223
  • 4
  • 24
  • 47

2 Answers2

2

Maybe I misunderstand your problem, but this can be done by configuring welcome files in the web.xml file:

Web Application developers can define an ordered list of partial URIs called welcome files in the Web application deployment descriptor. The purpose of this mechanism is to allow the deployer to specify an ordered list of partial URIs for the container to use for appending to URIs when there is a request for a URI that corresponds to a directory entry in the WAR not mapped to a Web component. This feature can make your site easier to use, because the user can type a URL without giving a specific filename.

Note: Welcome files can be JSPs, static pages, or servlets.

Just:

<welcome-file-list>
    <welcome-file>index.jsf</welcome-file>
    <welcome-file>index.html</welcome-file>
</welcome-file-list>

I've tested this on WildFly 11 and it works, my project structure is:

+ WebContent
   - index.xhtml
   + folder_a
        - index.xhtml
   + folder_b
        - index.html
   + folder_c
        - other_name.xhtml
        - some_name.html

When I enter:

  • http://localhost:8080/myproject/ I get a content from WebContent/index.xhtml
  • http://localhost:8080/myproject/folder_a I get a content from WebContent/folder_a/index.xhtml
  • http://localhost:8080/myproject/folder_b I get a content from WebContent/folder_b/index.html

Does the .xhtml file contain JSF content? is it parsed? I always thought defining a jsf/facelets file as a welcome file did not work and you needed http redirects via 'meta-inf'

.jsf' is that an extension I am using in myweb.xml` file:

<servlet-mapping>
    <servlet-name>Faces Servlet</servlet-name>
    <url-pattern>*.jsf</url-pattern>
</servlet-mapping>

Please read answers for the below question to learn what it is:

krokodilko
  • 35,300
  • 7
  • 55
  • 79
  • Does the .xhtml file contain JSF content? is it parsed? I always thought defining a jsf/facelets file as a welcome file did not work and you needed http redirects via 'meta-inf' (see the last note in your quote) – Kukeltje Aug 15 '17 at 12:32
  • I've updated the answer with an explanation hat is `jsf` – krokodilko Aug 15 '17 at 12:40
1

JSF 2.0 does not have support for extensionless URL by default. This is a long waited feature for the community, but finally we got this with JSF 2.3 (released early 2017). If you care able to update your application to JSF 2.3, or maybe want some more information, please check it out here.

A brief example of how you can make the default JSF approach for extensionless URLs:

In the web.xml file, include each desired mapping using a tag inside the tag:

  <url-pattern>/page1</url-pattern>
  <url-pattern>/page2</url-pattern>
  <url-pattern>/path/page1</url-pattern>

These patterns will redirect the user to the following paths respectively:

  • www.domain.com/page1 -> www.domain.com/page1.xhtml
  • www.domain.com/page2 -> www.domain.com/page2.xhtml
  • www.domain.com/path/page1 -> www.domain.com/path/page1.xhtml

Note that the above examples are not providing a way to redirect to the index.xhtml page, they are just a example of how the new JSF 2.3 mapping work.

However, if you can't update your project right now, I suggest you to follow @Kukeltje advice and take a look on the PrettyFaces library.

PrettyFaces is focused on provide a simple way to create URL mappings for older JSF versions. Heres a example of how it works:

In the pretty-config.xml file, include your desired mappings using a tag:

<url-mapping id="view-page">
    <pattern value="/page" />
    <view-id value="/page/index.xhtml" />
</url-mapping>
<url-mapping id="view-page-edit">
    <pattern value="/page/#{id}" />
    <view-id value="/page/edit.xhtml" />
</url-mapping>

These patterns would work like the following examples:

  • www.domain.com/page -> www.domain.com/page/index.xhtml
  • www.domain.com/page/2 -> www.domain.com/page/edit.xhtml?id=2

Both are good alternatives, PrettyFaces looks more powerful right now, since they are support this kind of solution for more time, but the JSF 2.3 should also do the trick.

Bonifacio
  • 1,482
  • 10
  • 19