1

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?

2
  • Maybe you are simply wrong in writing the URL; did you chec all logs? does spring context load correctly? Did you use the right web application contexts? Commented Nov 10, 2016 at 16:09
  • The URL is correct: "localhost:7001/app-name/". The logs are showing no activity, as if the application is just a normal empty web application. There are no web.xml usage for Spring java configuration Commented Nov 10, 2016 at 16:19

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Hey did you add any weblogic.xml to your project as well ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.