Simple rest mapping to page - jsf

I am trying the prettyfaces library for REST url mapping to JSF2 pages.
I first set up prettyfaces in maven's pom.xml:
<dependency>
<groupId>com.ocpsoft</groupId>
<artifactId>prettyfaces-jsf2</artifactId>
<version>3.3.3</version>
</dependency>
Then my pretty-config.xml:
<pretty-config xmlns="http://ocpsoft.com/prettyfaces/3.3.2"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://ocpsoft.com/prettyfaces/3.3.2
http://ocpsoft.com/xml/ns/prettyfaces/ocpsoft-pretty-faces-3.3.2.xsd">
<url-mapping id="view-marchi">
<pattern value="/marchi/{urlMarchio}" />
<view-id value="/marchio.xhtml" />
</url-mapping>
</pretty-config>
I have a marchio.xhtml in my webapp root folder. It's working directly accessed: www..com/marchio.xhtml.
However the mapping doesn't work as I expected:
www..com/marchi/testparam
404 - Not found!
I don't get the point... am I doing something wrong? Or maybe there's something misconfigured..?

I believe the parameters in the mapping have to match #{param-name}. As I can see, you have missed the #. Change your pattern in the url-mapping to:
<pattern value="/marchi/#{urlMarchio}" />

Related

pretty faces does not work

I tried pretty faces with my jsf app.URL has not changed.I followed the steps mentioned on the site.
pom.xml
<dependency>
<groupId>org.ocpsoft.rewrite</groupId>
<artifactId>rewrite-servlet</artifactId>
<version>2.0.12.Final</version>
</dependency>
<dependency>
<groupId>org.ocpsoft.rewrite</groupId>
<artifactId>rewrite-config-prettyfaces</artifactId>
<version>2.0.12.Final</version>
</dependency>
I added pretty-config.xml in WEB-INF/
pretty-config.xml
<pretty-config xmlns="http://ocpsoft.org/schema/rewrite-config-prettyfaces"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://ocpsoft.org/schema/rewrite-config-prettyfaces
http://ocpsoft.org/xml/ns/prettyfaces/rewrite-config-prettyfaces.xsd">
<url-mapping id="login">
<pattern value="/login" />
<view-id value="/pages/unsecure/login.jsf" />
</url-mapping>
</pretty-config>
my local project url(full url)
http://localhost:9080/projectName/pages/unsecure/login.jsf
I use myfaces2.2.7,spring/security,hibernate,tomcat7
Is there another setting I need to do?What I'm missing.I dont understand.
What exactly should I do?Thanks in advance..
UPDATE:
I don't get any error.just does not work. URL does not change..
The URL in the browser isn't going to change automatically. PrettyFaces maps pretty URLs to internal URLs. So if you request:
http://localhost:9080/projectName/login
You would actually see the /pages/unsecure/login.jsf page as specified in the configuration. Navigation using JSF navigation or internal redirects to this page will automatically use the pretty URL.
If you want to automatically redirect from the internal URL to the pretty URL from external Requests (like in your example,) then you need to add a rewrite condition to do that:
<pretty-config xmlns="http://ocpsoft.org/schema/rewrite-config-prettyfaces"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://ocpsoft.org/schema/rewrite-config-prettyfaces
http://ocpsoft.org/xml/ns/prettyfaces/rewrite-config-prettyfaces.xsd">
<url-mapping id="login">
<pattern value="/login" />
<view-id value="/pages/unsecure/login.jsf" />
</url-mapping>
<rewrite match="/pages/unsecure/login.jsf" substitute="/login" redirect="301"/>
</pretty-config>
Alternatively, you can use Rewrite directly for both of these rules (since you are already using Rewrite with the PrettyFaces extension), using the Join rule:
#RewriteConfiguration
public class ExampleConfigurationProvider extends HttpConfigurationProvider
{
#Override
public int priority()
{
return 10;
}
#Override
public Configuration getConfiguration(final ServletContext context)
{
return ConfigurationBuilder.begin()
.addRule(Join.path("/login").to("/pages/unsecure/login.jsf").withInboundCorrection());
}
}
Note the .withInboundCorrection() method call. This sets up the inbound redirect from the OLD url to the NEW url automatically.
I hope this helps. Cheers!

URL rewrite for JSF

I'm using JSF, Jboss. I used urlrewrite filter and i don't know why:
when i type localhost:8080/myweb/user/myname will be forwarded to localhost:8080/myweb/user.xhtml?u=myname. it makes me don't like urlrewritefilter.
After that, i try using prettyfaces. Maybe, it is good for others, not me. i can't find out good tutorials except the documentation. ajax error after adding prettyfaces into my project. And some codes in pretty-config.xml
<url-mapping id="ideas">
<pattern value="/article/#{g}" />
<view-id value="/ideas/article.xhtml" />
</url-mapping> -->
And a form in jsf page will redirect this page with param
public String addUserToGroup() {
...
return "/ideas/article.xhtml?g=" + g + "&faces-redirect=true";
}
can't run.
Can you give me some advices about what library i should use to rewrite URL now. Or how to fix errors of prettyfaces. thanks
Is your configuration commented out? I noticed the '-->' in your code snippit.
<url-mapping id="ideas">
<pattern value="/article/#{g}" />
<view-id value="/ideas/article.xhtml" />
</url-mapping> -->

Can I replace pretty-config.xml configuration with annotations

I use the configuration file pretty-config.xml to rewrite urls:
<pretty-config xmlns="http://ocpsoft.com/prettyfaces/3.3.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://ocpsoft.com/prettyfaces/3.3.0
http://ocpsoft.com/xml/ns/prettyfaces/ocpsoft-pretty-faces-3.3.0.xsd">
<url-mapping id="connect_ss">
<pattern value="/sicav_security" />
<view-id value="/AuthentificationSS.jsf" />
</url-mapping>
<url-mapping id="connect_oblig">
<pattern value="/sicav_oblig" />
<view-id value="/AuthentificationCAPOBLIG.jsf" />
</url-mapping>
</pretty-config>
Can I replace it with annotations?
If I understand your question correctly, you are asking whether it is possible to configure PrettyFaces using annotations.
Yes, that is possible. You can simply use #UrlMapping for that:
#URLMapping(id="connect_ss", pattern="/sicav_security",
viewId="/AuthentificationSS.jsf")
public class SomeBean {
/* Your code */
}
Have a look at this part of the official documentation for details:
http://ocpsoft.org/docs/prettyfaces/3.3.3/en-US/html/Configuration.html#config.annotations

using pretty faces with web filters

Using Tomcat 7 --- Primefaces 3.4.1 --- javax faces 2.1.17 --- prettyfaces-jsf2 3.3.3
I configured pretty faces on my project correctly but my web filters are not working with new urls which are written by pretty faces.
Here is an example pretty-config.xml
<url-mapping id="home">
<pattern value="/home"/>
<view-id value="/secure/homepage.xhtml"/>
</url-mapping>
<url-mapping id="register">
<pattern value="/register"/>
<view-id value="/public/register.xhtml"/>
</url-mapping>
<url-mapping id="welcome">
<pattern value="/"/>
<view-id value="/public/welcome.xhtml"/>
</url-mapping>
<url-mapping id="profile">
<pattern value="/profile/#{userId}"/>
<view-id value="/profile.xhtml"/>
</url-mapping>
login(welcome) and register pages are in "public" folder, and their web filter is defined with annotation : #WebFilter("/public/*")
for my home page in "secure" folder (exactly there will be more pages in the folder), i defined a web filter also and its annotation : #WebFilter("/secure/*)
pretty urls are working fine, but these filters are only working when i write the original urls.
1) How can i repair my webfilters ?
2) I also want to block user for entering original url. I know that pretty faces is hiding original urls fully but is there a way to do it ?
-- SOLVED -- thanks for BalusC
if you defined your filters with annotations, you can configure dispatcher settings like
#WebFilter(urlPatterns = "/public/*", dispatcherTypes = {DispatcherType.REQUEST, DispatcherType.FORWARD})
PrettyFaces uses like many URL-rewriting solutions RequestDispatcher#forward() to forward the request to the desired target resource.
Servlet filters, when mapped without any <dispatcher>, listens by default on "initial" requests only, not on forwarded, included, nor error'ed requests.
So, when you map another servlet filter in web.xml after the PrettyFaces one, then it would by default not be triggered, unless you explicitly set a <dispatcher> on FORWARD next to the default of REQUEST (you should keep this one for the case PrettyFaces actually doesn't need to perform a forward).
<filter-mapping>
...
<dispatcher>REQUEST</dispatcher>
<dispatcher>FORWARD</dispatcher>
</filter-mapping>
Or, for the case you're using #WebFilter on your filters, use the dispatcherTypes attribute:
#WebFilter(..., dispatcherTypes = { REQUEST, FORWARD })
Alternatively, if the filter in question doesn't change the request/response target in any way, e.g. setting the charset, compressing using Gzip, listening on exceptions, etc, then you can also just put it before the PrettyFaces one.

Pretty Faces: Generic URL mapping

Using Pretty Faces 3, I have some mappings like these:
<url-mapping id="search">
<pattern value="/search" />
<view-id value="/views/search.xhtml" />
</url-mapping>
<url-mapping id="edit">
<pattern value="/edit" />
<view-id value="/views/edit.xhtml" />
</url-mapping>
Is there any way to define all them using some wildcard, like this?
<url-mapping id="generic">
<pattern value="/*" />
<view-id value="/views/$1.xhtml" />
</url-mapping>
I know is possible to use EL in the view-id value, but I can manage it to work.
Its currently not possible to use a wildcard like this.
However as you already mentioned you could use a dynaview (EL expression for the view-id) to achieve something similar. We recently fixed a bug that prevented something like this to work correcty. Perhaps you could give 3.3.1-SNAPSHOT a try. My guess is that it will work fine now. Feel free to post a message on the forums if you have any further problems.
You might be better off using Rewrite which lets you do more powerful configuration:
https://github.com/ocpsoft/socialpm/blob/master/web/src/main/java/com/ocpsoft/socialpm/URLRewriteConfiguration.java
.addRule(Join.path("/{page}").to("/views/{page}.xhtml"))

Resources