1

I am using Spring and AngularJS to build a website. I have my services at localhost:8080/service/*

I would like all URL's that don't start with /service/ to be redirected to index.html so that I can do all the routing in Angular.

So if the person types any url like below Spring sends them to index.html

  localhost:8080/products
  localhost:8080/products/343848
  localhost:8080/contact
  localhost:8080/jdfndf

Can someone please share with me how to do this? I found this post that is someone else basically asking the same question but they never get a clear answer. Configure Spring MVC with AngularJS

I am new to Spring and AngularJS but I can't find any resources explaining this.

Community
  • 1
  • 1
Justin Cross
  • 99
  • 11

1 Answers1

0

Sorry for mis-read your question. :)

As I see from the comments, you have to create a Single page application.

To achieve your desired results, you have to use ng-route, ng-view.

You have to something similar to this:

<head>
    <script src="script.js"></script>
</head> 
<body ng-app="single-page-app">
<div ng-controller="Controller">
<div>
<nav>
     <ul>
     <li><a href="#/">Home</a></li>
    <li><a href="#/product/{id}">View product</a></li>
    </ul> 
 </nav>   
</div>
 <br/>
 <div ng-view>
<!--
 This DIV loads templates depending upon route.
 -->
</div>
</div>
</body>
</html>

The script js should look like:

(function(){
var routerApp = angular.module("CustomRouter", ['ui.router', 'App']);

routerApp.config(function($stateProvider, $urlRouterProvider) {
    $urlRouterProvider.otherwise('/home');

    $stateProvider.state('default', {
        url : '/home',
        templateUrl : 'index.html',
        controller : 'HomeBodyController'
    }).state('product', {
        url : '/product/:productId',
        controller : 'ProductController'
    })
});

})();

This is just an example. Untested one.

Angular JS is capable of routing. Angular Js routing

Please go through this tutorial hope you will get how to do this.

NewUser
  • 3,729
  • 10
  • 57
  • 79
  • Hi NewUser, I follow all your code and mine is set up very similar but I am missing the key idea here still. At the top you say "I assume as you are re directed to home page always", no I am not, that is what I want to do. I just changed mine to use *.Json for my controller, so how do I make my server use index.html no matter what url is typed besides ones that have .Json? – Justin Cross Dec 15 '15 at 02:36
  • @Justin cross Oh! Are you saying you need a view for the json URL? – NewUser Dec 15 '15 at 02:41
  • 1
    No, i want any url that doesn't include .json to run index.html page. The *.json paths work correctly by returning json from my database but I just don't have any idea how to make every other request run index.html – Justin Cross Dec 15 '15 at 02:45
  • Ok. You mean every request other than *.json should go to index. Html. Am I correct? – NewUser Dec 15 '15 at 02:48
  • you are correct, say i type http://localhost:8080/product/4545 it will use index.html. Now I don't want it to be a redirect where I enter http://localhost:8080/product/4545 but then the url in the browser ends up changing to http://localhost:8080/index.html. I want it to use my index.html but still show product/4545 in the browser url – Justin Cross Dec 15 '15 at 02:50
  • In this case you should be using angular router. – NewUser Dec 15 '15 at 02:54
  • How can an Angular router listen to requests? I am new to Angular but unless I'm missing something that doesn't sound possible. I am trying to redirect all non *.json request to index.html so that I can handle the routing of my website via AngularJS. – Justin Cross Dec 15 '15 at 02:58
  • @JustinCross: Lets continue the discussion in http://chat.stackoverflow.com/rooms/97915/angular-js-discussion – NewUser Dec 15 '15 at 03:15
  • It won't let me type there. I need 20 reputation :( sorry – Justin Cross Dec 15 '15 at 03:20