首先配置注解扫描(该包扫描必须放在springmvc配置文件中,如果放在spring配置文件中,则扫描不到)
<context:component-scan base-package="com.test.controller"/>
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
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/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<context:component-scan base-package="com.test"/>
<!--注解驱动,相当于配置HandlerMapping及HandlerAdapter-->
<mvc:annotation-driven></mvc:annotation-driven>
<!--放行静态资源-->
<!--一个*表示该文件夹下的所有子文件,**表示该文件夹的所有子文件及子文件夹下的内容-->
<!--location表示文件资源所在路径,mapping表示请求路径-->
<mvc:resources mapping="/js/**" location="/WEB-INF/js/"/>
<mvc:resources mapping="/css/**" location="/WEB-INF/css/"/>
<mvc:resources mapping="/images/**" location="/WEB-INF/images/"/>
</beans>
controller类:
package com.test.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class DemoController {
// String表示跳转到哪个页面
@RequestMapping("demo")
public String demo() {
return "main.jsp";
}
}