Rewrite not working

Splash Forums Rewrite Users Rewrite not working

This topic contains 13 replies, has 3 voices, and was last updated by  Lincoln Baxter III 2 years, 9 months ago.

Viewing 14 posts - 1 through 14 (of 14 total)
  • Author
    Posts
  • #25199

    rahulsoni
    Participant

    Hi all,

    I’m a newbie to this Url Rewriting beating his head hard to get this thing work.I’ve created a SampleConfigurationProvider class at location
    src/Main/SampleConfigurationProvider.java.

    I’ve also created ConfigurationProvider file at location
    WebContent/WEB-INF/classes/META-INF/org.ocpsoft.rewrite.config.ConfigurationProvider.

    My complete project structure is:

    MyProject
    JAX-WS Web Services
    Deployment Descriptor:MyProject
    java Resources
    src
    Main
    SampleConfigurationProvider.java
    JavaScript Resources
    Build
    Webcontent
    META-INF
    org.ocpsoft.rewrite.config.ConfigurationProviders
    NewPages
    newfile.html
    WEB-INF
    lib
    classes
    META-INF
    index.jsp

    What I’m trying to do is that the index.jsp file contains a LINK to NewPages/newfile.html.
    So I want that when the link is clicked the URL shouldn’t be :
    http://localhost:8080/MyProject/NewPages/newfile.html

    Instead it should be like:
    http://localhost:8080/MyProject/RewriteWork/MyFile

    The rule that I’ve added is:
    .addRule()
    .when(Direction.isInbound().and(Path.matches(“/NewPage/newfile.html”)))
    .perform(Redirect.temporary(“/RewriteWork/MyFile”));

    I’ve added a System.out.println(“Conf file loaded.”) msg in the Configuration file to test whether this file being loaded or not but the MSG isn’t getting displayed at the console.
    So is it that my configuration files aren’t placed properly ?

    I’ve attached my project.

    Plz help me out with this problem. My project has got stuck here.

    Thanks in advance 🙂

    • This topic was modified 2 years, 9 months ago by  rahulsoni.
    Attachments:
    1. MyProject1.zip
    #25201

    Looks like you placed the ServerLoader file in the wrong location. Try

    WebContent/WEB-INF/classes/META-INF/services/org.ocpsoft.rewrite.config.ConfigurationProvider

    Instead of:

    WebContent/WEB-INF/classes/META-INF/org.ocpsoft.rewrite.config.ConfigurationProvider

    #25204

    rahulsoni
    Participant

    Christian Sir,
    Thanks a Tonnnnnn,
    The Configuration file is now loading and the URL rewriting is now working. But I’m still not getting the desired URL effect.

    What I want is that when the Link in index.jsp is clicked the URL:
    NewFile

    The URL should be:
    http://localhost:8080/MyProject/11/article

    But the URL that is being displayed is:
    http://localhost:8080/MyProject/NewPages/other.html

    My Rule is:
    .addRule()
    .when(Direction.isInbound().and(Path.matches(“/11/article”))) .perform(Redirect.temporary(“/MyProject/NewPages/other.html”));

    What I want is that the URL should be the same as I’ve provided in the href of the anchor but the request should indirectly be forwarded to the desired location..

    How can I achieve this…

    Thanks in advance..

    • This reply was modified 2 years, 9 months ago by  rahulsoni.
    • This reply was modified 2 years, 9 months ago by  rahulsoni.
    #25207

    Note, if you are using Rewrite 2.0.8, you can simply add the following annotation to your configuration provider, instead of using the service registration:

    @RewriteConfiguration
    public class MyConfigurationProvider ... {
    }

    Also, I believe you may be misunderstood the purpose of Redirect 🙂 That will actually issue an HTTP redirect that sends the browser to a new location. I believe you want Forward, instead… but there is actually a simpler way to do this, using Join: Read the docs on ‘url mapping’ for an explanation.

    #25208

    rahulsoni
    Participant

    Thanks Lincoln Sir,
    I tried with Forward and Join, its working.

    Can u tell me the link where I can find details and explanations to all these Methods like JOIN,FORWARD,REDIRECT, etc..

    Thanks in advance

    #25209

    Forwards and redirects are terms not specific to Rewrite itself. Join is a concept of Rewrite. You could have a look at the Rewrite documentation. But it’s currently not complete as we don’t find much time to work on it:

    http://ocpsoft.org/rewrite/docs/

    #25210

    Actually, even though our documentation on the website is not yet complete (working on it! sorry!) most of the JavaDocs are very comprehensive. So if you download the sources, or the JavaDocs, you should be able to see what each configuration element does!

    http://search.maven.org/remotecontent?filepath=org/ocpsoft/rewrite/rewrite-servlet/2.0.8.Final/rewrite-servlet-2.0.8.Final-sources.jar

    ~Lincoln

    #25211

    @lincoln: BTW: We should think about publishing the javadocs somewhere. I think this would useful for our users.

    #25220

    rahulsoni
    Participant

    Hello Lincoln Sir and Christian Sir,

    The URL Rewriting is doing fine now. But now there’s another problem.
    I’ve send my URL to SERVLET and after doing the processing the Servlet calls RequestDispatcher to FORWARD the request to a JSP page.

    The JSP page is also getting called but the STYLESHEET and JAVASCRIPT files linked to that JSP page are not loading.

    How can I fix this issue ??

    If the styling is included inside the <style> tags then its working but the external .css file isn’t being loaded.

    My RULE: .addRule(Join.path(“/{var1}/content/.*”).to(“/myservlet”))

    Servlet:
    RequestDispatcher dis = request.getRequestDispatcher(“/mypage.jsp”);
    dis.forward(request,response);

    ISSUE: mypage.jsp is loading without its CSS and Javascript.

    Is it bcoz the stylesheets and scripts are loaded when the page is loaded into the browser while here the page is being called as a result of a forward request.

    On doing INSPECT ELEMENT on the browser I saw this msg:
    The stylesheet http://localhost:8080/OCPRewrite2.0.8/21/10/tutorial/java/style.css was not loaded because its MIME type, “text/html”, is not “text/css”.

    Thanks in advance

    #25221

    Rewrite should not do anything to change the Content-Type header unless you specify otherwise, so I am guessing that the container you are using is not actually setting the content type, and that you may need to use a mime-mapping in your web.xml file.

    <mime-mapping>
            <extension>css</extension>
            <mime-type>text/css</mime-type>
        </mime-mapping>
    #25222

    rahulsoni
    Participant

    Sir,
    Tried adding mime-mapping to my web.xml but its still not working.

    When I type the URL to access the stylesheet as:
    http://localhost:8080/OCPRewrite2.0.8/NewPages/style.css

    I got this msg in the browser upon Element inspection:

    The character encoding of the plain text document was not declared. The document will render with garbled text in some browser configurations if the document contains characters from outside the US-ASCII range. The character encoding of the file needs to be declared in the transfer protocol or file needs to use a byte order mark as an encoding signature. @ http://localhost:8080/OCPRewrite2.0.8/NewPages/style.css

    #25227

    But the CSS file loads correctly? So it is send to you with a 200 OK response code? Where do you see this warning? If you load the file directly you should see the source code of the CSS file.

    #25235

    rahulsoni
    Participant

    Hello Lincoln Sir and Christian Sir,

    I got the sol’n to my prob. i was using Relative URL to call my CSS and JS files. But when request is Forwarded from Servlet to JSP, ABSOLUTE URL addressing is required. So I changed my URL to absolute and its working ..

    Thanks 🙂

    #25239

    Glad you got it working!

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

You must be logged in to reply to this topic.

Comments are closed.