My Application is based on these technologies:
- Spring MVC with REST Services
- Angular.js.
The Application is working fine but I'm now at a point where I thinking I am using the wrong architecture.
What I want is to separate the backend and frontend functionalities at all. I started to build a menu in angular with different URL's.
My Index.html looks like this:
<html> //all html stuff
...
<nav class="hidden-xs">
<ul class="nav navbar-nav">
<a href="#home" role ="button" class="navbar-brand">
Home
</a>
<a href="#searchContract" role ="button" class="navbar-brand">
Search Contract
</a>
</ul>
</nav>
...
<div id="wrapper">
<div ng-view></div>
</div>
...
//inport scripts
</html>
At the backend I created UI Controller which returns my html pages. The pages getting rendered in the Index.html. The Url is also changing.
IndexUIController.java
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping("/")
public class IndexUIController {
@RequestMapping
public String getIndexPage() {
return "index";
}
}
I created same Controller for home
and searchContract
. My HTML pages are in WEB-INF folder.
My app.js looks like this
var TestApp = {};
var App = angular.module('TestApp', ['ui.bootstrap','ngRoute','angular-table']);
App.config(['$routeProvider', function ($routeProvider) {
$routeProvider.when('/searchContract', {
templateUrl: 'searchContract/searchContractLayout',
controller: contractController
});
$routeProvider.when('/home', {
templateUrl: 'home/homeLayout',
});
$routeProvider.otherwise({redirectTo: '/home'});
}]);
I use Velocity as View Resolver:
part of web.xml
<!-- Handles Spring requests -->
<servlet>
<servlet-name>TestApp</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>WEB-INF/spring/webmvc-config.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>TestApp</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
part of webmvc-config.xml
<mvc:resources mapping="/resources/**" location="/resources/" />
<context:component-scan base-package="at.testApp" />
<bean id="velocityConfig" class="org.springframework.web.servlet.view.velocity.VelocityConfigurer">
<property name="resourceLoaderPath" value="/WEB-INF/html/"/>
</bean>
<bean id="viewResolver" class="org.springframework.web.servlet.view.velocity.VelocityViewResolver">
<property name="cache" value="true"/>
<property name="prefix" value=""/>
<property name="suffix" value=".html"/>
<property name="exposeSpringMacroHelpers" value="true"/>
</bean>
I'm really unsure if this is the right architecture to build a single page application with angular and using functionalities for Spring and REST Services. Because I also thinking about to use the frontend application in an offline way (IndexedDb...). My approach is to use Spring only for the REST Service and Business logic. All the client side stuff should be done at client side.
How can I use Spring MVC as a REST Service provider instead of JSP/Velocity/Any other Template view resolver. And also in a clean architecture way.
Thanks in advance for all information.