java - Spring 启动: No matching bean found exception

标签 java spring spring-boot spring-mvc jsp

我的第一个 SpringBoot 应用程序中的登录页面:

主类

@SpringBootApplication

public class MainGate extends SpringBootServletInitializer {

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(MainGate.class);
    }

    public static void main(String... args) {
        System.out.println("Booting .. ");
        SpringApplication.run(MainGate.class, args) ;
    }

}

我的 Gradle 文件

buildscript {
    ext {
        springBootVersion = '1.5.4.RELEASE'
    }
    repositories {
        maven {
            url "http://masked_domain/repository/external-proxy-group/"
        }
        maven {
            url "https://plugins.gradle.org/m2/"
        }
}
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
    }
}

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'

group = 'com.arun'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8

task fatJar(type: Jar) {
    manifest {
        attributes(
            'Implementation-Title': 'Arun Spring Boot Application',
            'Implementation-Version': version,
            'Built-By': System.getProperty('user.name'),
            'Built-Date': new Date(),
            'Main-Class':  'com.arun.MainGate',
            'Built-JDK': System.getProperty('java.version')
        )
    }
    baseName = project.name + '-all'
    from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }
    with jar
}

repositories {
        maven {
            url "http://masked_domain/repository/external-proxy-group/"
        }
        maven {
            url "https://plugins.gradle.org/m2/"
        }
}


dependencies {
    compile('org.springframework.boot:spring-boot-starter-web')
    testCompile('org.springframework.boot:spring-boot-starter-test')
    testImplementation 'junit:junit:4.12'
}

应用程序属性文件

spring.mvc.view.prefix: jsp/
spring.mvc.view.suffix: .jsp

logging.level.org.springframework=debug

资源文件夹

enter image description here

Controller 类

@Controller
public class CommonController {

    @RequestMapping("/")
    public String home(Map<String, Object> model) {
        System.out.println("Reached the homeContoller");
        return "sso_arch" ;
    }

}

异常

 017-10-20 17:01:28.568 TRACE 6704 --- [nio-8080-exec-1] .w.s.m.m.a.ServletInvocableHandlerMethod : Invoking 'com.arun.controller.CommonController.home' with arguments [{}]
Reached the homeContoller
2017-10-20 17:01:28.568 TRACE 6704 --- [nio-8080-exec-1] .w.s.m.m.a.ServletInvocableHandlerMethod : Method [com.arun.controller.CommonController.home] returned [sso_arch]
2017-10-20 17:01:28.574 DEBUG 6704 --- [nio-8080-exec-1] o.s.w.s.v.ContentNegotiatingViewResolver : Requested media types are [text/html, application/xhtml+xml, image/webp, image/apng, application/xml;q=0.9, */*;q=0.8] based on Accept header types and producible media types [*/*])
2017-10-20 17:01:28.574 DEBUG 6704 --- [nio-8080-exec-1] o.s.w.servlet.view.BeanNameViewResolver  : No matching bean found for view name 'sso_arch'
2017-10-20 17:01:28.577 DEBUG 6704 --- [nio-8080-exec-1] o.s.b.f.s.DefaultListableBeanFactory     : Invoking afterPropertiesSet() on bean with name 'sso_arch'

How to configure spring boot mvc app for JSP?中提到的解决方案不管用。

最佳答案

您找到了正确的问题,但您似乎没有在答案中添加解决方案,您需要 Add an InternalResourceViewResolver

This ViewResolver allows us to set properties such as prefix or suffix to the view name to generate the final view page URL:

@Bean
public ViewResolver getViewResolver(){
    InternalResourceViewResolver resolver = new InternalResourceViewResolver();
    resolver.setPrefix("/WEB-INF/jsp/");
    resolver.setSuffix(".jsp");
    resolver.setViewClass(JstlView.class);
    return resolver;
}

并确保你有依赖项:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-tomcat</artifactId>
</dependency>
<dependency>
    <groupId>org.apache.tomcat.embed</groupId>
    <artifactId>tomcat-embed-jasper</artifactId>
</dependency>
<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>jstl</artifactId>
</dependency>

关于java - Spring 启动: No matching bean found exception,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46848616/

相关文章:

java - 双向一对多关系不起作用

java - Spring将ProtobufHttpMessageConverter添加到没有xml配置的 Controller

java - spring-boot自动导入applicationContext.xml?

Javax : Extending named service

java - JPA - EntityManagerFactory 指向本地主机

java - 尝试从 Spring Boot 创建 SWING/AWT 框架时由 : java. awt.HeadlessException 引起

java - 用于文件名的字符串具有转义字符 "\"。我该如何解决这个问题?

java - 如何使注入(inject)的对象映射器输出特定于区域设置

java - Autowiring 适用于从 PerConnectionWebSocketHandler 实例化的类吗?

java - 如何在 mongodb 中加密 Spring Boot 应用程序数据