Deploying Spring MVC on Oracle WebLogic Server

1- The objective of the document

In the post, I will guide you how to deploy Spring MVC Web app on Oracle WebLogic Server. The post is written based on:
  • Spring MVC 4.x

  • Oracle WebLogic Server 12c (12.2.1.1.0).

Make sure that you have already installed WebLogic Server, if not, you can see the see the instructions here:
In the post, I will package Spring MVC Web app that is written on Eclipse and deploye on WebLogic Server.

Take note that if you deploy Spring MVC on other servers such as Tomcat and Glassfish, you do not need to change anything on your Source code. However if you deploy it on WebLogic, you need to add some configuration files to source code prior to package.
I will deploy the application "Hello World Spring MVC" on WebLogic. You can see the application here:
Or download Source Code:

2- Add the configuration to the project

This is the image of Project after completion:
Adding  weblogic.xml to WEB-INF. In the weblogic.xml file, you need to configure contextPath (context-root) for application. In case I name it as /springmvc. You can name as  / if you want to use empty ContextPath.
/WEB-INF/weblogic.xml
<?xml version='1.0' encoding='UTF-8'?>
<weblogic-web-app>
    <context-root>/springmvc</context-root>
    <container-descriptor>
        <show-archived-real-path-enabled>true</show-archived-real-path-enabled>
    </container-descriptor>
</weblogic-web-app>

3- Packaging Applications

Maven Clean:
Maven Install:
War file was created on the "target" folder of project.

4- Deploy on Weblogic

Note: If you get error in one of the deployment step, let's see the Appendix at the end of the post.
Your application has been deployed on WebLogic and it has the status "prepared". You need to take one more step to activate it.
Re-unlock to change.
Start apps:
Your application has been activated successfully.

Test apps:

5- Undeloy

In case that you want to redeploy, or to undeloy it, you must stop application.

6- Some errors when deploying

If you get error below during the deployment process on WebLogic:
java.io.FileNotFoundException:
Could not open ServletContext resource [/WEB-INF/SpringDispatcher-servlet.xml]
The cause of error found is that your project Spring MVC uses Annotation to configure (not use XML) but application still reads configuration from XML file ( SpringDispatcher-servlet.xml) that is not existing on your Project.

You need to configure it on SpringWebAppInitializer to make sure that application will not find to read XML files above-mentioned.
// Add code:

dispatcher.setInitParameter("contextClass", appContext.getClass().getName());

servletContext.addListener(new ContextLoaderListener(appContext));
View full code:
SpringWebAppInitializer.java
package org.o7planning.hellospringmvc.config;

import javax.servlet.FilterRegistration;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration;

import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.context.ContextLoaderListener;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.filter.CharacterEncodingFilter;
import org.springframework.web.servlet.DispatcherServlet;

public class SpringWebAppInitializer implements WebApplicationInitializer {

    @Override
    public void onStartup(ServletContext servletContext) throws ServletException {
        AnnotationConfigWebApplicationContext appContext = new AnnotationConfigWebApplicationContext();
        appContext.register(ApplicationContextConfig.class);

        // Dispatcher Servlet
        ServletRegistration.Dynamic dispatcher = servletContext.addServlet("SpringDispatcher",
                new DispatcherServlet(appContext));
        dispatcher.setLoadOnStartup(1);
        dispatcher.addMapping("/");

        // IMPORTANT!!
        dispatcher.setInitParameter("contextClass", appContext.getClass().getName());

        // IMPORTANT!!
        servletContext.addListener(new ContextLoaderListener(appContext));

        // UTF8 Charactor Filter.
        FilterRegistration.Dynamic fr = servletContext.addFilter("encodingFilter", CharacterEncodingFilter.class);

        fr.setInitParameter("encoding", "UTF-8");
        fr.setInitParameter("forceEncoding", "true");
        fr.addMappingForUrlPatterns(null, true, "/*");
    }

        // .........

}