gpt4 book ai didi

java - 注解配置-spring配置类位置怎么说

转载 作者:行者123 更新时间:2023-11-30 09:10:22 25 4
gpt4 key购买 nike

Spring 看不到我的 Controller 的请求映射。我尝试使用不带 xml 的注解配置。

项目结构:

enter image description here

配置.java

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;
}
}

WebInitializer.java

 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);
}
}

HelloWorldController.java

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";
}
}

当我调用这个应用程序时,我看到以下结果:

enter image description here

localhost:8080/dnn-project/hello

返回相同的结果。

我真的不明白我哪里弄错了。

附言此示例的源代码:source

我想 Spring 没有看到 ConfigWebInitializer 类,但我不明白为什么以及如何修复它。

附言

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

如果向这个url发送请求

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)

...

最佳答案

实际上,您的 Spring 配置没有问题。唯一缺少的部分是 JSTL 依赖项:

<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>

Tomcat 不包含 JSTL,因此它必须与您的应用程序一起提供。

关于java - 注解配置-spring配置类位置怎么说,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22575608/

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