简体   繁体   English

RequestToViewNameTranslator不起作用

[英]RequestToViewNameTranslator doesn't work

I am following the book 'Getting started with the spring framework'(sec. edition).In ch11-bankapp, functions with @ModelAttribute do not return a view name. 我正在阅读《 Spring框架入门》(第二版)这本书。在ch11-bankapp中,具有@ModelAttribute的函数不会返回视图名称。 Hence, the view name is supposed to be returned by RequestToViewNameTranslator. 因此,视图名称应该由RequestToViewNameTranslator返回。

@Controller
@RequestMapping(value = "/fixedDeposit")
public class FixedDepositController {
    private static Logger logger = Logger
            .getLogger(FixedDepositController.class);

    @Autowired
    private FixedDepositService fixedDepositService;

    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ModelAttribute(value = "fdList")
    public List<FixedDepositDetails> listFixedDeposits() {
        logger.info("listFixedDeposits() method: Getting list of fixed deposits");
        return fixedDepositService.getFixedDeposits();
    }

however, when I enter the url http://localhost:8080/ch11-bankapp/fixedDeposit/list it gives me the HTTP Status 404 - error. 但是,当我输入网址http:// localhost:8080 / ch11-bankapp / fixedDeposit / list时,它给了我HTTP状态404-错误。 The console gives me the following warning 控制台给我以下警告

WARN org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/ch11-session-attributes/fixedDeposit/list] in DispatcherServlet with name 'bankapp' WARN org.springframework.web.servlet.PageNotFound-在DispatcherServlet中,名称为“ bankapp”的URI [/ ch11-session-attributes / fixedDeposit / list]找不到HTTP请求的映射

Can someone please explain me why? 有人可以解释一下为什么吗?

ps I am running the book's source code. ps我正在运行本书的源代码。 The book assumes that the code is perfect. 本书假定代码是完美的。 However it is not working. 但是,它不起作用。

I tried the example and its working like a charm. 我尝试了这个例子,它的工作就像一个魅力。
Make sure you have done the following 确保您已完成以下操作

1) Configure the viewNameTranslator 1)配置viewNameTranslator

    <bean id="viewNameTranslator" class="org.springframework.web.servlet.view.DefaultRequestTo‌​ViewNameTranslator"/‌​>

Where no view name is provided, the RequestToViewNameTranslator interface >determines the view name automatically. 如果未提供视图名称,则RequestToViewNameTranslator接口>将自动确定视图名称。 For this you need to configure >DefaultRequestToViewNameTranslator class in Spring MVC configuration file Taken from tutorial mentioned above 为此,您需要在Spring MVC配置文件中配置> DefaultRequestToViewNameTranslator类,摘自上述教程。

2) create a folder fixedDeposit under your WEB-INF/jsp folder (if you have configured some other View resolver than place the fixedDeposit folder inside that folder, for eg in my case I used freemarker (WEB-INF/templates), I hope it should work for jsp in the same way) 2)在WEB-INF / jsp文件夹下创建一个文件夹fixedDeposit(如果您配置了其他View解析器,而不是将fixedDeposit文件夹放置在该文件夹中,例如,在我的情况下,我使用了freemarker(WEB-INF / templates),我希望它应该以相同的方式适用于jsp)

RequestToViewNameTranslator this is a special bean that resolves the view name out of the request. RequestToViewNameTranslator这是一个特殊的Bean,可以从请求中解析视图名称。 By default the view name is resolved by removing the URI path from the request and removing the media type. 默认情况下,视图名称是通过从请求中删除URI路径并删除媒体类型来解析的。 So, for example, if the request is made to http://host:port/context/servlet/some/path/in/the/app.html then by default the view will be resolved to "app" - removing the path and the .html suffix. 因此,例如,如果请求发送到http:// host:port / context / servlet / some / path / in / the / app.html,则默认情况下,视图将解析为“ app”-删除路径和.html后缀。 therefore your view should be http://host:port/context/servlet/some/path/in/the/app 因此,您的视图应为http:// host:port / context / servlet / some / path / in / the / app

3) Your view file name should be list.jsp (list.ftl in my case) and place your view file with in the new WEB-INF\\jsp\\fixedDeposit folder 3)您的视图文件名称应为list.jsp(在我的情况下为list.ftl),并将其放置在新的WEB-INF \\ jsp \\ fixedDeposit文件夹中

This should work now. 现在应该可以使用了。 Next while positing your questions please give the complete background as well, for eg I would really like to see your application-context.xml file as well to help answer your question. 接下来,在提出您的问题时,请同时提供完整的背景知识,例如,我也很想看看您的application-context.xml文件,以帮助回答您的问题。 if you have root cause exceptions then more detailed stack trace would be more appreciated 如果您有根本原因异常,那么更详细的堆栈跟踪将更受赞赏

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM