spring-webmvc-4.24.RELAEASE.jar包中
DispatcherServlet.properties文件
处理器映射器 :
org.springframework.web.servlet.HandlerMapping=org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping,\
org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping
处理器适配器
org.springframework.web.servlet.HandlerAdapter=org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter,\
org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter,\
org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter
视图解析器
org.springframework.web.servlet.ViewResolver=org.springframework.web.servlet.view.InternalResourceViewResolver
这三个类都需要在springmvc中配置,处理器映射器绑定与请求路径相关;处理器适配器帮我们实例化Controller类并且调用方法。springMVC入门请求,必须配有这两个才可以成功访问。视图解析器配置前后缀简化访问;
比如:如下图:在springmvc.xml中配置前后缀后,简化mv.setVewName()方法。
package com.itheima.springmvc.controller;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import com.itheima.springmvc.pojo.Item;
@Controller
public class itemController {
@RequestMapping("/hello/list")
public ModelAndView list(){
ModelAndView mv = new ModelAndView();
//创建集合
List<Item> list = new ArrayList<Item>();
Item item1 = new Item(1, 2, new Date(), "1", "2");
Item item2 = new Item(2, 2, new Date(), "1", "2");
Item item3 = new Item(3, 2, new Date(), "1", "2");
Item item4 = new Item(4, 2, new Date(), "1", "2");
list.add(item1);
list.add(item2);
list.add(item3);
list.add(item4);
//设置数据模型,用于传递到jsp
mv.addObject("itemList", list);
//设置视图名称,用户响应用户
// mv.setViewName("/WEB-INF/jsp/itemList.jsp");
mv.setViewName("itemList");
return mv;
}
}
springmvc.xml配置文件如下:
<?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:p="http://www.springframework.org/schema/p"
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-4.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<!-- 配置@Controller处理器,包扫描器 -->
<context:component-scan base-package="com.itheima.springmvc.controller" />
<!-- 配置注解驱动,相当于同时使用最新处理器映射起和处理器适配器 -->
<mvc:annotation-driven/>
<!-- 配置视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>