gpt4 book ai didi

java - servlet 类对 "org.springframework.web.servlet.DispatcherServlet"的引用未实现接口(interface) javax.servlet.Servlet

转载 作者:太空宇宙 更新时间:2023-11-04 07:01:59 31 4
gpt4 key购买 nike

我是 Spring 新手,正在尝试使用 Maven 部署和运行我的第一个 Spring Web 应用程序。有关更多详细信息,请参阅 here (我昨天发布的一个问题):

现在,我遇到的问题是:当我尝试以下 2 个 URL 时,我收到一个HTTP 状态 404 - 资源不可用。

http://localhost:8080/CounterWebApp/welcome
http://localhost:8080/CounterWebApp/welcome/sandeep

Tomcat 服务器控制台警告消息:

No mapping found for HTTP request URI [/CounterWebApp/welcome]
No mapping found for HTTP request URI [/CounterWebApp/welcome/sandeep]

这是我的 web.xml 的相关部分:

<servlet>
<servlet-name>mvc-dispatcher</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>

这里我有一个警告:

servlet-class references to "org.springframework.web.servlet.DispatcherServlet" that does not implement interface javax.servlet.Servlet

Controller 类:

package com.sandeep.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
@RequestMapping("/")
public class BaseController {

@RequestMapping(value="/welcome", method = RequestMethod.GET)
public String welcome(ModelMap model) {

model.addAttribute("message", "Maven Web Project + Spring 3 MVC - welcome()");

//Spring uses InternalResourceViewResolver and return back index.jsp
return "index";

}

@RequestMapping(value="/welcome/{name}", method = RequestMethod.GET)
public String welcomeName(@PathVariable String name, ModelMap model) {

model.addAttribute("message", "Maven Web Project + Spring 3 MVC - " + name);
return "index";

}

}

我错过了什么?请帮忙。

编辑:

web.xml:

<servlet-mapping>
<servlet-name>mvc-dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>

mvc-dispatcher-servlet.xml:

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/WEB-INF/pages/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>

我在/WEB-INF/pages/中有这个index.jsp

pom.xml(servlet api 依赖项)

<!--Servlet API Dependency-->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>

最佳答案

解决方案:

mvc-dispatcher-servlet.xml 中的base-package 值不正确。

我必须纠正它,然后构建、部署并运行。 :)

mvc-dispatcher-servlet.xml(最终)

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">

<context:component-scan base-package="com.sandeep.controller" />

<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/WEB-INF/pages/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>

</beans>

这是完整的documentation :)

关于java - servlet 类对 "org.springframework.web.servlet.DispatcherServlet"的引用未实现接口(interface) javax.servlet.Servlet,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21966070/

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