zoukankan      html  css  js  c++  java
  • springmvc注解开发

    一、@RequestMapping作用

      ①、url映射

    //比如:商品列表:/items/queryItems.action
    @Controller
    @RequestMapping("/items")
    public class ItemsController {
    
        // 商品查询
        @RequestMapping("/queryItems")
        public ModelAndView queryItems() throws Exception {

      ②、限制http请求方法(出于安全性考虑,对http的链接进行方法限制)如:请求为post和get方式

        @RequestMapping(value="/queryItems",method = {RequestMethod.POST,RequestMethod.GET})
        public ModelAndView queryItems() throws Exception {

    二、Controller返回值

      ①、ModelAndView

        需要定义ModelAndView,将model和view分别进行设置。

        public ModelAndView queryItems() throws Exception {
            // 调用service查找 数据库,查询商品列表
            List<ItemsCustom> itemsList = itemsMapperCustomService.findItemsList();
    
            // 返回ModelAndView
            ModelAndView modelAndView = new ModelAndView();
            // 相当 于request的setAttribut,在jsp页面中通过itemsList取数据
            modelAndView.addObject("itemsList", itemsList);
    
            // 指定视图
            modelAndView.setViewName("items/itemsList");
    
            return modelAndView;
        }
    View Code

      ②、String

        2.1、逻辑视图名

          真正视图(jsp路径)=前缀+逻辑视图名+后缀

        public String editItems(Model model) throws Exception {
            
            ItemsCustom itemsList = itemsMapperCustomService.findItemsById(1);
            
            model.addAttribute("itemsList", itemsList);
            return "items/editItems";
        }

        2.2、redirect重定向

          redirect重定向特点:浏览器地址栏中的url会变化,无法共享request

            //RESTful风格的url
            return "redirect:queryItems";

        2.3、forward页面转发

          forward页面转发特点:浏览器地址栏url不变,共享request

        @RequestMapping("/editItems")
        public String editItems() throws Exception {
            //RESTful风格的url
            return "forward:queryItems";
        }
    
        @RequestMapping(value="/queryItems",method = {RequestMethod.POST,RequestMethod.GET})
        public ModelAndView queryItems(HttpServletRequest request ) throws Exception {
            //测试request共享
            System.out.println(request.getParameter("id"));
            
            .......
            return modelAndView;
        }    

          访问地址:http://localhost:8080/03springmvc/items/editItems?id=1

          结果:

          

      ③、void

        在controller方法形参上可以定义request和response,使用request或response指定响应结果:

    public void xxxfunc(HttpServletRequest request,HttpServletResponse) throws Exception {

        1、使用request转向页面,如下:

          request.getRequestDispatcher("页面路径").forward(request, response); 

        2、也可以通过response页面重定向:

          response.sendRedirect("url") 

        3、也可以通过response指定响应结果,例如响应json数据如下:

          response.setCharacterEncoding("utf-8");

          response.setContentType("application/json;charset=utf-8");

          response.getWriter().write("json串");

    三、参数绑定

      spring参数绑定过程

        从客户端请求key/value数据,经过参数绑定组件,将key/value数据绑定到controller方法的形参上。

      参数绑定组件

        PropertyEditor:只能将字符串传成java对象(早期)

        Converter:进行任意类型的转换

      默认支持类型 -- controller方法形参对象的类型,不需要实例化可以直接使用形参对象

      • HttpServletRequest:通过request对象获取请求信息
      • HttpServletReponse:通过response处理响应信息
      • HttpSession:通过session对象得到session中存放的对象
      • Model/ModelMap:将model数据填充到request域

      简单类型

        request传入参数名称和controller方法的形参名称一致,如请求:http://xxxx/user/findUserById?id=1,controller方法的参数:xxx findUserById(Integer id){......}

        要是不一样的话需要使用@RequestParam

        @RequestParam的属性:

        • value:值为request传入参数名称
        • required:参数是否必须要传入
        • defaultValue:设置默认值
    public String editItems(Model model,@RequestParam(value="id",required=true) Integer items_id)throws Exception {

      POJO绑定

        页面中input的name和controller的pojo形参中的属性名称一致,将页面中数据绑定到pojo

        页面:

    <tr>
        <td>商品名称</td>
        <td><input type="text" name="name" value="${itemsCustom.name }"/></td>
    </tr>
    <tr>
        <td>商品价格</td>
        <td><input type="text" name="price" value="${itemsCustom.price }"/></td>
    </tr>

        POJO:

    public class ItemsCustom {
    
        private Integer id;
    
        private String name;
    
        private Float price;
    
        private String pic;

        Controller方法:

    public String editItems(ItemsCustom items) throws Exception {

    参数类型之间互不影响,比如:页面上有name、price的输入框,方法是public String editItems(String name,float price,ItemsCustom items) throws Exception {,无论是items对象还是name、price参数都会绑定相应的值。

    四、Converter应用(自定义参数绑定组件)

      ①、实现日期类型绑定

    public class CustomDateConverter implements Converter<String,Date>{
    
        @Override
        public Date convert(String source) {
            
            //实现 将日期串转成日期类型(格式是yyyy-MM-dd HH:mm:ss)
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            
            try {
                //转成直接返回
                return simpleDateFormat.parse(source);
            } catch (ParseException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            //如果参数绑定失败返回null
            return null;
        }
    
    }
    View Code

      ②、向处理器适配器中注入自定义的参数绑定组件

    <mvc:annotation-driven conversion-service="conversionService"></mvc:annotation-driven>
        <!-- 自定义参数绑定 -->
        <bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
            <!-- 转换器 -->
            <property name="converters">
                <list>
                    <!-- 日期类型转换 -->
                    <bean class="com.xxx.springmvc.controller.converter.CustomDateConverter"/>
                </list>
            </property>
        </bean>
  • 相关阅读:
    回归测试
    系统测试
    单元测试
    软件测试规律之木桶原理
    集成测试
    软件测试度量
    测试用例设计方法之错误推测法
    测试用例设计方法之因果图方法
    有趣的算法:1元=1分
    【转】 arcServer 一些 FAQ
  • 原文地址:https://www.cnblogs.com/WarBlog/p/15019932.html
Copyright © 2011-2022 走看看