[英]Why Spring doesn't scan my controller
I want to learn something about Spring. 我想学习一些有关Spring的知识。 I'm trying to configure it but there is something I don't get because it seems that Spring doesn't recognize my controller. 我正在尝试配置它,但是有些东西我没有得到,因为似乎Spring无法识别我的控制器。
This is my web.xml 这是我的web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>dispatcher</display-name>
<welcome-file-list>
<welcome-file>index.jsp</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>
and this one is the servlet 这是servlet
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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">
<context:component-scan base-package="it.mexpenses.controller" />
<mvc:annotation-driven />
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
<property name="order" value="1" />
</bean>
</beans>
Controller: 控制器:
package it.mexpenses.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class LoginController {
@GetMapping("/login")
public ModelAndView login() {
User user = loginService.login(username, password);
return new ModelAndView("welcome", "currentUser", null);
}
}
In a typical spring MVC app you will find that there are two spring configuration files, a file that configures the application context usually started with the Spring context listener. 在典型的spring MVC应用程序中,您会发现有两个spring配置文件,一个配置应用程序上下文的文件通常从Spring上下文侦听器开始。
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
And a Spring MVC configuration file usually started with the Spring dispatcher servlet. 而且,Spring MVC配置文件通常从Spring调度程序Servlet开始。 For example. 例如。
<servlet>
<servlet-name>main</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>main</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
I strongly suggest the use of annotations for the Spring configuration. 我强烈建议对Spring配置使用批注。 It makes life much easier and is much more modern ... 它使生活更加轻松,并且更加现代...
You can also generate a project with spring boot and make your life easier: https://start.spring.io/ 您还可以使用spring boot生成一个项目,使您的生活更轻松: https : //start.spring.io/
Refs: Spring MVC: difference between <context:component-scan> and <annotation-driven /> tags? 参考: Spring MVC:<context:component-scan>和<annotation-driven />标记之间的区别?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.