Using complex urls & regular expressions with PrettyFaces

If you like to construct complex & pretty urls with JSF2 & PrettyFaces, you might be interested in the following few lines of code.

In our example, we want to match a URL like this one

1
http://www.studyguru.eu/at/tuwien/184.153--Entwurfsmethoden-fuer-verteilte-Systeme

Previously I tried to match it with a PrettyFaces Pattern/Regex like this:

       <pattern value="/([a-z]{2})/([a-z0-9\-_]*)/([a-z0-9\-_\.]*)\-\-.*"/>

But thankfully, PrettyFaces >2.0.4 supports directly populating the RequestParams!

Configure your pretty-config like this:

1
2
3
4
5
6
7
8
9
10
11
<pretty-config xmlns="http://ocpsoft.com/prettyfaces/2.0.4"
               xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
               xsi:schemaLocation="http://ocpsoft.com/prettyfaces/2.0.4
                http://ocpsoft.com/xml/ns/prettyfaces/ocpsoft-pretty-faces-2.0.4.xsd">
....
 
    <url-mapping id="coursePage">
        <pattern value="/#{countryCode}/#{uniShortName}/#{courseId}--.*"/>
        <view-id>/path/to/coursePage.xhtml</view-id>
    </url-mapping>
....

and use JSF2’s viewParams like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
<?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">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:f="http://java.sun.com/jsf/core"
      xmlns:pretty="http://ocpsoft.com/prettyfaces"
      xmlns:ui="http://java.sun.com/jsf/facelets"
      xmlns:p="http://primefaces.prime.com.tr/ui"
        >
<body>
<f:metadata>
    <f:viewParam id="countryCodeId"
                 required="true"
                 requiredMessage="Kein Land spezifiziert"
                 name="countryCode" value="#{coursePageBean.countryCode}"
            >
        <f:validateRegex pattern="([a-z]{2})"/>
    </f:viewParam>
 
    <f:viewParam id="universityCodeId"
                 required="true"
                 requiredMessage="Keine Hochschule spezifiziert"
                 name="uniShortName" value="#{coursePageBean.universityCode}"
            >
        <f:validateRegex pattern="([a-z0-9\-_]*)"/>
    </f:viewParam>
    <f:viewParam id="courseIdId"
                 required="true"
                 requiredMessage="Keine KursId spezifiziert"
                 name="courseId" value="#{coursePageBean.courseId}"
            >
        <f:validateRegex pattern="([a-z0-9\-_\.]*)"/>
    </f:viewParam>
 
    <f:event type="preRenderView" listener="#{coursePageBean.populate}"/>
</f:metadata>
<h1>coursePage</h1>
 
countryCode #{ coursePageBean.countryCode} <br/>
courseId #{coursePageBean.courseId}<br/>
universityCode #{coursePageBean.universityCode}<br/>
</body>
</html>

Voila! You now can match complex urls with PrettyFaces and apply all your custom validators
to your Pretty URL 🙂

Join the Conversation

1 Comment

Leave a comment

Your email address will not be published. Required fields are marked *