Spring- MVC

The Spring Web MVC framework is a module of the popular Spring framework that follows a Model-View-Controller architecture. This framework allows for the creation of web applications, incorporating the advantages of the Spring framework along with loose coupling of components in the MVC design pattern. It is built around a DispatcherServlet that handles HTTP requests and responses, and dispatches the requests to necessary handlers and controllers.

This example explains the steps to build a sample app in the Spring MVC framework, export it as a WAR file, and deploy it to AppSail.

  1. Create a new Maven project using an IDE of your choice. You must select the following Archetype for the project: org.apache.maven.archetypes maven-archetype-webapp.

  2. In the pom.xml file of your project, add the spring dependencies given below.

    
copy
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>5.2.19.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.2.19.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>5.2.19.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>5.2.19.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>5.2.19.RELEASE</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency>

  1. Add the dependencies below in a build enclosure to compile and generate the WAR file.
    
copy
<plugins> <plugin> <artifactId>maven-compiler-plugin</artifactId> <version>3.8.0</version> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> <plugin> <artifactId>maven-war-plugin</artifactId> <version>3.2.1</version> <configuration> <warSourceDirectory>WebContent</warSourceDirectory> </configuration> </plugin> </plugins>

  1. Create a Spring XML configuration file in your project, and add the bean configurations given below.
    
copy
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd ">

Your Spring configuration file should look something like this:

    
copy
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd "> <mvc:annotation-driven/> <mvc:default-servlet-handler/> <context:component-scan base-package="com.amelia.example.controller" /> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/"></property> <property name="suffix" value=".jsp"></property> </bean> </beans>

  1. Add a server runtime to the project. For example, if you are working in the Eclipse IDE, you can add a targeted runtime for the Apache Tomcat server in the following way: Right click on the project -> Properties -> Targeted Runtime. Here, you can add the required runtime, then click Apply.

  2. You can now add the required controller and error page handling, as well as a servlet mapping in the web.xml file of your project. For example, you can use the configuration given below:

    
copy
<welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <servlet> <servlet-name>helloworld</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>helloworld</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <error-page> <error-code>404</error-code> <location>/error.jsp</location> </error-page> <error-page> <error-code>401</error-code> <location>/error.jsp</location> </error-page> <error-page> <error-code>500</error-code> <location>/error.jsp</location> </error-page> <error-page> <error-code>400</error-code> <location>/error.jsp</location> </error-page> <error-page> <error-code>403</error-code> <location>/error.jsp</location> </error-page> <error-page> <error-code>408</error-code> <location>/error.jsp</location> </error-page> <error-page> <error-code>413</error-code> <location>/error.jsp</location> </error-page> <error-page> <error-code>414</error-code> <location>/error.jsp</location> </error-page> <error-page> <error-code>505</error-code> <location>/error.jsp</location> </error-page> <error-page> <error-code>405</error-code> <location>/error.jsp</location> </error-page> <error-page> <error-code>409</error-code> <location>/error.jsp</location> </error-page> <error-page> <exception-type> java.lang.Throwable </exception-type> <location>/error.jsp</location> </error-page>

  1. You can upload the application to Catalyst by exporting it as a WAR file. In the Eclipse example, you can right click on the project and choose Export As -> WAR. You must do the following:

    a. Choose a destination with file name as ROOT.war
    b. Uncheck the Optimize for specific server runtime option.


  1. You can now initialize an AppSail service in the same directory from the CLI or add it in an existing project directory. The app’s source must be your application’s directory. Provide the following values while initializing the app service:

    Stack: Java 8
    Platform: Java WAR

  2. Ensure the compiled files are added in the build directory you specify during initialization. Catalyst will automatically ZIP your app files during deployment to the remote console.

  3. Deploy the app service to the console.


Access the deployed app service from its endpoint URL.

Last Updated 2023-12-14 16:25:23 +0530 +0530

ON THIS PAGE