这可能是关于这个Spring MVC错误的第100个问题,但是我无法让它继续工作。
我正在尝试将一个简单的控制器方法映射到/ account,稍后我想添加/ account / {id},但我甚至无法使用/ account工作。
这是我的web.xml
<?xml version="1.0" encoding="ISO-8859-1" ?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
version="2.4">
<display-name>My Spring MVC web application</display-name>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>springDispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:application-context.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springDispatcherServlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:application-context.xml</param-value>
</context-param>
</web-app>
application-context.xml的内容:
<mvc:annotation-driven />
<context:component-scan base-package="org.example" />
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/views/"/>
<property name="suffix" value=".jsp"/>
</bean>
AccountController.java:
@Controller
public class AccountController {
@RequestMapping(value="/account", method = RequestMethod.GET)
public ModelAndView showAccount() throws Exception {
ModelAndView mav = new ModelAndView();
mav.setViewName("account");
mav.addObject("someText", "Hello World!");
return mav;
}
}
的src /主/ web应用/视图/ account.jsp:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<h1>${someText}</h1>
当我在Tomcat中启动应用程序时,我看到日志中出现以下行:
[localhost-startStop-1] INFO org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping - Mapped "{[/account], methods=[GET], params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public org.springframework.web.servlet.ModelAndView org.example.springmvc.controller.AccountController.showAccount() throws java.lang.Exception
对我来说,这表明url localhost:8080 / account已正确映射,至少应该提供一些输出。但是,当我访问localhost:8080 /帐户时,我收到404错误,日志显示:
No mapping found for HTTP request with URI [/views/account.jsp] in DispatcherServlet with name 'springDispatcherServlet'
No mapping found for HTTP request with URI [/favicon.ico] in DispatcherServlet with name 'springDispatcherServlet'
非常感谢你的帮助。
答案 0 :(得分:4)
尝试将以下bean添加到application-context.xml
<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"/>
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>
答案 1 :(得分:4)
您的Spring配置没有任何问题,看起来您的Controller正在正确处理/account
URI,并且正在返回视图名称account
,这正由您的InternalViewResolver解决作为/views/account.jsp
&gt;的路径。
现在由于某种原因,这个调度出错了(因为Spring DispatcherServlet的/*
映射,假设Spring也可以处理这个/视图,这可能是你看到这个特定错误的原因)。您是否可以执行此操作,而不是将视图放在/views
文件夹中,将其移至/WEB-INF/views
文件夹并将您的视图分隔符更改为:
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/"/>
<property name="suffix" value=".jsp"/>
</bean>