@Response注解使用

@ResponseBody
@Responsebody 注解表示该方法的返回的结果直接写入 HTTP 响应正文(ResponseBody)中,一般在异步获取数据时使用,通常是在使用 @RequestMapping 后,返回值通常解析为跳转路径,加上 @Responsebody 后返回结果不会被解析为跳转路径,而是直接写入HTTP 响应正文中。
作用:
该注解用于将Controller的方法返回的对象,通过适当的HttpMessageConverter转换为指定格式后,写入到Response对象的body数据区。
使用时机:
返回的数据不是html标签的页面,而是其他某种格式的数据时(如json、xml等)使用。例如(当请求发送http://localhost:9555/mavenweb/index.do时进入页面(此时该controller对应的方法不加ResponseBody,若加了,则不会进入页面,只有一个空白页,显示方法中返回的字符串),当加载该页面的时候会发送一个请求去请求后台数据显示在页面,此时要加RequestBody,因为我们的页面已经加载出来,要显示数据,所以我们返回到前端的因该是一个json字符串);
但是,例如在点击超链接删除一个用户后,controller方法返回值为string,如果不添加该注解,则页面不会自动跳转

@Controller
public class controller {

    @Autowired
   private CustomerService customerService;


    // 这个方法不是异步请求,但是我们要的是将对象转换成json数据返回到页面,是直接显示在了一个空白页面上,没有定义任何
    // 页面,可以用model+页面将数据显示到页面上,这时候应该就不需要response了
    // 所以就添加@responseBody这个注解,他可以将java对象转换成json数据,反之亦可
    @RequestMapping("/customer/list")
    @ResponseBody
   public Customer showCustomer(Model model){
        System.out.print("===============");
        // List<Customer> customerList  =  customerService.getCustomerList();
        Customer customer = customerService.getCustomer();

        System.out.print(customer+"===============");
     return customer;
    }

   // 异步请求就要@response
    //不加的话就会进行页面跳转

    // 这里如果添加了@responsebody
    // 结果:整个页面只有:"customer" 字符串
    // 因为@responsebody 返回的数据不是html页面而是其他格式的数据,注意它直接将你返回的东西即retur后的字符串
    // 返回到了页面,例如json,xml等,这里呢,我们自己将数据进行处理,然后在页面中显示,springmvc的视图解析器,会
    // 将数据解析然后放到页面模板里结合,与reponsebody没关系了
    @RequestMapping("/customer/list2")
    public String showCustomerList(Model model){
        System.out.print("LIST+++++++++++++++++++++");
        // List<Customer> customerList  =  customerService.getCustomerList();
        List<Customer> customerList = customerService.getCustomerList();
        model.addAttribute("customerList",customerList);
        System.out.print(customerList+"LIST+++++++++++++++++++++");
        return "customers";
    }

    //  在前台页面我们点击超链接删除,是$.post也就是ajax请求,即异步请求
    // 异步请求就的用response
    // 根据id删除用户,这里如果不加@ResponseBody则数据可以被删除,但是页面不会进行自动刷新。
    @RequestMapping("/customer/delete")
    @ResponseBody
    public String deleteCustomer(Long id){
        customerService.deleteCustomer(id);
        System.out.print("删除成功.....");
        return "OK";

    }
这里的ResponseBody会将java对象转换成json,前端是html,然后$.ajax的回调函数会将数据拿到
  @RequestMapping("/customer/ht")
    @ResponseBody
    public Customer htm(HttpServletResponse response) throws Exception{
        Customer customer = customerService.getCustomer();
        System.out.print("html..............");
       return customer;
    }


    // 点击按钮循环显示出所有数据到html页面,这里的@Response将数据转换成json格式,然后,就没它啥事了,到此这个controller方法就算是执行完毕了,然后前端的$.ajax的回调函数会拿到这个数据,然后进行操作
    @RequestMapping("/customer/hts")
    @ResponseBody
    public List<Customer> htms(){
        List<Customer> customerList = customerService.getCustomerList();
        System.out.println(customerList);
        return customerList;
    }


}

html:

 $.ajax({
            type:"get",
            url:"/mavenweb/customer/ht.do",
            dateType:"json",
            success:function (data) {
                var retData = eval("(" + data + ")");
                $("#username").val(retData.cust_name);
                $("#address").val(retData.cust_address);
            }
        });

注:如果加@Requestmapping,然后返回值为 return “a”,那么它会将数据封装到页面,然后跳转到一个名字为a的页面。

如果@RequestMapping,又添加了@ResponseBody,如果此时返回值仍然为“a”,那么它就不会跳转页面了,只在一个空白页面显示a这个字符。

如果要想在这连个注解同时存在的情况下,跳转页面(就不要返回“字符串”),即返回值是什么类型,return什么类型,如 return costomer(对象)。此时的requestMapping,将数据准换成json,返回给前台。

总结:执行跳转页面功能的为requestmapping而非responseBody。即通常是在使用 @RequestMapping 后,返回值通常解析为跳转路径,加上 @Responsebody 后返回结果不会被解析为跳转路径,而是直接写入HTTP 响应正文中

  • 12
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 6
    评论
@Excel注解是Spring Boot中的一个注解,用于将Java对象映射到Excel表格。它可以帮助开发人员快速地实现将数据导出为Excel文件的功能。 @Excel注解有以下几个属性: 1. name:指定Excel表格的名称。 2. orderNum:指定Excel表格的顺序。 3. width:指定Excel表格的宽度。 4. isImportField:指定是否导入该字段,默认为true。 5. isExportField:指定是否导出该字段,默认为true。 6. dateFormat:指定日期格式化。 7. replace:指定替换内容。 使用@Excel注解的步骤如下: 1. 在实体类中添加@Excel注解,指定属性的名称、顺序、宽度等。 2. 使用EasyExcel或其他Excel操作库,将数据导出为Excel文件。 3. 使用EasyExcel或其他Excel操作库,将Excel文件导入为Java对象。 以下是一个使用@Excel注解的示例: ``` public class User { @Excel(name = "姓名", orderNum = "0", width = 20) private String name; @Excel(name = "年龄", orderNum = "1", width = 20) private Integer age; // 省略getter和setter方法 } ``` 在上面的示例中,我们使用@Excel注解指定了User类中的name和age属性在Excel表格中的名称、顺序和宽度。 使用EasyExcel导出User对象为Excel文件的代码如下: ``` @Component public class UserExcelService { public void export(List<User> userList) { try { String fileName = "user.xlsx"; ServletOutputStream out = response.getOutputStream(); response.setContentType("multipart/form-data"); response.setHeader("Content-Disposition", "attachment;fileName=" + fileName); ExcelWriter writer = EasyExcel.write(out).build(); WriteSheet sheet = EasyExcel.writerSheet("用户信息").build(); writer.write(userList, sheet); writer.finish(); } catch (Exception e) { e.printStackTrace(); } } } ``` 使用EasyExcel将Excel文件导入为User对象的代码如下: ``` @Component public class UserExcelService { public List<User> import(MultipartFile file) { try { InputStream in = file.getInputStream(); List<User> userList = EasyExcel.read(in).head(User.class).sheet().doReadSync(); return userList; } catch (Exception e) { e.printStackTrace(); } return null; } } ```
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值