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:
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:
"mysettings.xhtml"
;"mysettings.xhtml"
, instead "mysettings_ac.xhtml"
or "accessible/mysettings.xhtml"
should be rendered;Thanks in advance!
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]
コメントを追加