警告: No mapping found for HTTP request with URI [/user/test] in DispatcherServlet with name 'springDi

文章目录

  • 报错
  • 原因
  • 代码

报错

五月 20, 2019 5:41:55 下午 org.springframework.web.servlet.DispatcherServlet noHandlerFound
警告: No mapping found for HTTP request with URI [/user/test] in DispatcherServlet with name 'springDispatcherServlet'

在这里插入图片描述
警告: No mapping found for HTTP request with URI [/user/test] in DispatcherServlet with name 'springDi_第1张图片

原因

警告: No mapping found for HTTP request with URI [/user/test] in DispatcherServlet with name 'springDi_第2张图片
警告: No mapping found for HTTP request with URI [/user/test] in DispatcherServlet with name 'springDi_第3张图片
应该将applicationContext.xml(我将springmvc的配置也放在了spring配置文件中)放到resource目录下面
原来在src下面,在web.xml中的配置就没有读取到applicationContext.xml配置文件,springmvc的扫描也没有找到。

在maven的项目中springmvc的配置文件放在resource下面后者WEB-INF下面

代码

@Controller
@RequestMapping("/user")
public class UserController {

	@Autowired
	private UserService userService;

	@RequestMapping("/test")
	public String test(User user,ModelMap model){
		userService.save(user);
		model.addAttribute(user);
		return "/return";
	}

}

web.xml

<!--springmvc-->
    <servlet>
        <servlet-name>springDispatcherServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath*:applicationContext.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>springDispatcherServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

applicationContext.xml

 <context:component-scan base-package="com.test"/>

    <!--视图层解析器-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="suffix" value=".jsp"/>
    </bean>

你可能感兴趣的:(#,JAVA,------,SSH,SSM,Servlet,#,Tool,------,Maven,Gradle)