0

I am using Angularjs, Java for my application. Initially I am using tomcat to run my application. when I run my project it will open the URL

http://localhost:8080/projectname

so I configured my base tag and html5 mode as

$locationProvider.html5Mode(true);
$locationProvider.hashPrefix('');
<base href="/projectname/">

with welcome file configured to main.html in web.xml and I added the code

<welcome-file-list>  
    <welcome-file>main.html</welcome-file>  
</welcome-file-list>

<error-page>
    <error-code>404</error-code>
    <location>/</location>
</error-page>

to make ui-router work with html5 mode(mentioned in ui-router issue page).

Then I change my project to google app engine standard java project and when I run my project it open with url

http://localhost:8080

so I changed my base tag to

<base href="/">

with this there no problem in main.html but my routing are not working. When I use $state.go its working. But on refreshing or manually entering the url i'm getting 404 error. My console error is

WARNING: No file found for: /url

I referred many blogs but still confused to configure


I have configured spring in web.xml file

<servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

It seems that it is mapping all the url request to spring, is there any way to solve this?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Rishi0405
  • 354
  • 1
  • 11
  • Possible duplicate of [Tomcat 8 URL Rewrite](https://stackoverflow.com/questions/28767585/tomcat-8-url-rewrite). – georgeawg Aug 20 '17 at 18:14
  • Possible duplicate of [AngularJS: can't get html5 mode urls](https://stackoverflow.com/questions/17444072/angularjs-cant-get-html5-mode-urls-with-ui-route-state) – georgeawg Aug 20 '17 at 18:21
  • @georgeawg I'm using Java EE rewrite, it worked when I used Tomcat but the same JavaEE rewrite is not working when I switched my project to google app engine. – Rishi0405 Aug 21 '17 at 07:21
  • @georgeawg, I found [this](https://stackoverflow.com/questions/41783634/how-do-i-configure-java-ee-running-on-gae-to-work-with-angular-ui-router-in-html) but there is no answer for that post. – Rishi0405 Aug 21 '17 at 07:48
  • @Rishi were you able to resolve this issue? If so it is recommended to post your solution as the answer to this question to better help the community. If not you may way to try setting the requireBase flag to be false to opt-out of the base tag as per the [feature added in v1.6.6](https://github.com/angular/angular.js/commit/dc3de7fb7a14c38b5c3dc7decfafb0b51d422dd1). – Jordan Sep 20 '17 at 17:34
  • @Jordan I have tried this code $locationProvider.html5Mode({ enabled: true, requireBase: false }); and removed the base tag. But it is not working. Still getting the 404 error on page route – Rishi0405 Sep 25 '17 at 17:58
  • @Rishi is this only an issue in the local development server? Have you tried [deploying your application](https://cloud.google.com/appengine/docs/standard/java/tools/uploadinganapp) and testing via the appspot.com url? – Jordan Sep 26 '17 at 13:39

1 Answers1

1

Problem is with refresh the page, if you will open the base and click on any link it will work, you need to write the code for re-write URL

For IIS running application we need to add:

<system.webServer>
    <rewrite>
     <rules> 
      <rule name="Main Rule" stopProcessing="true">
        <match url=".*" />
        <conditions logicalGrouping="MatchAll">
          <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />                                 
          <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
        </conditions>
        <action type="Rewrite" url="/" />
      </rule>
    </rules>
    </rewrite>
</system.webServer>

See detail for IIS configuration

Sorry, I never worked with tomcat

Community
  • 1
  • 1
Ali Adravi
  • 21,707
  • 9
  • 87
  • 85