gpt4 book ai didi

java - HTTP 错误 404。找不到请求的资源。 Spring MVC

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

我是 Spring MVC 的新手。在尝试基本程序时,我遇到了 404 错误页面。

Web.xml

<web-app id="WebApp_ID" 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>My Demo App</display-name>
<servlet>
<servlet-name>myDemoApp</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/config/myDemoApp-servletConfig.xml</param-value>
</init-param>
</servlet>

<servlet-mapping>
<servlet-name>myDemoApp</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>

</web-app>

myDemoApp-servletConfid.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">

<mvc:annotation-driven/>

<!-- Components Scanner...package name should be the one which is having the controllers -->
<context:component-scan base-package="com.demo.controllers"></context:component-scan>

<!-- View Resolver Interface -->
<!-- <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="WEB-INF/jsp/"></property>
<property name="suffix" value=".jsp"></property>
</bean> -->

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:prefix="/WEB-INF/jsp/" p:suffix=".jsp" />
</beans>

Controller 类

package com.demo.controllers;

import java.util.Random;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class MyDemoController {

private String[] quotes = {"Hi I am vikram,"
+ "I am from Bangalore,"
+ "Bangalore is full of traffic"};

@RequestMapping("/getQuote")
public String getRandomQuote(Model model){

int rand = new Random().nextInt(quotes.length);
String randomQuote = quotes[rand];

//http://localhost:8080/springMVCDemo/getQuote.html
model.addAttribute("randomQuote",randomQuote);
return "quote";
}

}

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>My Demo App</title>
</head>
<body>
<h1>The Quote is: </h1>
<p>${randomQuote}</p>
</body>
</html>

使用的网址:

http://localhost/8050/springMVCDemo/getQuote.html

文件夹结构:[在此处输入图像描述][1]

请向我建议错误的解决方案。

最佳答案

从错误中可以清楚地看出,spring 未能按照您当前的配置加载 Controller 。始终关注调试日志并查看启动时向 spring 容器注册了哪些 bean。日志记录将帮助您更好地解决这个问题。

尝试使用 Bootstrap 监听器 ContextLoaderListener将从 context-param

中设置的 contextConfigLocation 读取并加载配置

例如:

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/config/myDemoApp-servletConfid.xml</param-value>
</context-param>

<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>

关注相关post

关于java - HTTP 错误 404。找不到请求的资源。 Spring MVC,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36129803/

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