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!
Related
I built my site to be Multi-language. I want the language code to be embedded in the address of page according to the locale. I have the following:
http://localhost:8080/Wirote/index
I want to have it as the following:
http://localhost:8080/Wirote/de/index --- display German content
http://localhost:8080/Wirote/en/index --- display English content
http://localhost:8080/Wirote/ar/index --- display Arabic content
To achieve this I followed the step in :
multi-language url rewiting. Is it possible?
pretty-config.xml
<url-mapping id="base">
<pattern value="/#{localeManger.language}"/>
</url-mapping>
<url-mapping id="index" parentId="base">
<pattern value="/index"/>
<view-id value="/index.xhtml"/>
</url-mapping>
faces-config.xml
<application>
<locale-config>
<default-locale>de</default-locale>
<supported-locale>en</supported-locale>
<supported-locale>ar</supported-locale>
</locale-config>
<resource-bundle>
<base-name>I18n.lang</base-name>
<var>sprache</var>
</resource-bundle>
</application>
LocaleManger.java
#ManagedBean(name = "localeManger")
#SessionScoped
public class LocaleManger implements Serializable{
private Locale locale;
private static final long serialVersionUID = 2756934361134603857L;
#PostConstruct
public void init() {
FacesContext.getCurrentInstance().getApplication().getDefaultLocale();
}
public Locale getLocale() {
return locale;
}
public String getLanguage() {
return locale.getLanguage();
}
public void setLanguage(String language) {
locale = new Locale(language);
FacesContext.getCurrentInstance().getViewRoot().setLocale(locale);
}}
Now when I run the project, I got only:
http://localhost:8080/Wirote/index
also the page is stuck, so I can't navigate to another using links in the index.xtml
alternative I add the following to the index.xhtml:
<f:metadata>
<f:viewParam name="locale" value="#{localeManger.language}"/>
</f:metadata>
pretty-config.xml
<url-mapping id="index">
<pattern value="/#{locale}/index"/>
<view-id value="/index.xhtml"/>
</url-mapping>
Now when I run the project, I get the following:
http://localhost:8080/Wirote/de/index
But when I try to change the language, by clicking on English or Arabic language switcher, it doesn't work correctly, the content of the page change, but the address page is not. But if I change it manually to
http://localhost:8080/Wirote/en/index or
http://localhost:8080/Wirote/ar/index
it display the correct content in Arabic and English, but I need the address to be changed automatically not manually.
How can I get the correct address related to current locale?
I'm not sure if this helps you, since you're attempting to do this using PrettyFaces, but Rewrite (the library PrettyFaces is based on) has a dedicated feature for internationalizing URLs:
https://github.com/ocpsoft/rewrite/blob/master/documentation/src/main/asciidoc/configuration/i18n.asciidoc
This might help you and if you are using PrettyFaces based on Rewrite, then you already have access to this feature.
I have been using pretty faces for urls without issue until i started using mapped URLs feature (Section 3.6). http://ocpsoft.org/docs/prettyfaces/3.3.3/en-US/html/Configuration.html#config.actions
I'm getting the output and the actions i want to happen working fine. The problem though is i can't get to to any of my assets under assets folder. i'm getting a 404. The weird thing is that for every other config there hasn't been a problem. And isn't a problem when I run them together. The app is using a template so the links to css's, js's, etc.. are exactly the same. When I go to a plain ol' (e.g. /home) mapping they work fine, go to the other page with the action, they don't work. The template rendering does though. In fact the parameter injection and the action work as well.
PRETTY CONFIG:
<url-mapping id="home"> <!-- assets work -->
<pattern value="/home" />
<view-id value="/home.jsf" />
</url-mapping>
<url-mapping id="validate-token"> <!-- assets don't work -->
<pattern value="/validate-token/type/#{id:validateByTokenController.tokenType}/token/#{validateByTokenController.token}" />
<view-id value="/validate-token.jsf" />
<action>#{validateByTokenController.init}</action>
</url-mapping>
BEAN:
#RequestScoped
#Named
public class ValidateByTokenController {
private String tokenType;
private String token;
public void init() {
token = "J" + token;
tokenType = "J" + tokenType;
}
XHTML SAMPLE CSS LINKS: (same links for "home" config, but not for "validate-token")
<link rel="stylesheet"
href="assets/plugins/bootstrap/css/bootstrap.min.css" />
<link rel="stylesheet" href="assets/css/style.css" />
Thanks in advance.
The problem is that you are using relative URLs for your CSS files.
With an URL like /validate-token/type/foo/token/bar the browser thinks that /validate-token/type/foo/token/ is the current directory. So he tries to load the CSS file from /validate-token/type/foo/token/assets/css/style.css.
Try using absolute URLs for your CSS instead:
<link rel="stylesheet" href="#{request.contextPath}/assets/css/style.css" />
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> -->
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}" />
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