4

I have a URL in this way http:/myDomain/myWAR/myServletReceiver

I don't want end User to know about my WAR file name nor about the Servlet Receiver name. So, I thought I will personalize this URL into something like http:/myDomain/MyAccount .

I have added below piece of code for achieving this.

Created my own package. Code below.

package myOwnPackage
import javax.el.ExpressionFactory;
import javax.el.ValueExpression;
import javax.faces.application.ViewHandler;
import javax.faces.context.FacesContext;
import com.sun.facelets.FaceletViewHandler;


public class DynamicViewHandler extends FaceletViewHandler{

    public DynamicViewHandler(ViewHandler parent) {

        super(parent);
        System.out.println("Entered into DynamicViewHandler");
    }

    public String getActionURL(FacesContext context, String viewId) {
        System.out.println("Inside getActionURL");

        ExpressionFactory expressionFactory = context.getApplication().getExpressionFactory();
        ValueExpression valueExpression = expressionFactory.createValueExpression(context.getELContext(), viewId, String.class);
        String result = (String) valueExpression.getValue(context.getELContext());

        System.out.println("Value of Result is:" +result);

            //I am in the beginning steps and just want to print the value of "result" for each response
        return result;
    }

}

I registered this in faces-config.xml

<application>
<view-handler>myOwnPackage.FaceletViewHandler</view-handler>
<state-manager>org.jboss.portletbridge.application.PortletStateManager</state-manager>
</application>

When I hit my application, the welcome page that my Servlet redirects to is /jsp/html/index.xhtml.

So, in the logs I get below values printed.

Entered into DynamicViewHandler

Value of Result is:/jsp/html/index.xhtml

There are other links in index.jsf page. When I click on other links I get below error message on my Browser(instead of going to /jsp/html/secondPage.jsf)

http:/myDomain/jsp/html/index.html not found (404) error.

Am definitely missing things in my DynamicViewHandler, faces-config.xml and may be more.

What else am I missing here?

Also, I would like to map jsp/html/secondPage.jsf to /MyAccount

Vikas V
  • 3,176
  • 2
  • 37
  • 60
  • Your tagging is confusing. Are you using JSF 1.2 or JSF 2.x? The code suggests JSF 1.2 by the way, so you should absolutely not add `jsf-2` tag, but instead the `jsf` tag. – BalusC Dec 20 '12 at 14:34
  • My apologies BalusC. Am using JSF 1.2. Am not sure of creating `ViewHandler` in JSF2.0 Thought it will be the same in JSF2.x So, tagged that also. I have edited now. Thanks again :) – Vikas V Dec 21 '12 at 03:35
  • If I write my own `ViewHandler` and override the `getActionURL`, I should return a String which is the Friendly URL that I want. Lets say I am returning `MyAccounts` as friendly URL, then it will try to find a mapping for `MyAccounts`(else it will throw 404 error). For this, if I make an entry in `web.xml` and write a Servlet mapping , what should be the code in the `Servlet` to display the actual contents of the page but still show the friendly URL `MyAccounts` on the URL? – Vikas V Dec 26 '12 at 03:22
  • @Vikas you need to change your URL,display some different URL, hide original file path? – Jubin Patel Dec 26 '12 at 11:40
  • Ya jubin. I need to change the URL, display some other (my own friendly URL) and hide the original file path – Vikas V Dec 26 '12 at 11:46

2 Answers2

1

For display different URL instead of your original URL.

Method 1 :

you have a file name pretty-config.xml.

With this file you can show a different url other than real patch.

Example:

With these two simple lines of configuration, the user sees: pattern="/mySite/" in the browser URL and in the output HTML, but the server is actually rendering the resource: /faces/sites/mySite.jsf the actual location of the page on the server.

You can find information here http://ocpsoft.org/prettyfaces/

Method 2: As BalusC suggestion on SO

PrettyFaces is for JSF only.You need Rewrite (which is still beta; these days Tuckey's URLRewriteFilter is the best available)

Jubin Patel
  • 1,959
  • 19
  • 38
0

Option 1:- If you are using httd service, you can use mod_rewrite option to change the url pattern http://httpd.apache.org/docs/current/mod/mod_rewrite.html

Option 2:-

As jubinPatel said UrlRewriteFilter is the best option to change the url pattern.

http://tuckey.org/urlrewrite/

Anantharaman
  • 67
  • 1
  • 6