gpt4 book ai didi

java - 基于 Spring Java 的配置不扫描我的 Controller 映射

转载 作者:太空宇宙 更新时间:2023-11-04 10:44:42 24 4
gpt4 key购买 nike

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

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

下面是我的代码:

项目结构:

enter image description here

Controller :

 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>

最佳答案

在 SpringContextConfig.java 中

viewResolver.setPrefix(".jsp");

应该是

welcome.jsp

不是

.jspwelcome

更改为viewResolver.setSuffix(".jsp");

关于java - 基于 Spring Java 的配置不扫描我的 Controller 映射,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48546484/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com