Results 1 to 3 of 3

Thread: Help: Javabased config isn't working

  1. #1
    Join Date
    Aug 2012
    Posts
    4

    Default Help: Javabased config isn't working

    Hi Folks,
    I am relatively new to this subject, so I need a little help.
    I just started a small demo-project "bookstore" and I can't get the container working.
    What I've done so far:

    1. My dependencies
    Code:
    <dependencies>
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>3.8.1</version>
                <scope>test</scope>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-core</artifactId>
                <version>3.2.0.RELEASE</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-webmvc</artifactId>
                <version>3.2.0.RELEASE</version>
                <exclusions>
                    <exclusion>
                        <artifactId>commons-logging</artifactId>
                        <groupId>commons-logging</groupId>
                    </exclusion>
                </exclusions>
            </dependency>
            <dependency>
                <groupId>cglib</groupId>
                <artifactId>cglib</artifactId>
                <version>2.2.2</version>
            </dependency>
            <dependency>
                <groupId>javax.servlet</groupId>
                <artifactId>javax.servlet-api</artifactId>
                <version>3.1-b04</version>
                <scope>compile</scope>
            </dependency>
        </dependencies>
    2. My Configuration class:
    Code:
    @Configuration
    @EnableWebMvc
    @ComponentScan(basePackages = {"de.logentis.bookstore.Controller"})
    public class BookstoreConfiguration {
        @Bean
        public InternalResourceViewResolver configureInternalResourceViewResolver() {
            InternalResourceViewResolver resolver = new InternalResourceViewResolver();
            resolver.setPrefix("/WEB-INF/views/");
            resolver.setSuffix(".jsp");
            return resolver;
        }
    }
    3. My Initialization Class:
    Code:
    public class BookstoreWebApplicationConfiguration implements WebApplicationInitializer {
    
        private WebApplicationContext createContext(final Class<?>... annotatedClasses) {
            AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
            context.register(annotatedClasses);
            return context;
        }
    
        @Override
        public void onStartup(ServletContext servletContext) throws ServletException {
            WebApplicationContext dispatcherContext = createContext(BookstoreConfiguration.class);
            DispatcherServlet dispatcherServlet = new DispatcherServlet(dispatcherContext);
            ServletRegistration.Dynamic dispatcher = servletContext.addServlet("dispatcher", dispatcherServlet);
            servletContext.addListener(new ContextLoaderListener(dispatcherContext));
            dispatcher.setLoadOnStartup(1);
            dispatcher.addMapping("/*");
        }
    }
    4. My Testcontroller:
    Code:
    @Controller
    @RequestMapping(value = "/")
    public class HomeController {
    
        @RequestMapping(value="/home")
        public String displayHome(){
            return("index");
        }
    }
    As far as I can see, I didn't forget anything, did I?
    Packaging the whole to a WAR and deploying in Tomcat 7.0.34 results in a simple 404, when calling "/home"
    Not even a message in the log could be found, suggesting, that an appropriate route isn't configured.
    Seems, that the dispatcher servlet isn't working (correctly). But I have no clue, why this is the case.
    So I would be glad, if anybody points out, what I did wrong.
    Thanks in advance!

  2. #2
    Join Date
    Aug 2012
    Posts
    4

    Default

    Strange enough ... the examples above didn't work in IntelliJ 12.x. But when I start with STS, get a fresh SpringMVC project, do a little cleanup on the deps and clear the web.xml it's working.

    @Mods: Please close as SOLVED :] Thank you !

  3. #3
    Join Date
    Aug 2012
    Posts
    4

    Default

    Didn't delete web.xml ...
    When I use the Standard web.xml for Servlet 3.0:
    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    
    <web-app
            version="3.0"
            xmlns="http://java.sun.com/xml/ns/javaee"
            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
    
    </web-app>
    It works like a charm ... something learned :]

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •