首页 > 解决方案 > Java Spring MVC 映射问题

问题描述

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

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

我使用集成在 Eclipse IDE 中的 Apache Tomcat 8.5 以及 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";
 }

标签: javaspringmodel-view-controller

解决方案


如下更改您的 servlet 映射

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

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

更新:

在你的 spring控制器中配置默认​​或主页,如下所示。

代替

@RequestMapping("/welcome")

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

推荐阅读