简体   繁体   English

Spring启动与MVC和自动配置

[英]Spring boot with mvc and autoconfigured

I'm trying to get my head around spring MVC and boot for more than a week now. 我正在努力使自己在春季MVC上领先,并且现在已经启动了一个多星期。 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. 我知道白页错误问题已被问过1000次,但是它们都使用web.xml进行配置,而我使用了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. 我创建了一个项目,首先,我的应用程序是我的控制器,在其中我使用请求映射和响应正文来呈现JSP页面,即使多次调用不同的JSP页面,该效果也很好。 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. 我遵循本教程的内容,但他的主要应用程序位于默认程序包中,我的应用程序位于打包的应用程序中,原因是我收到一个错误消息,说我无法在基本程序包上执行组件,并且在最后一个程序包与视频被卡住了,他覆盖了受保护的SpringApplicationBuilder方法配置,但是当我尝试覆盖它时,我得到一个警告,它不是超类的方法。 He also never explains what the message does. 他也从不解释消息的作用。

This is my pom.xml: 这是我的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: 我的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 供参考:我正在使用本教程: 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. @ComponentScan拿包并搜索所有子包。 For example, a main class with package com.dan.ny.App will look in com.dan.ny.controllers.* . 例如,包com.dan.ny.App的主类将在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. 它归结为Java最佳实践。 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. Spring需要将您的代码与类路径库中的代码区分开。

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. 看来您的控制器未添加到spring配置中。 I see a naked annotation 我看到一个裸露的注解

@ComponentScan

As you can see in its javadoc : 如您在其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. 因此,您需要将控制器放置在与App类相同或AB子包中,而不应该找到它。

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. 但它们都使用web.xml进行配置,我使用application.properties。

The web.xml is used to configure the servlet context. web.xml用于配置servlet上下文。 Since java servlet version 3 this can be done completely in Java. 从Java Servlet版本3开始,这可以完全在Java中完成。 Spring does this for you, that is why you do not need a web.xml anymore. Spring为您做到了,这就是为什么您不再需要web.xml的原因了。

application.properties are used for the configuration of spring and your own classes. application.properties用于配置spring和您自己的类。 This is something completely different than web.xml does. 这与web.xml完全不同。

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. 希望我能对您有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Spring 引导:ComponentScan 与自动配置的 Jar 中声明的 Bean - Spring Boot: ComponentScan vs Declared Bean in an Autoconfigured Jar 如何在没有persistence.xml文件的情况下禁用Wildfly 10 Hibernate Search模块,该文件是自动配置的Spring Boot应用程序? - How to disable Wildfly 10 Hibernate Search module without persistence.xml file is autoconfigured Spring Boot app? 将 Spring Boot 属性 server.compression.enabled 设置为 true 不会应用于自动配置的 Tomcat 服务器 - Setting Spring Boot property server.compression.enabled to true doesn't get applied to the autoconfigured Tomcat Server 您可以在 Autoconfigured Spring Boot H2 测试数据库上设置兼容模式吗? - Can you set the compatibility mode on the Autoconfigured Spring Boot H2 test DB? Spring:依赖库中的自动配置bean未在Spring上下文中加载 - Spring : autoconfigured bean in dependency library is not loaded in Spring context 传统 Spring MVC 到 Spring Boot - Legacy Spring MVC to Spring Boot Spring MVC vs Spring Boot vs Spring - Spring MVC vs Spring Boot vs Spring Spring Boot + Spring Data + Spring MVC编码 - Spring Boot + Spring Data + Spring MVC Encoding 带有MVC的Spring Boot SOAP Web服务 - Spring Boot SOAP webservice with MVC 用于 Spring MVC 的 Spring Boot Actuator,无需在另一个端口上启动 Spring - Spring Boot Actuator for Spring MVC without Spring boot on another port
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM