gpt4 book ai didi

java - Spring MVC 与 Maven

转载 作者:行者123 更新时间:2023-11-30 08:13:46 25 4
gpt4 key购买 nike

我创建了一个 Maven Web 项目 Project

通过porm.xml添加maven依赖使用Spring Bean配置Xml文件在文件夹WEB-INF/config中创建了servelet-config.xml创建了一个文件夹 src\main\java 并在该文件夹中创建了一个 HelloController 类。现在,当我在 HelloController 类中使用 @Controller 注释时,无法解析。我不知道当我在 serverlet-config.xml 中使用时,它显示:

Configures the annotation-driven Spring MVC Controller programming model. Note that this tag works in Web MVC only, not in Portlet MVC!

我正在关注.... https://www.youtube.com/watch?v=9MdnvleI6-8

我创建的 web.xml 是:

`<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
<display-name>Archetype Created Web Application</display-name>


<!-- The front controller of this Spring Web application, responsible for handling all application requests -->
<servlet>
<servlet-name>springDispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/config/servlet-config.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>

<!-- Map all requests to the DispatcherServlet for handling -->
<servlet-mapping>
<servlet-name>springDispatcherServlet</servlet-name>
<url-pattern>*jsp</url-pattern>
</servlet-mapping>

</web-app>
`

我的 porm.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.sat</groupId>
<artifactId>SpringMVC5</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>SpringMVC5 Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>1.2.6</version>
</dependency>
<dependency>
<groupId>springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>1.2.6</version>
</dependency>
<dependency>
<groupId>springframework</groupId>
<artifactId>spring-core</artifactId>
<version>1.2.6</version>
</dependency>
<dependency>
<groupId>springframework</groupId>
<artifactId>spring-context</artifactId>
<version>1.2.6</version>
</dependency>
</dependencies>
<build>
<finalName>SpringMVC5</finalName>
</build>
</project>

我的 servlet 配置是:

<?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:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.1.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd">


<mvc:annotation-driven/>
<context:component-scan base-package="com.sat.controllers"></context:component-scan>



</beans>

我的 Controller 类是:

package com.sat.controllers;

import javax.annotation.Resource;

import org.springframework.web.servlet.config.annotation.EnableWebMvc;



@EnableWebMvc
@Controller

public class HelloController {



}

我做错了什么?请帮忙...

最佳答案

您的 pom.xml 指定了 Spring 1.2.x - 这是一个非常旧的版本,并且肯定不支持任何类型的注释 - 您从哪里获得该 POM 片段?

Spring 已经到了 4.1.x,如果你想学习,你应该使用它。

关于java - Spring MVC 与 Maven,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29956799/

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