Java 找不到 Spring MVC 控制器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9806559/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-16 07:52:34  来源:igfitidea点击:

Spring MVC Controller Not Found

javaspring-mvc

提问by Jason

This is probably an easy one, but I'm missing something I guess. The problem comes down to this: I am trying to use a HelloController to display "/WEB-INF/hello.jsp". Unfortunately, I get a 404 when trying to access http://example.com/app/hello

这可能是一个简单的,但我想我错过了一些东西。问题归结为:我正在尝试使用 HelloController 来显示“/WEB-INF/hello.jsp”。不幸的是,我在尝试访问http://example.com/app/hello时收到 404

Here is the code. Probably an easy fix.

这是代码。可能是一个简单的修复。

web.xml:

网页.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<display-name>app</display-name>

<welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
</welcome-file-list>

 <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/classes/applicationContext.xml</param-value>
  </context-param>

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

<servlet>
    <servlet-name>dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/classes/applicationContext.xml</param-value>
    </init-param>
</servlet>

    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>
</web-app>

applicationContext.xml:

应用上下文.xml:

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

<context:component-scan base-package="web.controller" />

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

</beans>

HelloController.java:

你好控制器.java:

@Controller
public class HelloController {

    @RequestMapping(value="/hello", method=RequestMethod.GET)
    public ModelAndView helloWorld() {
        ModelAndView mv = new ModelAndView();
        mv.setViewName("hello");
        return mv;
    }
}

hello.jsp:

你好.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>Hello</title>
</head>
<body>
<p>Hello</p>
</body>
</html>

Update: Added error message per request.

更新:为每个请求添加了错误消息。

Error 404--Not Found From RFC 2068 Hypertext Transfer Protocol -- HTTP/1.1: 10.4.5 404 Not Found

The server has not found anything matching the Request-URI. No indication is given of whether the condition is temporary or permanent.

If the server does not wish to make this information available to the client, the status code 403 (Forbidden) can be used instead. The 410 (Gone) status code SHOULD be used if the server knows, through some internally configurable mechanism, that an old resource is permanently unavailable and has no forwarding address.

错误 404——未从 RFC 2068 超文本传输​​协议中找到——HTTP/1.1:10.4.5 404 未找到

服务器未找到任何与请求 URI 匹配的内容。没有说明这种情况是暂时的还是永久性的。

如果服务器不希望客户端可以使用此信息,则可以使用状态代码 403(禁止)。如果服务器通过某些内部可配置机制知道旧资源永久不可用且没有转发地址,则应使用 410(消失)状态代码。

采纳答案by skaffman

This the problem (in web.xml):

这是问题(在web.xml):

<url-pattern>/*</url-pattern>

This will redirect allrequests to the Spring servlet, includingyour request from the controller to the JSP. Essentially, the control flow will loop from your controller back into Spring again. You need to narrow that down so that the request for the JSP goes direct to the underlying container, rather than to Spring.

这会将所有请求重定向到 Spring servlet,包括您从控制器到 JSP 的请求。本质上,控制流将再次从您的控制器循环回 Spring。您需要缩小范围,以便对 JSP 的请求直接发送到底层容器,而不是 Spring。

Try changing it to

尝试将其更改为

<url-pattern>/app*</url-pattern>

And try again. You might need to fiddle a bit with leading and trailing slahes to make it work (e.g. <url-pattern>/app*</url-pattern>or @RequestMapping("hello"), etc)

然后再试一次。您可能需要稍微调整一下前导和尾随斜线以使其工作(例如<url-pattern>/app*</url-pattern>@RequestMapping("hello")等)

回答by vacuum

As you metioned in comment, log dont give you infromation that Controller mapped to specific url. So I think prblem is in controller.

正如您在评论中提到的,日志不会为您提供控制器映射到特定 url 的信息。所以我认为 prblem 在控制器中。

Make sure, controller in the "mil.army.retain.web.controller" package and turn on the annotation-config:

确保“mil.army.retain.web.controller”包中的控制器并打开注释配置:

<context:annotation-config />

回答by Prabhat Kumar Kashyap

Please check that your all controller class/sub-packages or other classes are within the same package as your have mentioned in the following line:

请检查您的所有控制器类/子包或其他类是否在与您在以下行中提到的相同的包中:

<context:annotation-config />
<context:component-scan base-package="com.kfs" />