【问题标题】:Spring Java based config not scanning my controller mappings基于 Spring Java 的配置不扫描我的控制器映射
【发布时间】:2018-02-02 06:20:36
【问题描述】:

我创建了一个简单的 Hello 控制器类,其基于 java 的配置如下所示,但在启动应用程序时它没有映射请求 URI(即 /sayHello)。因此,当我请求“http://localhost:8080/helloMvcWithJavaConfig/sayHello”时,我们会收到 Http-404 错误。

为什么 Spring 上下文在启动应用程序时没有映射我的控制器 URI?

下面是我的代码:

项目结构:

控制器:

 package com.baji.mvc.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
public class Hello {

    @RequestMapping(value="/sayHello", method = RequestMethod.GET)
    public String sayHello(ModelMap mm){
        mm.addAttribute("wishes", "Hello!");
        return "welcome";
    }

}

welcome.jsp

    <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
	pageEncoding="ISO-8859-1"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>HelloWorld page</title>
</head>
<body>Greeting : ${wishes}
</body>
</html>

WebInitilizer.java

    package com.baji.mvc.config;

import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;

public class WebInitializer extends AbstractAnnotationConfigDispatcherServletInitializer{

    @Override
    protected Class<?>[] getRootConfigClasses() {
        return new Class[]{SpringContextConfig.class};
    }

    @Override
    protected Class<?>[] getServletConfigClasses() {
        return new Class[]{SpringContextConfig.class};
    }

    @Override
    protected String[] getServletMappings() {

        return new String[]{"/"};
    }

}

SpringContextConfig.java

    package com.baji.mvc.config;

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.view.InternalResourceViewResolver;

@Configuration
@EnableWebMvc
@ComponentScan("com.baji.mvc")
public class SpringContextConfig {

    @Bean
    public ViewResolver viewResolver() {

        InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
        viewResolver.setPrefix("/WEB-INF/jsp/");
        viewResolver.setPrefix(".jsp");

        return viewResolver;
    }

}

在启动应用程序时,我在 Tomcat 控制台上收到以下消息:

    Jan 31, 2018 9:06:19 PM org.apache.catalina.core.AprLifecycleListener init
INFO: The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: C:\Program Files\Java\jdk1.7.0_80\bin;C:\WINDOWS\Sun\Java\bin;C:\WINDOWS\system32;C:\WINDOWS;C:\Program Files (x86)\Intel\iCLS Client\;C:\Program Files\Intel\iCLS Client\;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\IPT;C:\Program Files\Intel\Intel(R) Management Engine Components\IPT;C:\Program Files\Java\jdk1.7.0_80\bin;C:\Baji\softwares\apache-maven-3.5.0\bin;C:\Program Files\Git\cmd;C:\salt;C:\Users\baji.shaik\AppData\Local\Microsoft\WindowsApps;C:\Users\baji.shaik\AppData\Local\Programs\Fiddler;C:\Users\baji.shaik\AppData\Local\Microsoft\WindowsApps;;.
Jan 31, 2018 9:06:19 PM org.apache.tomcat.util.digester.SetPropertiesRule begin
WARNING: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.j2ee.server:helloMvcWithJavaConfig' did not find a matching property.
Jan 31, 2018 9:06:19 PM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["http-nio-8080"]
Jan 31, 2018 9:06:19 PM org.apache.tomcat.util.net.NioSelectorPool getSharedSelector
INFO: Using a shared selector for servlet write/read
Jan 31, 2018 9:06:19 PM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["ajp-nio-8009"]
Jan 31, 2018 9:06:19 PM org.apache.tomcat.util.net.NioSelectorPool getSharedSelector
INFO: Using a shared selector for servlet write/read
Jan 31, 2018 9:06:19 PM org.apache.catalina.startup.Catalina load
INFO: Initialization processed in 716 ms
Jan 31, 2018 9:06:19 PM org.apache.catalina.core.StandardService startInternal
INFO: Starting service Catalina
Jan 31, 2018 9:06:19 PM org.apache.catalina.core.StandardEngine startInternal
INFO: Starting Servlet Engine: Apache Tomcat/8.0.0-RC1
Jan 31, 2018 9:06:20 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["http-nio-8080"]
Jan 31, 2018 9:06:20 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["ajp-nio-8009"]
Jan 31, 2018 9:06:20 PM org.apache.catalina.startup.Catalina start
INFO: Server startup in 996 ms

pom.xml

<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/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.baji</groupId>
    <artifactId>helloMvcWithJavaConfig</artifactId>
    <packaging>war</packaging>
    <version>0.0.1-SNAPSHOT</version>

    <properties>
        <springframework.version>4.3.7.RELEASE</springframework.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${springframework.version}</version>
        </dependency>

        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.0.1</version>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.6</version>
                <configuration>
                    <failOnMissingWebXml>false</failOnMissingWebXml>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

【问题讨论】:

  • 你能发布你的文件夹/包结构吗?您的控制器是否在加载您的应用程序的主 Spring 类的子包中,以便 Spring 可以在组件扫描期间找到您的控制器。
  • 确定是:localhost:8080/helloMvcWithJavaConfig/sayHello 不是localhost:8080/sayHello?或者您是不是将您的应用程序部署到嵌入式 tomcat?
  • 是的,它只是 localhost:8080/helloMvcWithJavaConfig/sayHello。但无论如何,即使 localhost:8080/sayHello 也无法正常工作。
  • 更新了我帖子中的项目结构,请参考
  • 你正在使用'@Controller',所以你应该在你的方法中添加'@RequestBody'或'@ResponseBody'或者只使用'@RestController'来代替

标签: java spring-mvc tomcat8


【解决方案1】:

在你的 SpringContextConfig.java 中

viewResolver.setPrefix(".jsp");

应该是

welcome.jsp

不是

.jsp欢迎

改成viewResolver.setSuffix(".jsp");

【讨论】:

  • 是的,它的类型问题。我刚刚更正为 viewResolver.setSuffix(".jsp");但仍然是同样的问题。
  • 你在eclipse中tomcat服务器的启动配置中添加了以下内容吗? -Djava.library.path=[tomcat_home]\bin
【解决方案2】:

我相信你会收到Http Status 404 -/WEB-INF/jsp/welcome.jsp

如果是这样,除了拼写错误之外,您的大多数配置都是正确的 viewResolver.setSuffix(".jsp");

除此更改外,我建议您更改类 SpringContextConfig 以扩展 WebMvcConfigurerAdapter

我相信如果你想定制任何你已经扩展到WebMvcConfigurerAdapter的spring mvc组件。

【讨论】:

  • 即使在扩展 WebMvcConfigurerAdapter 之后也无法正常工作
  • 有趣的日志缺少 Spring Dispatcher Servlet 的引导程序,INFO: 1 Spring WebApplicationInitializers detected on classpath INFO: Initializing Spring root WebApplicationContext INFO: Initializing Spring FrameworkServlet 'dispatcher'
  • 您是否使用“maven-archetype-webapp”的正确 maven artifactId 创建了项目?
  • 我知道已经有 2 年了。这事有进一步更新吗。即使我也面临同样的问题。
猜你喜欢
  • 1970-01-01
  • 2017-12-16
  • 2019-05-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多