我跟着this tutorial创建了一个简单的MVC项目,然后又添加了一个控制器和视图。但是现在,无论我使用哪个链接,我总是在chrome中出现这个错误:
HTTP Status 404 - /spring-MVC
type Status report
message /spring-MVC
description The requested resource is not available.
Apache Tomcat/8.0.30
的web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" 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">
<display-name>spring-MVC</display-name>
<context-param>
<param-name>contextClass</param-name>
<param-value>
org.springframework.web.context.support.AnnotationConfigWebApplicationContext
</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>SpringDispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextClass</param-name>
<param-value>
org.springframework.web.context.support.AnnotationConfigWebApplicationContext
</param-value>
</init-param>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>spring_maven.spring_MVC</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>SpringDispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>30</session-timeout>
</session-config>
</web-app>
MvcConfiguration.java:
package spring_maven.spring_MVC.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
@Configuration
@ComponentScan(basePackages="spring_maven.spring_MVC")
@EnableWebMvc
public class MvcConfiguration extends WebMvcConfigurerAdapter{
@Bean
public ViewResolver getViewResolver(){
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix("/WEB-INF/views/");
resolver.setSuffix(".jsp");
return resolver;
}
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
}
}
HomeController.java:
package spring_maven.spring_MVC.controller;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class HomeController {
@RequestMapping(value="/")
public ModelAndView ejub(HttpServletResponse response) throws IOException{
return new ModelAndView("home.jsp");
}
}
PlacesController.java:
package spring_maven.spring_MVC.controller;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class PlacesController {
@RequestMapping(value="/getAllPlaces")
public ModelAndView getAllPlaces() throws IOException{
return new ModelAndView("getAllPlaces.jsp");
}
}
针对home.jsp:
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Home</title>
</head>
<body>
<h1>Hello World!</h1>
<p>This is the homepage!</p>
</body>
</html>
getAllPlaces.jsp:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>getAllPlaces</title>
</head>
<body>
eeeee
</body>
</html>
和eclipse中的文件结构:
据我所知,这些链接应该有效,但它们会产生上述错误:
http://localhost:8080/spring-MVC http://localhost:8080/spring-MVC/getAllPlaces http://localhost:8080/spring-MVC/home
我做错了什么?
答案 0 :(得分:1)
您的控制器返回home.jsp for / so以获取主页URL应该看起来像http://localhost:8080/spring-MVC/
以/结尾,而不是/ home
我猜你不需要在控制器return语句中使用.jsp:
return new ModelAndView("home");
查看解析器将完成剩下的工作
答案 1 :(得分:0)
由于两件事而发生错误: 我忘记将项目拖到正在运行的Tomcat服务器上。 我忘了重启服务器。
当我做了这两件事时,它突然开始工作.. :)