|
INTERVIEW QUESTIONS
J2EE
JSF
DETAILS
Question: How to mask actual URL to the JSF page?
Answer: You'll need to implement your own version of javax.faces.ViewHandler which does what you need. Then, you register your own view handler in faces-config.xml.
Here's a simple abstract ViewHandler you can extend and then implement the 3 abstract methods for. The abstract methods you override here are where you'll do your conversions to/from URI to physical paths on the file system. This information is just passed right along to the default ViewHandler for JSF to deal with in the usual way. For example, you could override these methods to add and remove the file extension of an incoming view id (like in your example), for extension-less view URIs.
import java.io.IOException; import java.util.Locale; import javax.faces.FacesException; import javax.faces.application.ViewHandler; import javax.faces.component.UIViewRoot; import javax.faces.context.FacesContext; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; /** * A facade view handler which maps URIs into actual physical views that the * underlying implementation can deal with regularly. * Therefore, all internal references to view ids, for example in faces-config, * will use the path to the physical files. Everything publicized, however, will * see a "converted" / facade url. */ public abstract class SimpleConverterViewHandler extends ViewHandler { private static final Log LOG = LogFactory.getLog(SimpleConverterViewHandler.class); private final ViewHandler base; public SimpleConverterViewHandler(ViewHandler base) { this.base = base; } /** * Distinguishes a URI from a physical file view. * Tests if a view id is in the expected format -- the format corresponding * to the physical file views, as opposed to the URIs. * This test is necessary because JSF takes the view ID from the * faces-config navigation, and calls renderView() on it, etc. */ public abstract boolean isViewFormat(FacesContext context, String viewId); /** * Convert a private file path (view id) into a public URI. */ public abstract String convertViewToURI(FacesContext context, String viewId); /** * Convert a public URI into a private file path (view id) * note: uri always starts with "/"; */ public abstract String convertURIToView(FacesContext context, String uri); public String getActionURL(FacesContext context, String viewId) { // NOTE: this is used for FORM actions. String newViewId = convertViewToURI(context, viewId); LOG.debug("getViewIdPath: " + viewId + "->" + newViewId); return base.getActionURL(context, newViewId); } private String doConvertURIToView(FacesContext context, String requestURI) { if (isViewFormat(context, requestURI)) { return requestURI; } else { return convertURIToView(context, requestURI); } } public void renderView(FacesContext context, UIViewRoot viewToRender) throws IOException, FacesException { if (null == context || null == viewToRender) throw new NullPointerException("null context or view"); String requestURI = viewToRender.getViewId(); String newViewId = doConvertURIToView(context, requestURI); LOG.debug("renderView: " + requestURI + "->" + newViewId); viewToRender.setViewId(newViewId); base.renderView(context, viewToRender); } public UIViewRoot restoreView(FacesContext context, String viewId) { String newViewId = doConvertURIToView(context, viewId); LOG.debug("restoreView: " + viewId + "->" + newViewId); return base.restoreView(context, newViewId); } public Locale calculateLocale(FacesContext arg0) { return base.calculateLocale(arg0); } public String calculateRenderKitId(FacesContext arg0) { return base.calculateRenderKitId(arg0); } public UIViewRoot createView(FacesContext arg0, String arg1) { return base.createView(arg0, arg1); } public String getResourceURL(FacesContext arg0, String arg1) { return base.getResourceURL(arg0, arg1); } public void writeState(FacesContext arg0) throws IOException { base.writeState(arg0); } }
|
|
|
Category |
JSF Interview Questions & Answers -
Exam Mode /
Learning Mode
|
Rating |
(0.3) By 8866 users |
Added on |
9/23/2014 |
Views |
70604 |
Rate it! |
|
|
Question:
How to mask actual URL to the JSF page?
Answer:
You'll need to implement your own version of javax.faces.ViewHandler which does what you need. Then, you register your own view handler in faces-config.xml.
Here's a simple abstract ViewHandler you can extend and then implement the 3 abstract methods for. The abstract methods you override here are where you'll do your conversions to/from URI to physical paths on the file system. This information is just passed right along to the default ViewHandler for JSF to deal with in the usual way. For example, you could override these methods to add and remove the file extension of an incoming view id (like in your example), for extension-less view URIs.
import java.io.IOException; import java.util.Locale; import javax.faces.FacesException; import javax.faces.application.ViewHandler; import javax.faces.component.UIViewRoot; import javax.faces.context.FacesContext; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; /** * A facade view handler which maps URIs into actual physical views that the * underlying implementation can deal with regularly. * Therefore, all internal references to view ids, for example in faces-config, * will use the path to the physical files. Everything publicized, however, will * see a "converted" / facade url. */ public abstract class SimpleConverterViewHandler extends ViewHandler { private static final Log LOG = LogFactory.getLog(SimpleConverterViewHandler.class); private final ViewHandler base; public SimpleConverterViewHandler(ViewHandler base) { this.base = base; } /** * Distinguishes a URI from a physical file view. * Tests if a view id is in the expected format -- the format corresponding * to the physical file views, as opposed to the URIs. * This test is necessary because JSF takes the view ID from the * faces-config navigation, and calls renderView() on it, etc. */ public abstract boolean isViewFormat(FacesContext context, String viewId); /** * Convert a private file path (view id) into a public URI. */ public abstract String convertViewToURI(FacesContext context, String viewId); /** * Convert a public URI into a private file path (view id) * note: uri always starts with "/"; */ public abstract String convertURIToView(FacesContext context, String uri); public String getActionURL(FacesContext context, String viewId) { // NOTE: this is used for FORM actions. String newViewId = convertViewToURI(context, viewId); LOG.debug("getViewIdPath: " + viewId + "->" + newViewId); return base.getActionURL(context, newViewId); } private String doConvertURIToView(FacesContext context, String requestURI) { if (isViewFormat(context, requestURI)) { return requestURI; } else { return convertURIToView(context, requestURI); } } public void renderView(FacesContext context, UIViewRoot viewToRender) throws IOException, FacesException { if (null == context || null == viewToRender) throw new NullPointerException("null context or view"); String requestURI = viewToRender.getViewId(); String newViewId = doConvertURIToView(context, requestURI); LOG.debug("renderView: " + requestURI + "->" + newViewId); viewToRender.setViewId(newViewId); base.renderView(context, viewToRender); } public UIViewRoot restoreView(FacesContext context, String viewId) { String newViewId = doConvertURIToView(context, viewId); LOG.debug("restoreView: " + viewId + "->" + newViewId); return base.restoreView(context, newViewId); } public Locale calculateLocale(FacesContext arg0) { return base.calculateLocale(arg0); } public String calculateRenderKitId(FacesContext arg0) { return base.calculateRenderKitId(arg0); } public UIViewRoot createView(FacesContext arg0, String arg1) { return base.createView(arg0, arg1); } public String getResourceURL(FacesContext arg0, String arg1) { return base.getResourceURL(arg0, arg1); } public void writeState(FacesContext arg0) throws IOException { base.writeState(arg0); } } Source: CoolInterview.com
If you have the better answer, then send it to us. We will display your answer after the approval.
Rules to Post Answers in CoolInterview.com:-
- There should not be any Spelling Mistakes.
- There should not be any Gramatical Errors.
- Answers must not contain any bad words.
- Answers should not be the repeat of same answer, already approved.
- Answer should be complete in itself.
|
|
Related Questions |
View Answer |
|
Is it possible to have more than one Faces Configuration file?
|
View Answer
|
|
22) What is the different between getRequestParameterMap() and getRequestParameterValuesMap()
|
View Answer
|
|
How to show Confirmation Dialog when user Click the Command Link?
|
View Answer
|
|
How to download PDF file with JSF?
|
View Answer
|
|
How to reload the page after ValueChangeListener is invoked?
|
View Answer
|
|
How to implement "Please, Wait..." page?
|
View Answer
|
|
How to terminate the session?
|
View Answer
|
|
How to access web.xml init parameters from jsp page?
|
View Answer
|
|
How to access web.xml init parameters from java code?
|
View Answer
|
|
How to get current page URL from backing bean?
|
View Answer
|
|
How to add context path to URL for outputLink?
|
View Answer
|
|
How to pass a parameter to the JSF application using the URL string?
|
View Answer
|
|
What is JavaServer Faces?
|
View Answer
|
|
What does it mean by rendering of page in JSF?
|
View Answer
|
|
How does JSF depict the MVC (a.k.a Model View Controller) model?
|
View Answer
|
|
How do I configure the configuration file?
|
View Answer
|
|
How to declare the Navigation Rules for JSF?
|
View Answer
|
|
How the components of JSF are rendered? An Example
|
View Answer
|
|
How JSF different from conventional JSP / Servlet Model?
|
View Answer
|
|
What is JSF architecture?
|
View Answer
|
Please Note: We keep on updating better answers to this site. In case you are looking for Jobs, Pls Click Here Vyoms.com - Best Freshers & Experienced Jobs Website.
View All JSF Interview Questions & Answers - Exam Mode /
Learning Mode
|