简体   繁体   中英

Spring boot with mvc and autoconfigured

I'm trying to get my head around spring MVC and boot for more than a week now. I Understand the theory behind it, but somehow I can't get it to work. I know white page error questions have been asked 1000 times, but they all use a web.xml to configure, I use the application.properties.

I created a project and first, my app was my controller where I used request mapping and a response body to present the JSP page, that worked great, even with multiple calls for different JSP pages. But when I try to separate my controllers and put them in a controller class, I get a white label page error? Anyone an idea of what I'm doing wrong?

I follow the tutorial to the letter except for, his main app is in the default package, mine in a packaged app, cause I got an error saying I can't perform a components can on the base package, and somewhere between the last and the video I got stuck he overrided the protected SpringApplicationBuilder method configure, but when I try to override it I get a warning that it's not a method of the superclass. He also never explains what the message does.

This is my pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0     
     http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>be.intecBrussel.danielDemesmaecker</groupId>
<artifactId>springMVC</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<properties>
    <java.version>1.8</java.version>
</properties>

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.3.8.RELEASE</version>
</parent>

<build>
    <finalName>SpringMVC</finalName>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <configuration>
                <executable>true</executable>
            </configuration>
        </plugin>
    </plugins>
</build>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
        <groupId>org.apache.tomcat.embed</groupId>
        <artifactId>tomcat-embed-jasper</artifactId>
        <scope>required</scope>
    </dependency>
</dependencies>
</project>

my PageController:

@Controller
public class PageController {

    @RequestMapping("/")
    String home(){
        return "home";
    }

    @RequestMapping("/about")
    String about(){
        return "about";
    }

    @RequestMapping("/contact")
    String contact(){
        return "contact";
    }
}

My app:

@EnableAutoConfiguration
@ComponentScan

public class App {
    public static void main(String[] args) {
        SpringApplication.run(App.class,args);
    }
}

My app when she was still working without the controller class:

@EnableAutoConfiguration
@Controller

public class App {
    public static void main(String[] args) {
        SpringApplication.run(App.class,args);
    }

    @RequestMapping("/")
    String home(){
        return "home";
    }

    @RequestMapping("/about")
    String about(){
        return "about";
    }

    @RequestMapping("/contact")
    String contact(){
        return "contact";
    }
}

I know the community frowns on questions like why isn't this working, but I simply don't know what the problem so doesn't know any other way to ask the question. So anyone can help me understand why after moving my controller to a separate class result in broken view links, that would be nice. For reference: I was using this tutorial: http://courses.caveofprogramming.com/courses/spring-boot-tutorial/lectures/1063634

Excuse me my English, but I'm dutch

The package is the main problem. @ComponentScan takes the package and searches all sub packages. For example, a main class with package com.dan.ny.App will look in com.dan.ny.controllers.* .

When you say the default package, I am assuming that you are not putting a package declaration at the top. You need to do this. It comes down to java best practices. It doesn't have to be a good package name, but you will appreciate having it in bigger apps. Spring needs to differentiate your code from the code in libraries on classpath.

Here is the kind of folder structure you should hava

mvc/src/main/java/da/ny/App.java
mvc/src/main/java/da/ny/Controller.java

Put at the top of both files

package da.ny;

It looks like your controller is not added to the spring configuration. I see a naked annotation

@ComponentScan

As you can see in its javadoc :

If specific * packages are not defined, scanning will occur from the package of the * class that declares this annotation.

So you need to put your controller in the same, or ab subpackage as your App class, than it should be found.

Add a constructor to your controller class, add a breakpoint inside and start the app in debug mode. If it does not stop at the breakpoint, the controller is not instantiated by spring....

You wrote :

but they all use a web.xml to configure, I use the application.properties.

The web.xml is used to configure the servlet context. Since java servlet version 3 this can be done completely in Java. Spring does this for you, that is why you do not need a web.xml anymore.

application.properties are used for the configuration of spring and your own classes. This is something completely different than web.xml does.

Finaly you may have a look at this basic spring tutorial, it may be easier to follow than what you tried so far...

Hope I could help you a bit.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM