Retaing View Parameters with Rewrite

Splash Forums Rewrite Users Retaing View Parameters with Rewrite

This topic contains 10 replies, has 3 voices, and was last updated by  naska.om 2 years, 5 months ago.

Viewing 11 posts - 1 through 11 (of 11 total)
  • Author
    Posts
  • #25744

    naska.om
    Participant

    I have used the following approach which @Christian Kaltepoth introduced to me, for my multilanguage JSF application and now I noticed that URLs with view-parameters are processed just as other ones. I wonder where should I state that view-parameters should remain in url in the following code:

    @Inject
        private LocaleBean localeBean;
        
        private static Map<String, Properties> langToViews = new HashMap<>();
        private static Map<String, Properties> langToPaths = new HashMap<>();
        
        private String getView(String lang, String path){
            Properties viewMap = getViewMap(lang);
            String view = viewMap.getProperty(path);
            return String.format("/%s.xhtml", view);
        }
        private Properties getViewMap(String lang){
            Properties props = langToViews.get(lang);
            if(props == null){
                props = new Properties();
                try{
                    props.load(getClass().getResourceAsStream(String.format("text_%s.properties",lang)));
                    langToViews.put(lang, props);
                } catch(IOException e){
                    throw new RuntimeException(e);
                }
            }
            return props;
        }
        private String getPath(String lang, String view){
            Properties pathMap = getPathMap(lang);
            String path = pathMap.getProperty(view);
            return String.format("/%s/%s",lang, path);
        }
        private Properties getPathMap(String lang){
            Properties props = langToPaths.get(lang);
            if(props == null){
                props = new Properties();
                try{
                    props.load(getClass().getResourceAsStream(String.format("text_%s.properties",lang)));
                    langToPaths.put(lang, props);
                }catch (IOException e){
                    throw new RuntimeException(e);
                }
            }
            return props;
        }    
       @Override
       public Configuration getConfiguration(final ServletContext context)
       {      
           
         return ConfigurationBuilder.begin()
                 
                 .addRule()
                 .when(Direction.isInbound().and(Path.matches("/{lang}/{path}")))
                 .perform(new HttpOperation(){
                     @Override
                     public void performHttp(HttpServletRewrite event, EvaluationContext context){                     
                         String lang = (String) Evaluation.property("lang").retrieve(event, context);
                         String path = (String) Evaluation.property("path").retrieve(event, context);
                         String view = getView(lang, path);
                         localeBean.setLang(lang);
                         Forward.to(view).perform(event, context);                     
                     }
                 })             
                 .where("lang").matches("en|ar|ku")             
                 
                 .addRule()
                 .when(Direction.isOutbound().and(Path.matches("/{view}.xhtml")))
                 .perform(new HttpOperation(){
                     @Override
                     public void performHttp(HttpServletRewrite event, EvaluationContext context){
                         String lang = localeBean.getLocale().getLanguage();
                         String view = (String) Evaluation.property("view").retrieve(event, context);
                         String path = getPath(lang, view);
                         Substitute.with(path).perform(event, context);                     
                     }
                 });
    #25747

    I’m not sure exactly what you mean by “I noticed that URLs with view-parameters are processed just as other ones”. Could you please explain this and give examples?

    Thank you.
    ~Lincoln

    #25758

    naska.om
    Participant

    I’m sorry Lincoln. My explanation was not clear enough. I clarify by the following example:
    Articles.xhtml —> ArticlesApp/en/Articles
    View_Article.xhtml?faces-redirect=true&includeViewParams=true —-> ArticelsApp/en/View_Article
    It should be like this:
    ArticlesApp/en/View_Article?id=1

    Thank you for you reply.

    #25775

    I have to admit that I’m still not sure what you are asking. Where does id=1 come from in your example? I don’t see this parameter referenced in any of your code.

    #25778

    naska.om
    Participant

    It’s an <h:commendLink> with includeViewParams=true.
    It’s like the following:

    <h:commandLink id="viewArticleDetails" action="#{ArticleController.viewArticleByID()}" styleClass="view-details">
                                                                <h:outputText value="View Details"/>
                                                                <f:setPropertyActionListener target="#{ArticleController.id}" value="#{item.ArticleId}"/>
                                                                <f:setPropertyActionListener target="#{ArticleLclController.id}" value="#{item.ArticleId}"/>
                                                            </h:commandLink>

    In ArticleController.viewArticleByID() method I have:
    return "View_Article?faces-redirect=true&includeViewParams=true";

    This returns the results I posted in my previous post. I want my view pages to be bookmarkable by including view_params like ArticlesApp/en/View_Article?id=1 but they become URLs like ArticlesApp/en/View_Article and so you cannot copy this URL and paste it in the address bar and view an Article.

    I hope I’ve been clear.
    Please let me know if I am not.

    • This reply was modified 2 years, 6 months ago by  naska.om.
    #25783

    This looks really complicated. If I understand your code correctly, you are basically doing a postback which will set the id in the target bean, invoke an action method which redirects the user to the details view using the ID previously written to the bean.

    Why don’t you use h:link instead? This way you will get a plain HTML link and don’t have to do a postback to the server at all:

    <h:link outcome="View_Article">
      <h:outputText value="View Details"/>
      <f:param name="id" value="#{item.ArticleId}" />
    </h:link>
    

    Much cleaner. 🙂

    #25784

    naska.om
    Participant

    I couldn’t manage to make <h:link> work. But I wonder why wouldn’t <h:commandLink> work? Why a postback call to the server make includeViewParams=true not work?

    #25785

    I’ve no idea why includeViewParams doesn’t work. Could you provide a minimal Maven sample project that reproduces this? Then I could have a deeper look at this issue.

    However, I still think that h:link is a way better choice for your usecase. What you are currently trying to do is sending a postback just to get redirect to another URL. This doesn’t make sense to me. Why not simply render a link pointing to the page you want to go to?

    #25857

    naska.om
    Participant

    After a long workaround, I used get requests instead of post requests, still includeViewParams and f:param didn’t work. I shut down the ocpsoft rewrite filter in web.xml and excluded primefaces and it still didn’t work. I tested the solution on pages which rewrite didn’t pass through them and it worked. Then I upgraded to glassfish 4. A new problem raised and the previous one solved. Rewrite doesn’t work, although I have all jars in my lib folder and it used to work in glassfish 3.1.2 but without rewrite now, the links work alright. Any ideas? I would like to have both of them 🙂

    #25858

    As Christian said, could you please upload a sample maven project that reproduces this issue? We need a closer look in order to be able to help more. Thanks!

    #25860

    naska.om
    Participant

    I’ve never used maven. I’ve built my project with ant. May I upload a simplified ant project?

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

You must be logged in to reply to this topic.

Comments are closed.