gpt4 book ai didi

Java Spring MVC 映射问题

转载 作者:行者123 更新时间:2023-12-02 10:53:05 25 4
gpt4 key购买 nike

我知道也许其他人已经问过这个问题,我在 stackoverflow 上检查了所有相关问题,但我无法解决我的问题,我希望你们中的人可以帮助我。我现在正在学习 Spring MVC,现在处理简单的页面。由于某种原因,即使我一切正确(或者至少我认为我这样做),我的代码也无法加载。当我尝试访问 localhost:8080/PROJECTNAME/welcome 时我得到描述源服务器没有找到目标资源的当前表示或不愿意透露该表示的存在。

来自 Java 的错误是:在名为“spring-dispatcher”的 DispatcherServlet 中找不到带有 URI [/ProjetName/welcome] 的 HTTP 请求的映射。

我使用 Apache Tomcat 8.5,它集成在 Eclipse IDE 中还有 JDK 8

这是我的代码。

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<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>MySpringMVCWebApp</display-name>

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

<servlet-mapping>
<servlet-name>spring-dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>

</web-app>

spring-dispatcher-servlet.xml

<beans xmlns = "http://www.springframework.org/schema/beans"
xmlns:context = "http://www.springframework.org/schema/context"
xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation = "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.xsd">


<context:component-scan base-package="org.mypackagename.com.*">
</context:component-scan>
<bean id="viewResolver"
class =
"org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name = "prefix" value ="/WEB-INF/jsp/"/>
<property name = "suffix" value =".jsp"/>
</bean>

HelloController.java

package org.mypackagename.com;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class HelloController {

@RequestMapping("/welcome")
public ModelAndView helloWorld() {
ModelAndView mav = new ModelAndView("HelloPage");
mav.addObject("msg", "Helloooooooooooo");
return mav;

}


}

HelloPage.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Spring MVC</title>
</head>
<body>
<h2>${msg}</h2>

</body>
</html>

HelloController.java 已更改以便工作,下面是更新后的代码:

@Controller
@RequestMapping("/hello")
public class HelloController {
@RequestMapping(method = RequestMethod.GET)public String printHello(ModelMap
model) {
model.addAttribute("message", "Hello the fcking Spring MVC Framework!");
return "hello";
}

最佳答案

如下更改您的 servlet 映射

<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>

将 url-pattern 指定为 /仅匹配 host/servlet'localhost:8080/PROJECTNAME/'

更新:

在 Spring Controller 中配置默认主页,如下所示。

而不是

@RequestMapping("/welcome")

@RequestMapping(value = "/", method = GET)

关于Java Spring MVC 映射问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52009940/

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