Problem with link generation with language parameter.

Splash Forums PrettyFaces Users Problem with link generation with language parameter.

This topic contains 6 replies, has 2 voices, and was last updated by  Marcin Kapusta 2 years, 12 months ago.

Viewing 7 posts - 1 through 7 (of 7 total)
  • Author
    Posts
  • #24457

    Marcin Kapusta
    Participant

    I have difficulty to make this simple example to work.

    My configuration: JSF 2.1.24, Pretty Faces 3.3.3, Tomcat 7.0.42

    I have such mapping:

    
    <url-mapping id="base">
      <pattern value="/#{ /pl|en|de/ locale.language }" />
    </url-mapping>
    
    <url-mapping id="home" parentId="base">
      <pattern value="/" />
      <view-id value="/views/home.xhtml" />
    </url-mapping>

    In my /views/home.xhtml i have such link

    <h:link outcome="pretty:home" value="Home" />

    And my bean is like that LocaleBean.java:

    
    import javax.annotation.PostConstruct;
    import javax.faces.bean.ManagedBean;
    import javax.faces.bean.RequestScoped;
    
    @ManagedBean(name = "locale")
    @RequestScoped
    public class LocaleBean {
    
    	public static final String DEFAULT_LANGUAGE = "pl";
    
    	private String language;
    
    	@PostConstruct
    	public void init() {
    		System.out.println("init");
    		this.language = DEFAULT_LANGUAGE;
    	}
    
    	public String getLanguage() {
    		System.out.println("getLanguage: " + language);
    		return language;
    	}
    
    	public void setLanguage(String language) {
    		System.out.println("setLanguage: " + language);
    		this.language = language;
    	}
    
    }
    

    When I enter to mysite.com/web-base/en/ the link that is generated on home view is like that:

    
    <a href="/web-base/views/home.xhtml?com.ocpsoft.mappingId=home">Home</a>
    

    The question is. Why the link is not like that?:

    
    <a href="/web-base/en/">Home</a>
    

    The web-base is the context path and in my console I have such output:

    init
    setLanguage: en

    What Am I doing wrong? Can somebody help?

    #24466

    This is very strange. Could you perhaps try the latest version of PrettyFaces? See http://ocpsoft.org/prettyfaces/ for instructions. If this is still failing, would you mind uploading a sample application that we can use to test your problem?

    I’m sorry you are having trouble.

    Thanks!
    ~Lincoln

    #24474

    Marcin Kapusta
    Participant

    Lincoln,

    I get it to working by write link in this way:

    <h:link outcome="pretty:home" includeViewParams="true">

    The includeViewParams was needed.

    Also I had f:viewParam defined in the master template and this is bad idea. I moved it to my view and insert it under f:metadata, and now it works. There is still problem with language parameter that exists on every page and I can’t put it in the master template. I have to manualy provide the value of the parameter like this:

    <h:link outcome="pretty:home" includeViewParams="true">
      <f:param name="language" value="#{localeBean.language}" />
    </h:link>

    Regards,
    Martin

    #24480

    Strange. Maybe this is something that changed in one of the most recent versions of JSF. Do you think you could upload an example so that I could play with it on some servers?

    Glad you got a workaround, though!

    Thanks!
    ~Lincoln

    #24484

    Marcin Kapusta
    Participant

    Lincoln

    I attached my project from eclipse. It use maven 3 pom.xml
    I running it on Tomcat 7.0.42, Java 7, Windows 7 32 bit

    When You run it, type in your browser http://localhost:8080/web-base/pl/

    Take a look at 3 links “About Us”.

    UPDATE:
    After reading this article: http://java.dzone.com/articles/bookmarkability-jsf-2 which help me a lot about understandings some concepts about url and parameters in JSF 2 I noticed that when I add to the view/about-us.xhtml file such lines after the line: template=”/WEB-INF/templates/base.xhtml”>

    <f:metadata>
      <f:viewParam name="language" value="#{locale.language}" />
    </f:metadata>

    Then on the page http://localhost:8080/web-base/pl/ second and third link (About Us) is generated correctly.
    It seems that in view I have to define metadata with viewParam’s when I want to use pretty faces to generate link to this view with parameters. Also note that the includeViewParams=”true” must be provided because default value of this attribute is false.


    Regards
    Marcin

    Attachments:
    1. web-base.zip
    #24534

    Hi Marcin,

    Thanks for posting your example – apologies that I did not see it sooner. Our forums had a problem sending notification emails for a few days, and I’ve been on Vacation, so I was not monitoring as aggressively 🙂

    Do I understand correctly that you’ve found the solution for your issue? If not, just let me know and I’ll take a deeper look.

    Thanks!
    ~Lincoln

    #24541

    Marcin Kapusta
    Participant

    Yes I found a solution and after that I switch to the Rewrite. Thankt for reply.


    Regards
    Marcin

Viewing 7 posts - 1 through 7 (of 7 total)

You must be logged in to reply to this topic.

Comments are closed.