0

I've developed a simple web page with angularjs and spring framework.

I tried to make the web page using angular route so that web page work as SPA.

Here's my simple main jsp file. The page is shown when I access to 'http://localhost:8080/test' on the chrome.

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ page session="false" contentType="text/html; charset=UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<title>Home</title>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.1/angular.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.1/angular-route.min.js"></script>

<script>
    angular.module("app", ['ngRoute'])
        .config(function($routeProvider) {
            $routeProvider.when("/home", {
                templateUrl: "home.html",
                controller: "controller"
            })
        })
        .controller("controller", function() {

        });
</script>
</head>

<body ng-app="app" ng-controller="controller as main">
    <ul>
        <li><a href="#/home">HOME</a></li>
    </ul>

    <ng-view></ng-view>
    <div ng-view></div>
</body>

</html>

I want to put the home.html into <ng-view> or <div class="ng-view"> when I click the 'HOME' link without page refresh but it's not working.

What's the problem with the code?

hshan
  • 655
  • 2
  • 6
  • 19

1 Answers1

1

To get it to work change href="#/home" to href="#!home"

Also, make sure home.html is in the /src/main/resources/static/ directory

I would prefer to use .html with Thymeleaf instead of .jsp

Read

Integrating JSP with AngularJS, Is it a concern in real world...Beginer in Angular JS

https://spring.io/blog/2015/08/19/migrating-a-spring-web-mvc-application-from-jsp-to-angularjs

Community
  • 1
  • 1
Sully
  • 14,672
  • 5
  • 54
  • 79
  • You are a life saver! There are useful for me. Thanks for your answer. :) – hshan Feb 23 '17 at 02:48
  • why I should use '#!home' instead? All documents I found said that use '#/home'. – hshan Feb 23 '17 at 05:19
  • The issue is due to the character encoding to make the URL html safe. I tested and this was fine for the case. If you find out let me know – Sully Feb 23 '17 at 07:59