gpt4 book ai didi

java - Spring MVC Controller 映射未注册

转载 作者:搜寻专家 更新时间:2023-11-01 03:13:50 26 4
gpt4 key购买 nike

这几天我正在用 spring mvc 做一些测试,我遇到了一个问题。对于要测试的基本应用程序,我选择了本教程: http://loianegroner.com/2010/09/extjs-spring-mvc-3-and-hibernate-3-5-crud-datagrid-example/我已经下载了示例,一切都按预期工作。问题是当我尝试添加另一个 Controller 时。似乎我添加的文件未被扫描。

我的 web.xml 文件:

<servlet>
<servlet-name>extjs-crud-grid-spring-hibernate</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/spring/app-config.xml
</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>extjs-crud-grid-spring-hibernate</servlet-name>
<url-pattern>*.action</url-pattern>
</servlet-mapping>

/WEB-INF/spring/app-config.xml:

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

<!-- Scans the classpath of this application for @Components to deploy as beans -->
<context:component-scan base-package="com.loiane" />

<!-- Configures the @Controller programming model -->
<mvc:annotation-driven />

<!-- misc -->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="suffix" value=".jsp"/>
</bean>

<!-- Configures Hibernate - Database Config -->
<import resource="db-config.xml" />

如您所见,有 context:component-scan 和 mvc:annotation-driven 标签,它们应该告诉 spring 扫描 com.lione 包中的所有类以获取 @Controller 注释。但是当我尝试将我自己的 Controller 添加到这个应用程序时,它似乎没有被处理。仅映射第一个 Controller 。

这是我的 Controller 代码

    package com.loiane.web;


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


@Controller
public class HelloController {

@RequestMapping("/helloWorld.action")
public ModelAndView helloWorld() {
ModelAndView mav = new ModelAndView();
mav.setViewName("helloWorld");
mav.addObject("message", "Hello World!");
return mav;
}
}

在 eclipse 控制台日志中运行它之后我得到了

INFO: Mapped URL path [/contact/delete.action] onto handler 'contactController'
INFO: Mapped URL path [/contact/create.action] onto handler 'contactController'
INFO: Mapped URL path [/contact/update.action] onto handler 'contactController'
INFO: Mapped URL path [/contact/view.action] onto handler 'contactController'

但是缺少/helloWorld.action

提前致谢

最佳答案

有时在使用 <context:component-scan base-package="package-name" /> 时更具体地说明您的路径是个好主意所以你应该做 <context:component-scan base-package="com.loiane.web" />

关于java - Spring MVC Controller 映射未注册,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4246831/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com