SpringMVC_2 注解开发和非注解

1,注解开发

1.1认识一个适配器  SimpleControllerHandlerAdapter,

能够执行实现了Controller接口的Handler,所以我们编写的Handler需要实现Controller接口
复制代码

public class ItemsController1 implements Controller {

    @Override
    public ModelAndView handleRequest(HttpServletRequest request,
            HttpServletResponse response) throws Exception {

        //实际中是调用service查找数据库,查询商品列表,这里直接使用静态数据来模拟了
        List<Items> itemsList = new ArrayList<Items>();
        //向list中填充静态数据
        Items items_1 = new Items();
        items_1.setName("联想笔记本");
        items_1.setPrice(6000f);
        items_1.setDetail("ThinkPad T430 联想笔记本电脑!");

        Items items_2 = new Items();
        items_2.setName("苹果手机");
        items_2.setPrice(5000f);
        items_2.setDetail("iPhone6苹果手机!");

        itemsList.add(items_1);
        itemsList.add(items_2);

        //返回ModelAndView
        ModelAndView modelAndView = new ModelAndView();

        //相当于request的setAttribute,在jsp页面中通过itemsList取数据
        modelAndView.addObject("itemaList", itemsList);

        //指定视图
        modelAndView.setViewName("/WEB-INF/jsp/items/itemsList.jsp");

        return modelAndView;
    }
}
复制代码

1.2第二个适配器   org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter

Handler需要实现HttpRequestHandler接口
复制代码
public class ItemsController2 implements HttpRequestHandler {

    @Override
    public void handleRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        //实际中是调用service查找数据库,查询商品列表,这里直接使用静态数据来模拟了
        List<Items> itemsList = new ArrayList<Items>();
        //向list中填充静态数据
        Items items_1 = new Items();
        items_1.setName("联想笔记本");
        items_1.setPrice(6000f);
        items_1.setDetail("ThinkPad T430 联想笔记本电脑!");

        Items items_2 = new Items();
        items_2.setName("苹果手机");
        items_2.setPrice(5000f);
        items_2.setDetail("iPhone6苹果手机!");

        itemsList.add(items_1);
        itemsList.add(items_2);

        //设置模型数据
        request.setAttribute("itemsList", itemsList);

        //设置转发的视图
        request.getRequestDispatcher("/WEB-INF/jsp/items/itemsList.jsp").forward(request, response);

        //使用此方法可以通过修改response,设置响应的数据格式,比如响应json数据
        /*
        response.setCharacterEncoding("utf-8");
        response.setContentType("application/json;charset=utf-8");
        response.getWriter().write("json串");*/
    }
}
复制代码

ps:两者也就填充数据方式不一样

2.1 了解一下 处理器映射器 org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping

从名字就可以看出是根据bean的名字进行url查找复制代码

<bean name="/xxxx.action" class="xx.xx.xxController1"/>
复制代码

  2.2第二个处理器映射  org.springframework.web.servlet.handler.SimpleUrlHandlerMapping

直接根据url来查找Controller复制代码

<bean id="xxxController1" class="xx.xx.xxController1"/>

<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="/xx.action>xxxController1<prop>
</props>
</roperty>
</bean>复制代码

ps 仔细观察两者不同

2,注解方式

spring3.1之前

注解映射器:org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping 
注解适配器:org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter
复制代码

之后

注解映射器:org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping 
注解适配器:org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter
复制代码

这样小伙伴就会想,那怎么把url(xx.action)映射到Controller上了?

@Controller
public class ItemsController3 {

       // @RequestMapping实现 对queryItems方法和url进行映射,一个方法对应一个url
    // 一般建议将url和方法写成一样
    @RequestMapping("/queryItems")
    public ModelAndView queryItems() throws Exception {

        // 实际中是调用service查找数据库,查询商品列表,这里直接使用静态数据来模拟了
        List<Items> itemsList = new ArrayList<Items>();
        // 向list中填充静态数据
        Items items_1 = new Items();
        items_1.setName("联想笔记本");
        itemsList.add(items_2);

        // 返回ModelAndView
        ModelAndView modelAndView = new ModelAndView();

        // 相当于request的setAttribute,在jsp页面中通过itemsList取数据
        modelAndView.addObject("itemsList", itemsList);

        // 指定视图
        modelAndView.setViewName("/WEB-INF/jsp/items/itemsList.jsp");

        return modelAndView;
    }

    @RequestMapping("/queryItems2")
    public ModelAndView queryItems2() throws Exception {

        // 实际中是调用service查找数据库,查询商品列表,这里直接使用静态数据来模拟了
        List<Items> itemsList = new ArrayList<Items>();
        // 向list中填充静态数据
        Items items_1 = new Items();
        items_1.setName("联想笔记本2");
        itemsList.add(items_2);

        // 返回ModelAndView
        ModelAndView modelAndView = new ModelAndView();

        // 相当于request的setAttribute,在jsp页面中通过itemsList取数据
        modelAndView.addObject("itemsList", itemsList);

        // 指定视图
        modelAndView.setViewName("/WEB-INF/jsp/items/itemsList.jsp");

        return modelAndView;
    }
}
复制代码

继续简化 <mvc:annotion-driven></mvc:annotion-driven>代替

注解映射器:org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping 
注解适配器:org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter复制代码

但不要忘了,直接在类上加注解是没有用的,因为容器不知道 你还需要加

扫描包<context:component-scan base-package=""/>

实际开发中应该就是用注解了


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值