gpt4 book ai didi

java - Spring mvc 与 maven 给出 noHandlerFound 错误

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

我使用 spring 工具套件版本创建了一个 spring mvc maven 应用程序,如下所述:3.6.4.RELEASE

这是 pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.mytcg</groupId>
<artifactId>firstapp</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>firstapp Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>3.2.0.RELEASE</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<finalName>firstapp</finalName>
</build>
</project>

这是 web.xml

<?xml version="1.0" encoding="UTF-8"?>
<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>firstappservlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/config/servlet-config.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>firstappservlet</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>


<display-name>Archetype Created Web Application</display-name>
</web-app>

这是 servlet-config.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:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd">
<mvc:annotation-driven/>
<context:component-scan base-package="com.mytcg.controller"/>


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

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

这里是HelloController.java

package com.mytcg.controller;

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


@Controller
public class HelloController {

@RequestMapping(value ="/greeting" )
public String sayHell (Model model){

model.addAttribute("greeting", "Hellooo world");

return "hello";
}

}

这是 hello.jsp

<?xml version="1.0" encoding="ISO-8859-1" ?>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<title>Insert title here</title>
</head>
<body>
<h1>${greeting}</h1>
</body>
</html>

运行后,它显示“Hello World!”

但是当我为greeting.html提供以下网址

http://localhost:8083/firstapp/greeting.html

它给出了以下 404 错误

Apr 04, 2016 9:40:17 AM org.springframework.web.servlet.DispatcherServlet noHandlerFound
WARNING: No mapping found for HTTP request with URI [/firstapp/greeting.html] in DispatcherServlet with name 'firstappservlet'

最佳答案

您的问题出在您的 Controller 上。

在你的 Controller 中,@RequestMapping监听/greeting,但是你从你的浏览器发出一个http请求http://localhost:8083/firstapp/greeting.html

你应该这样改变你的 Controller :

@Controller
public class HelloController {

@RequestMapping(value ="/greeting.html" )
public String sayHell (Model model){

...
}

}

但是,我个人的建议是,您不应该将自己束缚在特定的实现上,而应该使用 @RequestMapping 方法,就像您以这种方式配置 DispatcherServlet 一样:

<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>
<load-on-startup>1</load-on-startup>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>

并为您的网址使用如下模式:http://localhost:8083/firstapp/greeting

希望对你有帮助

关于java - Spring mvc 与 maven 给出 noHandlerFound 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36396211/

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