Akin_Glen
Akin_Glen

Reputation: 767

Spring Java configuration on weblogic

I'm currently new to Spring, and I'm trying to learn the pure java configuration element of it.

I'm able to run my Spring application on Tomcat with the following classes:

Config class:

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.inka.spring.test.maven.configuration;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import org.springframework.web.servlet.view.JstlView;

/**
 *
 * @author User
 */
@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "com.inka.spring.test.maven")
public class HelloWorldConfiguration extends WebMvcConfigurerAdapter {
    @Bean
    public ViewResolver viewResolver() {
        InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
        viewResolver.setViewClass(JstlView.class);
        viewResolver.setPrefix("/WEB-INF/views/");
        viewResolver.setSuffix(".jsp");

        return viewResolver;
    }
}

Initializer class:

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.inka.spring.test.maven.configuration;

import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;

/**
 *
 * @author User
 */
public class HelloWorldInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {

    @Override
    protected Class<?>[] getRootConfigClasses() {
        return new Class[] {HelloWorldConfiguration.class};
    }

    @Override
    protected Class<?>[] getServletConfigClasses() {
        return null;
    }

    @Override
    protected String[] getServletMappings() {
        return new String[] {"/"};
    }

}

Controller class:

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.inka.spring.test.maven.controllers;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

/**
 *
 * @author User
 */
@Controller
@RequestMapping("/")
public class HelloWorldController {

    @RequestMapping(method = RequestMethod.GET)
    public String sayHello(ModelMap model) {
        model.addAttribute("greeting", "Hello World from Spring 4 MVC");
        return "welcome";
    }

    @RequestMapping(value = "/helloagain", method = RequestMethod.GET)
    public String sayHelloAgain(ModelMap model) {
        model.addAttribute("greeting", "Hello World Again, from Spring 4 MVC");
        return "welcome";
    }
}

This calls a .jsp page that prints the respective messages depending on the path I enter as stated above in the codes.

However, when I try this with weblogic, it doesn't work. All I get is a 403 and a 404 error.

I would have stuck with Tomcat, but we use weblogic at our organisation and I've been instructed to build my application to work with weblogic.

Please, is there some extra configuration I'm supposed to use on weblogic?

Upvotes: 1

Views: 2379

Answers (1)

Akin_Glen
Akin_Glen

Reputation: 767

Ok, I've finally resolved the issue. Turns out, in your initializer class, no matter what class you extend, you must ALWAYS implement the WebApplicationInitializer class if you intend to deploy on weblogic. You don't have to for Tomcat (don't know about JBoss and the rest), but for weblogic, you MUST implement that class.

So, after changing this:

public class HelloWorldInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {

To this:

public class HelloWorldInitializer extends AbstractAnnotationConfigDispatcherServletInitializer implements WebApplicationInitializer {

Everything works fine!

For more information, please visit the spring guide.

Upvotes: 3

Related Questions