web.xml
- 映射器和适配器的bean不用配置了,改用扫描指定路径的包和并把带有注解的类或方法生成bean注入容器中
- url和controller之间的映射不用配置成bean了,直接在对应的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.kuang.controller"/>
<mvc:default-servlet-handler/>
<mvc:annotation-driven/>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
controller
- 实现Controller接口转变成加上@Controller注解
- url和controller之间的映射也用注解实现
- 执行的方法参数不再是ModelAndView,而是Model,设置数据即可,不用设置ViewName,而return的字符串就是视图的名称。
@Controller
public class HelloController{
@RequestMapping("/hello")
public String hello(Model mv){
mv.addAttribute("msg", "hello执行了");
return "hello";
}
}