java spring RequestMapping 404 状态

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27231852/
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-11-02 11:27:11  来源:igfitidea点击:

spring RequestMapping 404 status

javaspringjspspring-mvc

提问by Dragos Geornoiu

Ok, I know there are like 20 posts here with the same problem, but none of them seem to help me, so this will probably be a duplicate, but I've gone over all the other posts and none of them solved my problem, so there must be something that I'm doing wrong or I'm not doing the right modifications from the answers of the previous mentioned questions.

好吧,我知道这里有大约 20 个帖子有同样的问题,但似乎没有一个对我有帮助,所以这可能是重复的,但我已经浏览了所有其他帖子,但没有一个解决了我的问题,所以一定是我做错了什么,或者我没有根据前面提到的问题的答案进行正确的修改。

I'm trying to make a small application using Spring, and I'm still experimenting with it, but I've spent like 4 days trying to figure what's wrong and I just can't. I still get a HTTP Status 404 whenever I try to get a jsp back from the controller. Nothing but 404 status through Tomcat, nothing else...

我正在尝试使用 Spring 制作一个小应用程序,我仍在试验它,但我已经花了大约 4 天的时间试图找出问题所在,但我就是做不到。每当我尝试从控制器获取 jsp 时,我仍然会收到 HTTP 状态 404。除了通过 Tomcat 的 404 状态,别无他物...

WebAppController:

网络应用控制器:

@Controller
public class WebAppController {

    @RequestMapping(value="/login", method = RequestMethod.GET)
    public String login() {
        System.out.println("You have entered the login maprequest");
        return "test1";
    }

}

web.xml:

网页.xml:

    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    version="2.5">
    <display-name>Hotel Application</display-name>
    <servlet>
        <servlet-name>WebApp</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>WebApp</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
    <welcome-file-list>
        <welcome-file>login.jsp</welcome-file>
    </welcome-file-list>
</web-app>

webApp.xml:

webApp.xml:

   <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"
    xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">
    <context:component-scan base-package="com.iquestgroup" />

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

This configuration works in a simple maven project with only the above mentioned in it. The problem is, the exact same thing isn't working in a maven project with 3 modules(persistence, service and webapp). In the webapp i've copied the exact same thing and when I run it on server I get 404 http status... even though the modules are building with success.

此配置适用于一个简单的 maven 项目,其中仅包含上述内容。问题是,完全相同的事情在具有 3 个模块(持久性、服务和 webapp)的 maven 项目中不起作用。在 webapp 中,我复制了完全相同的东西,当我在服务器上运行它时,我得到 404 http 状态......即使模块正在成功构建。

L.E. The first part of the accepted answer refers to a common servlet mapping mistake made by those who are starting with Spring. My problem was not related to it and I ended up removing it after initial answer. In order to not become confusing for readers, the first part of the accepted answer refers to the following code:

LE 接受答案的第一部分是指那些刚开始使用 Spring 的人犯的一个常见的 servlet 映射错误。我的问题与它无关,我在初步回答后最终将其删除。为了不让读者混淆,第一部分接受的答案参考了以下代码:

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

采纳答案by Sotirios Delimanolis

Change

改变

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

to

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

Currently, your mapped DispatcherServletis marked as handling all requests because of /*. This means that it will also attempt to handle a request dispatched to /WEB-INF/jsp/test1.jsp. It obviously doesn't have a handler for that and will fail.

目前,您的映射DispatcherServlet被标记为处理所有请求,因为/*. 这意味着它还将尝试处理分派到 的请求/WEB-INF/jsp/test1.jsp。它显然没有处理程序并且会失败。

The pattern /is special in that it is the default fallback if no other pattern matches. If there's a Servletthat maps to the request path, that Servletwill be chosen before your mapped DispatcherServlet.

该模式/的特殊之处在于,如果没有其他模式匹配,它是默认回退。如果有Servlet映射到请求路径的 ,Servlet则将在您的映射之前选择DispatcherServlet

Most (probably all) Servlet containers come mapped with a Servletto handle rendering JSPs with the url-patternof *.jsp. That will take precedence over your DispatcherServletmapped to /.

大部分(可能全部)Servlet容器配备了映射Servlet处理渲染的JSP用url-pattern*.jsp。这将优先于您DispatcherServlet映射到/.

If you're stillgetting 404 after these changes, then there are a few possibilities.

如果在这些更改后您仍然收到 404,则有几种可能性。

Spring logs at the INFO level any handler methods (@RequestMappingannotated methods within @Controllerbeans) it registers. If you don't see any of those in your logs, then your web application hasn't been deployed correctly/successfully.

Spring 在 INFO 级别记录它注册的任何处理程序方法(bean 中的@RequestMapping注释方法@Controller)。如果您在日志中没有看到任何这些,那么您的 Web 应用程序尚未正确/成功部署。

If your Servlet container is embedded in your IDE, check the appropriate tab/view on the apps that are deployed. If it's standalone, check that your generated .warfile is in the appropriate directory and expanded correctly.

如果您的 Servlet 容器嵌入在您的 IDE 中,请检查部署的应用程序上的相应选项卡/视图。如果它是独立的,请检查您生成的.war文件是否在适当的目录中并正确展开。

回答by Ziac

First of all, you can listen / request with any root controller like below.

首先,您可以使用如下所示的任何根控制器侦听/请求。

@Controller
@RequestMapping ("/")
public class RootCtrl {

Logger logger = Logger.getLogger(RootCtrl.class);

@RequestMapping (value = "/", method = {RequestMethod.GET, RequestMethod.POST})
public String index(ModelMap model) {

    // redirect to login page
    return "redirect:admin/index";
}}

With that controller, you will map all requests to root. Than you may redirect request to your login controller.

使用该控制器,您将所有请求映射到 root。比您可以将请求重定向到您的登录控制器。

It's all about request mapping. Your loginController or webController shold listen a request which should be specified before. In my application, login controller listens /admin request paths. Than, my login controller is like this:

这都是关于请求映射的。您的 loginController 或 webController 应该侦听应在之前指定的请求。在我的应用程序中,登录控制器侦听 /admin 请求路径。比,我的登录控制器是这样的:

@Controller
@RequestMapping ("/admin")
public class LoginCtrl {

@RequestMapping (value = "/index", method = {RequestMethod.GET, RequestMethod.POST})
public String index(@RequestParam (value = "captchaId", defaultValue = "") String captchaId, ModelMap model, HttpServletRequest request, HttpServletResponse response) {

    if(StringUtils.isNullOrEmpty(captchaId)){
        captchaId = RandomGUID.getRandomGUID();
    }

    model.addAttribute("captchaId", captchaId);

    // redirect to login page
    return "login";
}

When you will get request from this path : localhost/SampleProject/admin/index. Mappings will work.

当您从此路径收到请求时:localhost/SampleProject/admin/index.html 映射将起作用。