Spring没有看到我的控制器的请求映射。我尝试使用没有xml的注释配置。
项目结构:
package com.dnn.web.config;
//imports
@Configuration
@ComponentScan("com.dnn.spring")
@EnableWebMvc
public class Config {
@Bean
public UrlBasedViewResolver setupViewResolver() {
UrlBasedViewResolver resolver = new UrlBasedViewResolver();
resolver.setPrefix("/WEB-INF/views/");
resolver.setSuffix(".jsp");
resolver.setViewClass(JstlView.class);
return resolver;
}
}
package com.dnn.web.config;
//imports
public class WebInitializer implements WebApplicationInitializer {
public void onStartup(ServletContext servletContext)
throws ServletException {
AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
ctx.register(Config.class);
ctx.setServletContext(servletContext);
Dynamic servlet = servletContext.addServlet("dispatcher", new DispatcherServlet(ctx));
servlet.addMapping("/");
servlet.setLoadOnStartup(1);
}
}
package com.dnn.spring.controller;
//imports
@Controller
public class HelloWorldController {
@RequestMapping("/hello")
public String hello( @RequestParam(value = "name", required = false, defaultValue = "World") String name, Model model) {
model.addAttribute("name", name);
return "helloworld";
}
}
当我调用此应用程序时,我看到以下结果:
localhost:8080 / dnn-project / hello
返回相同的结果。
我真的不明白我错在哪里。
P.S。 此示例的源代码:source
我认为Spring没有看到Config
和WebInitializer
类,但我不明白为什么以及如何修复它。
P.S。
的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.dnn.web</groupId>
<artifactId>dnn-project</artifactId>
<packaging>war</packaging>
<version>1.0.0-SNAPSHOT</version>
<name>dnn-project Maven Webapp</name>
<url>http://maven.apache.org</url>
<properties>
<spring.version>4.0.1.RELEASE</spring.version>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<!-- Spring dependencies -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<finalName>dnn-project</finalName>
</build>
</project>
P.S.3
如果向此网址发送请求
http://localhost:8080/hello
然后
мар 22, 2014 1:44:33 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [dispatcher] in context with path [] threw exception [Handler processing failed; nested exception is java.lang.NoClassDefFoundError: javax/servlet/jsp/jstl/core/Config] with root cause
java.lang.NoClassDefFoundError: javax/servlet/jsp/jstl/core/Config
at org.springframework.web.servlet.support.JstlUtils.exposeLocalizationContext(JstlUtils.java:101)
at org.springframework.web.servlet.view.JstlView.exposeHelpers(JstlView.java:135)
...
答案 0 :(得分:2)
Spring配置实际上一切都很好。只有缺少的部分是JSTL依赖:
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
Tomcat不包含JSTL,因此必须随应用程序一起提供。
答案 1 :(得分:0)
也许您需要刷新ApplicationContext(ctx.refresh()
)? Spring中有一些预构建的初始化类可以扩展(或查看源代码)。
答案 2 :(得分:0)
看起来Spring无法从Config.java
方法读取配置类onStartUp()
。
尝试按以下方式定义配置:
AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
context.setConfigLocation("com.dnn.web.config
Config");
来源:http://kielczewski.eu/2013/11/spring-mvc-without-web-xml-using-webapplicationinitializer/
希望你能帮到你。
Shishir
答案 3 :(得分:0)
如何添加:
servletContext.addListener(new ContextLoaderListener(ctx));
到你的WebInitializer
?