首先配置注解掃描(該包掃描必須放在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"/>
<!--注解驅(qū)動射沟,相當于配置HandlerMapping及HandlerAdapter-->
<mvc:annotation-driven></mvc:annotation-driven>
<!--放行靜態(tài)資源-->
<!--一個*表示該文件夾下的所有子文件碰煌,**表示該文件夾的所有子文件及子文件夾下的內(nèi)容-->
<!--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表示跳轉(zhuǎn)到哪個頁面
@RequestMapping("demo")
public String demo() {
return "main.jsp";
}
}