[英]Spring MVC Java based configuration - Not detecting servlet dispatcher
I am writing a Spring MVC web application. 我正在编写一个Spring MVC Web应用程序。 I am attempting to configure Spring with Java based configuration, however Spring is unable to detect my controllers without adding additional XML configuration.
我正在尝试使用基于Java的配置来配置Spring,但是Spring无法在不添加其他XML配置的情况下检测到我的控制器。
My front controller class looks like this: 我的前端控制器类如下所示:
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[] {"/"};
}
}
My MVC configuration class: 我的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;
}
}
A simple home controller: 一个简单的家庭控制器:
package no.magnus.controller;
@Controller
public class HomeController {
@RequestMapping("/home")
public ModelAndView index() {
ModelAndView modelAndView = new ModelAndView();
modelAndView.setViewName("index");
return modelAndView;
}
}
Attempting to access url http://localhost:8080/home i get a 404 error. 尝试访问URL http:// localhost:8080 / home,我收到404错误。 If i add XML based configuration to web.xml as well as xml servlet configuration, the home controller is detected and index.jsp is returned.
如果我将基于XML的配置添加到web.xml以及xml servlet配置中,则会检测到主控制器并返回index.jsp。
subset web.xml: 子集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>
dispatcher-servlet.xml: dispatcher-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/home index.jsp returned successfully! http:// localhost:8080 / home index.jsp成功返回!
My expectations are that Spring MVC shall be able to detect Spring MVC configuration from java code, making above XML configuration redundant. 我的期望是Spring MVC将能够从Java代码检测Spring MVC配置,从而使上述XML配置变得多余。 Please clarify wheter my understanding is mistaken or not.
请澄清一下我的理解是否正确。 '
'
Thanks! 谢谢!
EDIT1: Applying code changes suggested by Chris. 编辑1:应用克里斯建议的代码更改。 New issue present.
目前有新期刊。 index.jsp is still not returned using only java config.
仅使用Java配置仍不会返回index.jsp。
├───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 wont call your HomeController
's index()
-Method (/home-Mapping) - in your posted url home
is the name of your app and you need a mapping for "/"
to index.jsp. http:// localhost:8080 / home不会调用
HomeController
的index()
Method(/ home-Mapping)-在发布的url中, home
是您的应用程序的名称,您需要映射以"/"
进行索引。 jsp。
please change your FrontController
to: 请将您的
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[] {"/"};
}
}
your MvcConfig should look like this: 您的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;
}
}
and your HomeController
should look like this: 并且您的
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;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.