SpringMVC入门(二)

使用注解的方式进行Handler的开发

  注意:此处只介绍和方式一不同的地方

1、注解的处理器适配器

 在spring3.1之前使用org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter注解适配器。

 在spring3.1之后使用org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter注解适配器

2、注解的处理器映射器

 在spring3.1之前使用org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping注解映射器。

 在spring3.1之后使用org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping注解映射器。

3、配置注解的处理器适配器

  <!---第一种配置方式-->
    <!--配置注解的处理器适配器-->
    <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter" />

4、配置注解的处理器映射器

 <!---第一种配置方式-->
 <!--配置注解的处理器映射器-->
    <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping" />

5、配置处理器适配器和映射器的另一种方式

<!--第二种配置方式-->
<!-- 使用 mvc:annotation-driven代替上边注解映射器和注解适配器配置
mvc:annotation-driven默认加载很多的参数绑定方法,
比如json转换解析器就默认加载了,如果使用mvc:annotation-driven不用配置上边的RequestMappingHandlerMapping和RequestMappingHandlerAdapter
实际开发时使用mvc:annotation-driven
 -->
<!--<mvc:annotation-driven></mvc:annotation-driven>-->

6、Handler的编写

 使用注解的映射器和注解的适配器。(注解的映射器和注解的适配器必须配对使用),在Handler文件中只要添加@Controller注解即可。

//使用Controller注解,标明本类是一个Handler
@Controller
public class TestController {

    //使用@RequestMapping进行url映射
    @RequestMapping("/queryItems")
    public ModelAndView queryItems() throws Exception {
        //调用service中的方法去数据库查询数据,这里采用静态数据进行模拟
        List<Items> itemsList = new ArrayList<>();
        //静态模拟数据
        Items items1 = new Items();
        items1.setId(1);
        items1.setName("笔记本电脑");
        items1.setPrice(5000.0f);

        Items items2 = new Items();
        items2.setId(2);
        items2.setName("鼠标");
        items2.setPrice(50.0f);

        itemsList.add(items1);
        itemsList.add(items2);

        //创建ModelAndView对象
        ModelAndView modelAndView = new ModelAndView();
        //填充model
        modelAndView.addObject("itemsList",itemsList);
        //填充view
        modelAndView.setViewName("/page/itemlist.jsp");
        return modelAndView;
    }

}

7、配置Handler

 此处使用组件扫描的方式配置Handler。

 <!--使用组件扫描的方式配置Handler-->
<!-- 对于注解的Handler可以单个配置 实际开发中建议使用组件扫描-->
<!-- <bean class="cn.itcast.ssm.controller.ItemsController3" /> -->
<!-- 可以扫描controller、service、...
这里让扫描controller,指定controller的包
 -->
<context:component-scan base-package="jack.controller" ></context:component-scan>

8、测试结果

 在浏览器访问如下url;http://localhost:8080/项目名称/queryItems.action

9、配置视图解析器的前缀和后缀

 通过配置视图解析器的前缀和后缀可以简化在Handler中view的书写。配置方式如下:

<!--配置视图解析器,解析jsp页面,默认使用jstl标签-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" >
        <!--配置视图解析器的前缀-->
        <property name="prefix" value="/page/"/>
        <!--配置视图解析器的后缀-->
        <property name="suffix" value=".jsp"/>
    </bean>

相应的,Handler中的view变为如下:

//没有配置前缀和后缀
modelAndView.setViewName("/page/itemlist.jsp");
//配置了前缀和后缀
modelAndView.setViewName("itemlist");
posted @ 2017-08-14 14:13  一条路上的咸鱼  阅读(250)  评论(0编辑  收藏  举报