• SPRING IN ACTION 第4版笔记-第五章BUILDING SPRING WEB APPLICATIONS-005-以path parameters的形式给action传参数(value=“{}”、@PathVariable)


    1.以path parameters的形式给action传参数

     1 @Test
     2   public void testSpittle() throws Exception {
     3     Spittle expectedSpittle = new Spittle("Hello", new Date());
     4     SpittleRepository mockRepository = mock(SpittleRepository.class);
     5     when(mockRepository.findOne(12345)).thenReturn(expectedSpittle);
     6     
     7     SpittleController controller = new SpittleController(mockRepository);
     8     MockMvc mockMvc = standaloneSetup(controller).build();
     9 
    10     mockMvc.perform(get("/spittles/12345"))
    11       .andExpect(view().name("spittle"))
    12       .andExpect(model().attributeExists("spittle"))
    13       .andExpect(model().attribute("spittle", expectedSpittle));
    14   }

    在浏览器中访问:http://localhost:8080/SpringInAction4_Chapter5_SpringMVC01/spittles/1

    2.controller

    package spittr.web;
    
    import java.util.Date;
    import java.util.List;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Controller;
    import org.springframework.ui.Model;
    import org.springframework.web.bind.annotation.PathVariable;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    import org.springframework.web.bind.annotation.RequestParam;
    
    import spittr.Spittle;
    import spittr.data.SpittleRepository;
    
    @Controller
    @RequestMapping("/spittles")
    public class SpittleController {
    
      private static final String MAX_LONG_AS_STRING = "9223372036854775807";
      
      private SpittleRepository spittleRepository;
    
      @Autowired
      public SpittleController(SpittleRepository spittleRepository) {
        this.spittleRepository = spittleRepository;
      }
    
    
      @RequestMapping(value="/{spittleId}", method=RequestMethod.GET)
      public String spittle(
          @PathVariable("spittleId") long spittleId, 
          Model model) {
          //The model key will be  spittle 
        model.addAttribute(spittleRepository.findOne(spittleId));
        return "spittle";
      }
    
    }

    在保证spittleId名称一致的情况下,可以简写

    1   @RequestMapping(value="/{spittleId}", method=RequestMethod.GET)
    2   public String spittle(
    3       @PathVariable long spittleId, 
    4       Model model) {
    5       //The model key will be  spittle 
    6     model.addAttribute(spittleRepository.findOne(spittleId));
    7     return "spittle";
    8   }

    3.view

     1 <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
     2 <html>
     3   <head>
     4     <title>Spitter</title>
     5     <link rel="stylesheet" 
     6           type="text/css" 
     7           href="<c:url value="/resources/style.css" />" >
     8   </head>
     9   <body>
    10     <div class="spittleView">
    11       <div class="spittleMessage"><c:out value="${spittle.message}" /></div>
    12       <div>
    13         <span class="spittleTime"><c:out value="${spittle.time}" /></span>
    14       </div>
    15     </div>
    16   </body>
    17 </html>
  • 相关阅读:
    翻译一下libtiff的手册
    LIBTIFF读写黑白TIFF
    LIBTIFF存储代码,存图、拼图
    几点TIFF的说明
    TIFF Tag TileWidth
    TIFF Tag Orientation
    OpenCV 加速图像处理
    Qt QString 、String、char* 三者之间相互转换
    PackBits解压缩算法
    关于LZW算法的压缩与解压缩
  • 原文地址:https://www.cnblogs.com/shamgod/p/5242530.html
Copyright © 2020-2023  润新知