我正在使用Request Mapping注释编写spring应用程序。 http://localhost:8080/SpringMVC/hello
给出404(请求的资源不可用)。从服务器记录数据`在DispatcherServlet中找不到带有URI [/SpringMVC/WEB-INF/jsp/helloView.jsp]的HTTP请求的映射,名称为“dispatcher” ”。
有人可以帮忙。
项目结构
@Controller
@RequestMapping(value="/hello")
public class HelloController
{
@RequestMapping(method=RequestMethod.GET)
public ModelAndView sayHello()
{
ModelAndView view=new ModelAndView("helloView","HelloMessage", "Welcome");
return view;
}
}
helloView.jsp
<html>
<head>
<title>Spring Application</title>
</head>
<body>
<h2>${HelloMessage}</h2>
</body>
</html>
的web.xml
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>
调度-servlet.xml中
<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:mvc="http://www.springframework.org/schema/mvc"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
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
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
<!-- Scans the classpath of this application for @Components to deploy as beans -->
<context:component-scan base-package="controller" />
<mvc:annotation-driven />
<bean id="jspViewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
答案 0 :(得分:2)
您忘记在dispatcher-servlet.xml中添加<context:annotation-config/>
,它会激活各种注释:@Controller
,@Component
,@Service
,@Repository
。
并通过映射到/
将spring调度程序设置为默认Servlet ,这样每个请求都将在调度程序servlet中结束。
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
答案 1 :(得分:1)
你的web.xml包含调度程序servlet,其中url模式/*
实际上映射了所有内容!所以,让它/
并尝试...