Logon.jsp
<!DOCTYPE html>
<html ng-app="angularspring">
<head>
</head>
<body ng-controller="LogonController">
<form>
<div>
TextBox : <input type="text" ng-model="person.userName" />
</div>
<div>
Password : <input type="password" ng-model="person.passWord" />
</div>
<div ng-view></div>
<button type="submit" ng-click="login(person)"> Submit
</button>
</form>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.8.2.min.js"></script>
<script type="text/javascript" src="js/jquery.i18n.properties-min-1.0.9.js"></script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/angularjs/1.0.2/angular.js"></script>
<script type="text/javascript" src="js/init.js"></script>
<script type="text/javascript" src="js/controller.js"></script>
<script type="text/javascript" src="js/i18n.js"></script>
<script type="text/javascript" src="js/base64.js"></script>
<script type="text/javascript" src="bootstrap/js/bootstrap.min.js"></script>
</body>
</html>
controller.js
var as = angular.module('angularspring',[]);
as.controller('LogonController', function ($scope, $http) {
var actionUrl = 'action/checkUserNameAndPassword/';
$scope.login = function (person) {
$http.post(actionUrl,$scope.person);
};
});
web.xml
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/action-servlet.xml
</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- Spring MVC -->
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>/action/*</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>logon.html</welcome-file>
</welcome-file-list>
In action-servlet.xml file contains the below tags and classes
action-servlet.xml
<mvc:annotation-driven/>
<context:annotation-config />
<context:component-scan base-package="com.pack" />
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"></bean>
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver" >
<property name="prefix" value="" />
<property name="suffix" value=".html" />
</bean>
LogonController.java
In LogonController i am able to get the request values that are bind to person object but i am not able to render the request to logonSuccess.html
@RequestMapping(value = "/checkUserNameAndPassword", method = RequestMethod.POST)
@ResponseBody
public ModelAndView logon(@RequestBody Person person) {
return new ModelAndView("WEB-INF/views/logonSuccess");
}
As per my understanding you are able to bind the modelattribute but not able to dispatch to the required view.
Here's what i observe
You have the @ResponseBody annotation on your logon method but it returns a ModelandView object.
If you want to return to a html view then remove the @ResponseBody annotation.
You can read more about this here
Also in the action-servlet.xml specify the prefix property of the viewResolver with the location of your html files which i think is WEB-INF/views
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/WEB-INF/views/</value>
</property>
<property name="suffix">
<value>.html</value>
</property>
</bean>
Now you can return the ModelandView like
return new ModelAndView("logonSuccess");
I faced the same issue. From the login page, I was trying to redirect to the home page on successful login. I was using angular js and spring mvc rest controllers. I was doing a http.post from angularjs and my rest controlelr was doing the authentication and returning a model and view object. But then angularjs was not redirecting me to the home page.
Then i changed my angularjs implementation and it worked. Try doing a form submit from angularjs to the controller and not a http.post or http.get. Now the page is redirected to the home page or the error page, whatever view being set in the ModelAndView object in the REST controller.