1.springmvc的配置檔案中添加注解支援
<?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">
<!--自動掃描包,讓指定包下的注解生效,由ICO容器統一管理-->
<context:component-scan base-package="com.lixv.controller"/>
<!--讓SpringMVC不處理靜态資源-->
<mvc:default-servlet-handler/>
<!--注解支援-->
<mvc:annotation-driven/>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="internalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
2.通過注解寫controller
package com.lixv.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping("/aaa")
public class HelloController01 {
@RequestMapping("/a1")
public String a1(Model model){
model.addAttribute("msg","hello,SpringMVCAnnotation");
return "test";
}
}
- @Controller将此類作為controller傳入springmvc中管理
- @RequestMapping注解的值為請求路徑,如圖a1方法對應的請求路徑為/aaa/a1
-
相當于model.addAttribute
request.setAttribute
- return的字元串内容傳入InternalResourceViewResolver的bean中進行加工,變成傳回的頁面的路徑