【问题标题】:It seems my request mapping is not working看来我的请求映射不起作用
【发布时间】:2017-05-07 08:16:58
【问题描述】:

这是我的 web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app 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"
      version="2.5">
<servlet>
<servlet-name>someServlet</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>someServlet</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"
xmlns:p="http://www.springframework.org/schema/p"
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.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.cmpny.controller">   </context:component-scan>

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

这是我的控制器

package com.cmpny.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 sayHello(Model model){

    model.addAttribute("greeting", "Hello World");

    return "hello";
}
}

/WEB-INF/jsp/hello.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>Greetings</title>
</head>
<body>
<h1>${greeting}</h1>
</body>
</html>``

点击 URL 后出现以下错误

HTTP Status [404] – [Not Found]
Type Status Report
Description The origin server did not find a current representation for the target resource or is not willing to disclose that one exists.

Apache Tomcat/9.0.0.M20

我正在使用 spring-web-4.0.0.RELEASE.jar。请帮助我摆脱这个错误。当我点击http://localhost:8080/SampleApplication/ 时,它工作正常并显示来自 index.jsp 的内容。但是,当我尝试http://localhost:8080/SampleApplication/greeting.html时出现错误

【问题讨论】:

  • 我已经复制了您的确切代码,将其部署在 Apache Tomcat 9 中,并且获得了预期的结果没有问题:Hello World。
  • 嗨 Rubio,您使用的是哪个版本的 java? 1.5 还是 1.8?我刚刚尝试了 1.5,它工作正常,但不知道为什么我之前看不到响应。
  • 我用过java 1.8.0_77

标签: java spring jsp spring-mvc servlets


【解决方案1】:

以下应该可以工作:

web.xml 的变化:

<servlet-mapping> 
    <servlet-name>someServlet</servlet-name> 
    <url-pattern>/*</url-pattern>
</servlet-mapping>

控制器的变化:

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

// Do something

}

【讨论】:

  • 嗨Shinchan,感谢您的回复,但它不起作用,我在进行更改后得到了相同的回复
  • 好的。检查您是否看到与 Spring 应用程序上下文初始化相关的任何错误。
  • 不,我没有看到任何错误。当我点击呈现我的 index.jsp 的localhost:8080/SampleApplication 时,它工作正常。当我尝试localhost:8080/SampleApplication/greeting.html 时,它只给了我 404,但我在控制台上没有错误。我怀疑映射有任何问题。
猜你喜欢
  • 1970-01-01
  • 2014-05-15
  • 2014-04-18
  • 1970-01-01
  • 2014-06-12
  • 1970-01-01
  • 2014-11-03
  • 1970-01-01
  • 2017-03-05
相关资源
最近更新 更多