java - Spring默认页面不经过Controller类

标签 java spring jsp spring-mvc

在 mvc-view-config.xml 中,我有 <mvc:view-controller path="/" view-name="countrys/countryList" />在 Controller 中我有下面的代码。这个想法是在站点首次加载时加载countrys/countryList.jsp。问题是它显示了正确的 jsp 页面,但代码没有在 Controller 类中调用此 showCountryList() 。但是,如果我只执行 mysite.com/countrys 或 mysite.com/contrys/contryList,我可以看到该页面,并且代码也调用了 showCountryList() 函数。我错过了什么?

 @RequestMapping(value = {"/countrys", "/countrys/countryList"})
    public String showCountryList(Map<String, Object> model) {
        // Here we are returning an object of type 'Vets' rather than a collection of Vet objects 
        // so it is simpler for Object-Xml mapping

        System.out.println("---- in here-----------");

        Collection<Country> results = this.clinicService.getCountry();
        model.put("selections", results);

        return "countrys/countryList";        
    }

最佳答案

此配置元素

<mvc:view-controller .../>

简单地注册一个ParameterizableViewController其中

[...] always returns a named view.

并且您已将其映射到/。因此,当您向 / 发送请求时,将使用该 Controller ,而不是包含您所显示的处理程序方法的 Controller 。

我不明白为什么您需要该配置元素,只需将处理程序方法映射到 / 除了其他路径之外

@RequestMapping(value = {"/", "/countrys", "/countrys/countryList"})
<小时/>

此外,“国家”的复数形式是“国家”。

关于java - Spring默认页面不经过Controller类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22951785/

相关文章:

java - 在 Spring 中使用日期创建 JPA 查询

spring - 由于 Tomcat ServerContainer,Spring-Websocket 的 ClassCastException

java - Gradle 任务 --help 打印到控制台而不是我的应用程序执行

java - Regexp::Assemble 的 Java 等价物是什么?

java - 一定时间间隔后更新文本字段

java - 带语言环境的 DecimalFormatSymbols

javascript - 如何在点击链接时创建 iframe?

java - 在 OpenCMS 网站上更改域名

java - struts2 ajax调用响应总是显示null

java - 二维数组转单数组