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">
<context:component-scan base-package="com.lixv.controller"/>
<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中进行加工,变成返回的页面的路径