gpt4 book ai didi

java - Spring MVC 基于 Java 的配置 - 未检测到 servlet 调度程序

转载 作者:行者123 更新时间:2023-12-02 02:03:19 25 4
gpt4 key购买 nike

我正在编写一个 Spring MVC Web 应用程序。我尝试使用基于 Java 的配置来配置 Spring,但是如果不添加额外的 XML 配置,Spring 无法检测到我的 Controller 。

我的前端 Controller 类如下所示:

package no.magnus.controller; 
public class FrontController extends
AbstractAnnotationConfigDispatcherServletInitializer {

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

@Override
protected Class<?>[] getServletConfigClasses() {
return null;
}

@Override
protected String[] getServletMappings() {
return new String[] {"/"};
}
}

我的MVC配置类:

package no.magnus.config;
@Configuration
@EnableWebMvc
@ComponentScan(basePackages = {"no.magnus"})
public class MvcConfig implements WebMvcConfigurer {

@Bean
public InternalResourceViewResolver viewResolver() {
InternalResourceViewResolver vr = new InternalResourceViewResolver();
vr.setSuffix(".jsp");
return vr;
}
}

一个简单的家庭 Controller :

package no.magnus.controller;
@Controller
public class HomeController {

@RequestMapping("/home")
public ModelAndView index() {

ModelAndView modelAndView = new ModelAndView();
modelAndView.setViewName("index");
return modelAndView;
}
}

正在尝试访问 url http://localhost:8080/home我收到 404 错误。如果我将基于 XML 的配置添加到 web.xml 以及 xml servlet 配置中,则会检测到 home Controller 并返回 index.jsp。

子集 web.xml:

<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/dispatcher-servlet.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>

调度程序-servlet.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:ctx="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<ctx:annotation-config/>
<ctx:component-scan base-package="no.magnus"/>

</beans>

http://localhost:8080/homeindex.jsp返回成功!

我的期望是 Spring MVC 应能够从 java 代码检测 Spring MVC 配置,从而使上述 XML 配置变得多余。请澄清我的理解是否有误。 '

谢谢!

编辑1:应用克里斯建议的代码更改。新问题出现。仅使用 java 配置仍然没有返回 index.jsp。 enter image description here

├───java
│ └───no
│ └───magnus
│ ├───config
│ │ MvcConfig.java
│ │
│ └───controller
│ FrontController.java
│ HomeController.java

└───webapp
│ result.jsp

└───WEB-INF
│ web.xml

└───jsp
index.jsp

最佳答案

http://localhost:8080/home不会调用您的 HomeControllerindex()-方法 (/home-Mapping) - 在您发布的网址中 home 是您的应用程序的名称并且您需要 "/" 到 index.jsp 的映射。

请将您的FrontController更改为:

package no.magnus.config; 
public class FrontController extends
AbstractAnnotationConfigDispatcherServletInitializer {

@Override
protected Class<?>[] getRootConfigClasses() {
// from
// return new Class[] {MvcConfig.class};
// to
return null;
}

@Override
protected Class<?>[] getServletConfigClasses() {
// from
// return null;
// to
return new Class[] {MvcConfig.class};
}

@Override
protected String[] getServletMappings() {
return new String[] {"/"};
}
}

你的 MvcConfig 应该是这样的:

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ViewResolverRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.view.InternalResourceViewResolver;


@EnableWebMvc
@Configuration
// to also scan subpackages for components
@ComponentScan("no.magnus.**")
public class MvcConfig
implements
WebMvcConfigurer {
@Override
public void configureDefaultServletHandling(final DefaultServletHandlerConfigurer configurer) {
configurer.enable();
WebMvcConfigurer.super.configureDefaultServletHandling(configurer);
}

@Override
public void configureViewResolvers(final ViewResolverRegistry registry) {
registry.viewResolver(this.jspViewResolver());
WebMvcConfigurer.super.configureViewResolvers(registry);
}

@Bean
public InternalResourceViewResolver jspViewResolver() {
final InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
viewResolver.setOrder(1);
// jsp-location!!! - please change it to where your jsp's are located
viewResolver.setPrefix("/WEB-INF/jsp/");
viewResolver.setSuffix(".jsp");
return viewResolver;
}
}

你的HomeController应该看起来像这样:

package no.magnus.controller;
@Controller("HomeController")
public class HomeController {

// for the very first start
// RequestMapping can be replaced with GetMapping
@GetMapping("/")
public ModelAndView index() {

ModelAndView modelAndView = new ModelAndView();
modelAndView.setViewName("index");
return modelAndView;
}
}

关于java - Spring MVC 基于 Java 的配置 - 未检测到 servlet 调度程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57365669/

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