我正在学习Spring MVC并且我在这个问题中被阻止了几个小时,应该有一个明显的解决方案:
我在web.xml中定义了DispatcherServlet springSoccer,并在WEB-INF目录下的springSoccer-servlet.xml中对其进行了配置。
在springSoccer-servlet.xml中我正在配置ViewResolver设置指向我的控制器包的组件扫描。
我正在Tomcat 8.0中部署,当我将浏览器指向elsewhere时,我收到错误:
<!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>
<!-- Source project: sip05, branch: 01 (Maven Project) -->
<servlet>
<servlet-name>springSoccer</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springSoccer</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
这只是一个找不到我的RequestMapping的问题。在下面找到配置:
的web.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"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd">
<context:component-scan base-package="com.spring.soccer.controller" />
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/WEB-INF/views/jsp/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
<mvc:resources mapping="/resources/**" location="/resources/" />
<mvc:annotation-driven />
</beans>
springSoccer-servlet.xml中
package com.spring.soccer.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
public class SoccerController {
@RequestMapping("/users")
public String printSoccerHome(ModelMap model) {
return "HelloSoccer";
}
}
SoccerController.xml
[3, [9, [11, [4,[],[]], []], [] ], [7 , [], [6,[],[]]] ]
[3, [5, [4,[],[]], [8,[],[]]] , [7, [9,[],[]], [6,[],[]]]]
["alpha", ["berries", ["carrots", ["diamonds", ["edward",[],[]],[]],[]],[]],[]]
[7,[3,[2,[9,[],[]],[11,[1,[],[]],[]]],[]],[4,[5,[],[]],[8,[12,[13,[],[]],[0,[],[]]],[]]]]
[99, [], []]
任何指针?非常感谢。
答案 0 :(得分:0)
<servlet-name>
仅定义哪个DispatcherServlet
处理特定请求。它不像@RequestMapping
注释那样工作。您只配置了Spring,以便springSoccer
DispatcherServlet
处理所有请求。
阅读Spring MVC documentation- 21.2 The DispatcherServlet了解详情。
为了使此请求有效:
http://localhost:8080/SoccerSpringMaven3/springSoccer/users/
您必须使用@RequestMapping("/springSoccer")
为控制器添加注释或使用@RequestMapping("/springSoccer/users")