【问题标题】:How to invoke controller methods in Spring MVC如何在 Spring MVC 中调用控制器方法
【发布时间】:2017-07-07 09:35:18
【问题描述】:

当导航到我的网络应用程序的根目录时,我想点击控制器的 readTasks 方法。我在方法中设置了一个断点,但在服务器上调试时我没有点击它。我正在使用 Eclipse。

我导航到:http://localhost:8080/ToDoList/,我看到了我的索引页面,但没有调用控制器方法。

我的控制器:

import java.util.List;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class TaskController {

    @RequestMapping(value = "/", method = RequestMethod.GET)
    public List<TaskEntity> readTasks() 
    {
        TaskEntityDao tasks = new TaskEntityDaoImpl();
        return tasks.getAllTasks();
    }
}

我的 web.xml:

<web-app version="2.4"
     xmlns="http://java.sun.com/xml/ns/j2ee"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
     http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" >

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

    <servlet>
        <servlet-name>Dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

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

</web-app>

我相信我缺少一些初始化控制器的配置,但我不知道该怎么做。我是否需要一个包含应用程序中每个控制器的初始化的配置文件?

【问题讨论】:

  • 你有spring的配置文件吗?
  • 我不知道 - 我意识到这是非常基本的东西,但我真的不明白如何编写它来初始化我的控制器......
  • 在你的控制器中添加一个 postconstruct 方法并在其中放置一个断点来查看控制器是否正在初始化
  • 尝试将@RequestMapping("/ToDoList") 添加到您的控制器

标签: java spring-mvc model-view-controller requestdispatcher


【解决方案1】:

您需要在 web.xml 中进行 Spring MVC 配置:

<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/spring/mvc-dispatcher-servlet.xml</param-value>
    </init-param>
</servlet>

然后在WEB-INF文件夹下创建mvc-dispatcher-servlet.xml。

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd ">

    <!-- enable to scan spring annotations, specify on web package -->
    <context:component-scan base-package="id.swhp.spring.web"/>
    <!-- Enable Spring MVC Anotations -->
    <mvc:annotation-driven/>

</beans>

【讨论】:

  • 感谢您的回答 - 在 web.xml 中它引用了“dispatcher-config.xml”,但随后您说要制作“mvc-dispatcher-servlet.xml”。这些不应该命名相同吗?我在base-package=[mypackage] 中使用我的包名尝试了这个,但它仍然没有初始化我的控制器。控制器是否需要注解?谢谢
  • 哎呀我刚从 spring 文档复制,不知道它有不同的命名,但是是的,它应该在你的控制器类上具有相同的名称(已经编辑了我的帖子)给 @Controller 或 @RestController` 注释.
  • 谢谢你,很好的答案,但我仍然没有击中我的控制器。我在base-package 中指定了我的控制器所在的包,并且我的控制器用@RestController 进行了注释。
  • 您是否已经配置了您的applicationContext.xml?也许你可以看到我的Spring项目配置,尤其是my github上的applicationContext.xml、mvc-dispatcher.xml和web.xml
  • 谢谢,我现在看看:)
【解决方案2】:

您需要编写一个 Spring 配置文件,其中包括对控制器所在包的组件扫描。这会告诉 Spring 在 Spring 上下文加载时初始化此控制器。然后你需要将你的 servlet 指向这个配置:

<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/spring/dispatcher-config.xml</param-value>
    </init-param>
</servlet>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-09-12
    • 1970-01-01
    • 2021-07-26
    • 2014-11-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多