2

I have a JAVA Spring server, and I am trying to return a HTML page with the controller, but the problem is that I don't know what is the right way to load the page with Angular. Or maybe I am doing it wrong?

JAVA

@Controller
public class Login {
@RequestMapping(value="/login", method=RequestMethod.POST)
public @ResponseBody String testLogin(@RequestBody LoginInfo login) throws MyException{
    if (login.getUser().equals("test") && login.getPass().equals("1234")){
    return "main.html";
    } else {
        throw new MyException("Wrong login info");
    }
}

AngularJS

$scope.login = function(){
url = "/login";
$http.post(url,{
    "user":$scope.user,
    "pass":$scope.pass  
}).then(
        function (response){
        // what should be here???
        });};
2
  • 1
    your java code shows you want to load html page but your angular code shows you want to make a login api call Commented Aug 6, 2016 at 15:06
  • Get your template through router and provide your credentials with $http object. Commented Aug 6, 2016 at 15:07

1 Answer 1

3

You must return true or false from your JAVA Controller

@Controller
public class Login {
@RequestMapping(value="/login", method=RequestMethod.POST)
public @ResponseBody String testLogin(@RequestBody LoginInfo login) throws MyException{
    if (login.getUser().equals("test") && login.getPass().equals("1234")){
        return true;
    } else {
        return false;
        throw new MyException("Wrong login info");
    }
}

And then in AngularJS :

1] If you are using Angular Route, then use $location.path()

2] If you are using angular-ui-router, then use $state.go()

1] Example :

$scope.login = function() {
    url = '/login';
    $http.post(url,{
        "user":$scope.user,
        "pass":$scope.pass  
    }).then(function (response) {
        if(response) {
            $location.path('/main');
        } else {
            $location.path(url);
        }
    });
};
Sign up to request clarification or add additional context in comments.

2 Comments

It worked, but now the URL is localhost:8080/#/main and it should be localhost:8080/main.html, I'v tried to set the $routeProvider and the $locationProvider.html5Mode(true) but it still doesn't work.
add code in index.html : <head> <meta charset="utf-8"> <base href="/"> </head> More info: link

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.