How to intercept JSF view resolving in order to replace the outcome page?

AlexSC

A certain managed bean has an action method that returns "/private/myview.jsf". However, without changing this code, I want to perform some checks and eventually render the view "/private/other/myview.jsf".

So, in summary, I want to translate "/private/myview.jsf" to "/private/other/myview.jsf" somewhere after the method return and before the actual view rendering.

How to achieve that?


Environment:

  1. Eclipse Luna
  2. Java 1.7
  3. JSF 2

Current condition

Web application fully operational but not developed to support accessibility features.

Requirement for the next version

To be an application good enough to be used by blind people who uses screen readers.

Details

After some study, we reached the conclusion that we will have to have an accessible version of each page we have nowadays.

We will design such accessible version of each page, the matter is when to show the not accessible version and when to show the accessible version.

We decided that the application will turn to accessible mode (that will not be the default state) when the user clicks a certain link in the top of the page. In accessible mode, only accessible versions of pages are rendered.

We don't want to review all the application, what we want is to intercept some phase of JSF and replace the outcome that should be rendered. For instance, consider the follow:

  1. A certain page has a link or a button to another one, let's say "mysettings.xhtml";
  2. When in accessible mode, we would like to tell to JSF to not render "mysettings.xhtml", instead "mysettings_ac.xhtml" or "accessible/mysettings.xhtml" should be rendered;
  3. Both pages will interact with the very same managed beans and will provide the very same features, but one will be good to the ones who can see and the other will be designed to be comfortable to screen reader users.

Thanks in advance!

BalusC

So, in summary, I want to translate "/private/myview.jsf" to "/private/other/myview.jsf" somewhere after the method return and before the actual view rendering.

If you're already on JSF 2.2, you can do this by providing a custom ResourceHandeler wherein you return the desired view resource in createViewResource() based on some condition.

public class YourResourceHandler extends ResourceHandlerWrapper {

    private ResourceHandler wrapped;

    public YourResourceHandler(ResourceHandler wrapped) {
        this.wrapped = wrapped;
    }

    @Override
    public ViewResource createViewResource(FacesContext context, final String name) {
        if (someCondition) {
            name = name.replace("/private/", "/private/other/");
        }

        return super.createViewResource(context, name);
    }

    @Override
    public ResourceHandler getWrapped() {
        return wrapped;
    }

}

Which is registered in faces-config.xml as below:

<application>
    <resource-handler>com.example.YourResourceHandler</resource-handler>
</application>

Or if you're not on JSF 2.2 yet, then use ResourceResolver instead.

public class YourResourceResolver extends ResourceResolver {

    private ResourceResolver parent;

    public YourResourceResolver(ResourceResolver parent) {
        this.parent = parent;
    }

    @Override
    public URL resolveUrl(String path) {
        if (someCondition) {
            path = path.replace("/private/", "/private/other/");
        }

        return parent.resolveUrl(path);
    }

}

Which is registered in web.xml as below:

<context-param>
    <param-name>javax.faces.FACELETS_RESOURCE_RESOLVER</param-name>
    <param-value>com.example.YourResourceResolver</param-value>
</context-param>

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

How to specify the order of evaluation in JSF?

分類Dev

How to intercept and replace Quartz.NET's default logging at runtime?

分類Dev

How to use JSF Expression Language in predesigned plain HTML page

分類Dev

How to save the outcome of collection.find into Array

分類Dev

how to select records depending on outcome of 2 queries

分類Dev

How to change order of the tabs in Sales Orders page

分類Dev

How can I reverse the page order in Gatsby?

分類Dev

Replace spaces with nonbreakable spaces in JSF

分類Dev

How to set a custom page to replace the default Firefox New Tab page

分類Dev

How to add different items to a Page View Flutter

分類Dev

How to render a web page in a portlet view?

分類Dev

How to disable ctrl+v (paste) function in a JSF page ?. I am using primefaces component.

分類Dev

How to Intercept Property Enumeration of an Object?

分類Dev

How to Intercept incoming call in Mule

分類Dev

Order rows that replace the id

分類Dev

How to use the constant outcome of a random formed function in python

分類Dev

print(type(a)) vs type(a)- How to see the outcome of a code

分類Dev

How would a word or long word addressable memory system change the outcome?

分類Dev

How to replace tab and new line from a view in CodeIgniter?

分類Dev

WooCommerce - How to add column to product list in admin order page

分類Dev

Oracle and React: How to keep order for next time page loads

分類Dev

How do you search and replace page breaks in Libre Office?

分類Dev

Replace IBOutlet view in code

分類Dev

Fixed position of buttons in dialog of JSF page

分類Dev

Javascript error in XHTML page in JSF 2

分類Dev

CodeIgniter - How to dynamically select a menu item in a partial view page for menu?

分類Dev

Unexpected outcome of a="$@"

分類Dev

Replace character order MySQL field

分類Dev

Webflux, How to intercept a request and add a new header

Related 関連記事

  1. 1

    How to specify the order of evaluation in JSF?

  2. 2

    How to intercept and replace Quartz.NET's default logging at runtime?

  3. 3

    How to use JSF Expression Language in predesigned plain HTML page

  4. 4

    How to save the outcome of collection.find into Array

  5. 5

    how to select records depending on outcome of 2 queries

  6. 6

    How to change order of the tabs in Sales Orders page

  7. 7

    How can I reverse the page order in Gatsby?

  8. 8

    Replace spaces with nonbreakable spaces in JSF

  9. 9

    How to set a custom page to replace the default Firefox New Tab page

  10. 10

    How to add different items to a Page View Flutter

  11. 11

    How to render a web page in a portlet view?

  12. 12

    How to disable ctrl+v (paste) function in a JSF page ?. I am using primefaces component.

  13. 13

    How to Intercept Property Enumeration of an Object?

  14. 14

    How to Intercept incoming call in Mule

  15. 15

    Order rows that replace the id

  16. 16

    How to use the constant outcome of a random formed function in python

  17. 17

    print(type(a)) vs type(a)- How to see the outcome of a code

  18. 18

    How would a word or long word addressable memory system change the outcome?

  19. 19

    How to replace tab and new line from a view in CodeIgniter?

  20. 20

    WooCommerce - How to add column to product list in admin order page

  21. 21

    Oracle and React: How to keep order for next time page loads

  22. 22

    How do you search and replace page breaks in Libre Office?

  23. 23

    Replace IBOutlet view in code

  24. 24

    Fixed position of buttons in dialog of JSF page

  25. 25

    Javascript error in XHTML page in JSF 2

  26. 26

    CodeIgniter - How to dynamically select a menu item in a partial view page for menu?

  27. 27

    Unexpected outcome of a="$@"

  28. 28

    Replace character order MySQL field

  29. 29

    Webflux, How to intercept a request and add a new header

ホットタグ

アーカイブ