java 如何在 Spring MVC 中正确使用多个控制器

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

How to properly use multiple controllers in Spring MVC

javaspringmodel-view-controllercontrollerrouting

提问by Andreas

I am trying to use 2 controllers with one dispatcher servlet in Spring MVC. But I am running into 404 errors when trying to render the views. The dispatcher is pretty straightforward, from web.xml:

我试图在 Spring MVC 中使用 2 个控制器和一个调度程序 servlet。但是我在尝试渲染视图时遇到了 404 错误。调度程序非常简单,来自 web.xml:

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

and with the following configuration:

并具有以下配置:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    ...
<context:component-scan base-package="com.mycompany.azalea" />

<mvc:annotation-driven />

</beans>

The controllers are:

控制器是:

package com.mycompany.azalea;

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

@Controller
@RequestMapping(value = "/home")
public class homeController {

    @RequestMapping(value = "/")
    public String home() {
         return "index";
    }
}

and

package com.mycompany.azalea;

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

@Controller
@RequestMapping(value = "/data")
public class dataController {

    @RequestMapping(value = "/")
    public String home() {
         return "index";
    }
}

and I am using a pretty standard resolver:

我正在使用一个非常标准的解析器:

@Configuration
public class AppConfig {

// Resolve logical view names to .jsp resources in the /WEB-INF/views directory
@Bean
ViewResolver viewResolver() {
        InternalResourceViewResolver resolver = new InternalResourceViewResolver();
        resolver.setPrefix("WEB-INF/views/");
        resolver.setSuffix(".jsp");
        return resolver;
    }
}

Views are set up under WEB-INF/views/home/ and WEB-INF/views/data/

视图设置在WEB-INF/views/home/和WEB-INF/views/data/下

However if I try to request a URL like http://localhost:8080/Azalea/home/

但是,如果我尝试请求像这样的 URL http://localhost:8080/Azalea/home/

I get an entry in the GlassFish log:

我在 GlassFish 日志中得到一个条目:

SEVERE: PWC6117: File ".../build/web/home/WEB-INF/views/index.jsp" not found

严重:PWC6117:找不到文件“.../build/web/ home/WEB-INF/views/index.jsp”

instead of the expected request for

而不是预期的请求

".../build/web/WEB-INF/views/home/index.jsp"

“.../build/web/WEB-INF/views/ home/ index.jsp”

Same pattern for "/data". It essentially looks like the request mapping is inserted into the wrong position in the request.

“/data”的相同模式。它本质上看起来像是将请求映射插入到请求中的错误位置。

My current work around is to modify the resolver to

我目前的工作是将解析器修改为

resolver.setPrefix("../WEB-INF/views/"); 

and return the following from the controller:

并从控制器返回以下内容:

public class homeController {

公共类 homeController {

    @RequestMapping(value = "/")
    public String home() {
         return "home/index";
    }
}

But this seems to be a suboptimal solution. Please let me know if you have any suggestions.

但这似乎是一个次优的解决方案。如果您有任何建议,请告诉我。

采纳答案by Bhashit Parikh

You almost got it right. The prefix has to be absolute here to make it work the way you want it to. That is: The prefixfor the view-resolver has to be set as an absolute:

你几乎猜对了。前缀在这里必须是绝对的,以使其按您希望的方式工作。也就是说:prefix视图解析器的 必须设置为绝对值:

resolver.setPrefix("WEB-INF/views/");

And, when you return the view names from the @RequestMappingmethods, they have to be the paths relative to your view-resolver's prefixpath. So, in the homeController, you should return home/index, and in your dataController, you should return data/index.

而且,当您从@RequestMapping方法返回视图名称时,它们必须是相对于您view-resolverprefix路径的路径。因此,在 中homeController,您应该返回home/index,而在您中dataController,您应该返回data/index