Integrating AngularJS With Spring MVC

Java Side

The following project allows you to:
- Resolve JSP pages as a view.
- convert response objects into a JSON response
-allow you to make Rest request via Spring 

Project Directory
the structure of the project is the Standard JAVA Web project structure either the one that is defined by Maven or the one that Eclipse-> Dynamic Web Project defines.

NOTE:
The WEB-INF directory shoud not contain all the pages and static files.

All of these files should be at the level of the webapp folder in the maven structure or WebContent folder in the Dynamic web project type of Eclipse IDE.


Project Dependencies

The following libraries are more than enough to run your project with AngularJS on the front-end side.


 
    <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.3.3</version>
        </dependency>
            <!-- Spring Data Neo4j -->
        
        <dependency>
            <groupId>org.codehaus.jackson</groupId>
            <artifactId>jackson-core-asl</artifactId>
            <version>1.9.13</version>
        </dependency>

<dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-beans</artifactId>
          <version>${spring-core.version}</version>
        </dependency>
        <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-expression</artifactId>
          <version>${spring-core.version}</version>
        </dependency>
        
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-config</artifactId>
            <version>${spring-security-core.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>${spring-core.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${spring-core.version}</version>
        </dependency>
        
        
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>${spring-core.version}</version>
            <scope>test</scope>
        </dependency>
        
        
        
        <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-context</artifactId>
          <version>${spring-core.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context-support</artifactId>
            <version>${spring-core.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${spring-core.version}</version>
        </dependency>
                <!-- Commons -->
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>${log4j.version}</version>
        </dependency>
        <dependency>
            <groupId>commons-logging</groupId>
            <artifactId>commons-logging</artifactId>
            <version>${commons-logging.version}</version>
        </dependency>
        <dependency>
            <groupId>commons-digester</groupId>
            <artifactId>commons-digester</artifactId>
            <version>${commons-digester.version}</version>
        </dependency>
        <dependency>
            <groupId>commons-collections</groupId>
            <artifactId>commons-collections</artifactId>
            <version>${commons-collections.version}</version>
        </dependency>
        <dependency>
            <groupId>commons-lang</groupId>
            <artifactId>commons-lang</artifactId>
            <version>${commons-lang.version}</version>
        </dependency>
       
        <!-- WEB  -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>${servlet-api.version}</version>
            <scope>provided</scope>
        </dependency>
        
        <!-- VIEW -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>${jstl.version}</version>
        </dependency>
        <dependency>
            <groupId>taglibs</groupId>
            <artifactId>standard</artifactId>
            <version>${standard.version}</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>${slf4j-api.version}</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>${slf4j-log4j12.version}</version>
        </dependency>
        <dependency>
            <groupId>cglib</groupId>
            <artifactId>cglib-nodep</artifactId>
            <version>${cglib-nodep.version}</version>
        </dependency>
       
       


web.xml
 

       <servlet>

      <servlet-name>mvc</servlet-name>

      <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

      <load-on-startup>1</load-on-startup>

     </servlet>

 

<servlet-mapping>

         <servlet-name>mvc</servlet-name>

         <url-pattern>/</url-pattern>

         </servlet-mapping>

    

     <servlet-mapping>

        <servlet-name>default</servlet-name>

        <url-pattern>/static/*</url-pattern>

     </servlet-mapping>

  

     <listener>

      <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>

     </listener>


Servlet.xml

<context:component-scan base-package="com.community.the"/>

         <mvc:annotation-driven/>

    <mvc:resources mapping="/css/**" location="/static/css/" />
    <mvc:resources mapping="/images/**" location="/static/images/" />
    <mvc:resources mapping="/js/**" location="/static/js/" />

   

    <mvc:default-servlet-handler/>

    

    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">

        <property name="prefix" value="/pages/" />

        <property name="suffix" value=".jsp" />

    </bean>

    

    <!-- Configure to plugin JSON as request and response in method handler -->

    <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">

        <property name="messageConverters">

            <list>

                <ref bean="jsonMessageConverter"/>

            </list>

        </property>

    </bean>
Do not forget the definition of the resources elements above defined if you are using Spring 3 And also this requires the annotation-driven tag defined otherwise they will no be resolved properly.
Java Controller
To load a tesp.jsp page that contains a directive included into my-directive.jsp we write the following controller

@Controller 
public class MainController{
@RequestMapping("/test")
    public ModelAndView view() throws Exception{
        return new ModelAndView("test");
    }
}   

  
AngularJS
Angular does two calls to the back-end when he calls a page with one directive separated into another file( in general this is a html file but in this particular case I am putting in it into a JSP page).

When Angular requests the html page where the directive is in (for example mydashboard) it will also trigger the second requet to bring the file that contains the directive to the front-end side.
This second request will be served by the following RequestMappingthat that it is also part of the above Java Controller previously defined.

 //AngularJS Components resolver
    @RequestMapping("/my-directive")
    public ModelAndView tick() throws Exception{

        return new ModelAndView("my-directive");

}

Spring MVC will resolve it to the RequestMapping that has a value="my-directive"
The page that contains the directive must be at the same level of the container page where the directive is referenced from.


If you are thinking where is the angular route defined for this well don't worry.
Until here there is no need to implement angular routes since everything is in front-end and Angular core does the work with in its scope.

 hope this help to some one.

No comments:

Post a Comment