gpt4 book ai didi

java - spring MVC HTTP状态404

转载 作者:行者123 更新时间:2023-12-01 10:44:39 24 4
gpt4 key购买 nike

我关注了this tutorial为了制作一个简单的 MVC 项目,我又添加了一个 Controller 和 View 。但现在,无论我使用以下哪个链接,我总是在 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中文件的结构:

enter image description here

据我所知,这些链接应该有效,但它们会产生上述错误:

http://localhost:8080/spring-MVC http://localhost:8080/spring-MVC/getAllPlaces http://localhost:8080/spring-MVC/home

我做错了什么?

最佳答案

您的 Controller 返回/的 home.jsp,因此获取主页 url 应该类似于 http://localhost:8080/spring-MVC/

ends with / , not /home

我想您不需要在 Controller 返回语句中使用 .jsp:

return new ModelAndView("home");

View 解析器将完成剩下的工作

关于java - spring MVC HTTP状态404,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34255436/

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