【SpringMVC中视图与Ajax请求】

一、SpringMVC中的视图

pringMVC 中 的 视图 是 View 接 口 , 视 图 的 作用 泻 染 数 据 , 将 模型 Model 中 的 数据 展示 给 用 户
SpringMVC 视 图 的 种 类 很 多 , 默 认 有 转发视图重 定向视图
当 工 程 引入 jstl 的 依赖 , 转 发 视图 会 自动 转换 为 jstlView

若 使 用 的 视图 技术 为 Thymeleaf, 在 SpringMVC 的 配置 文件 中 配置 了 Thymeleaf 的 视图 解析 器 , 由 此 视图 解析 器 解
析 之 后 所 得 到 的 是 ThymeleafView

1、ThymeleafView视图

当控制器方法中设置的视图名称没有任何前缀,会被之前SpringMVC中配置的Thymeleaf解析,拼接前后缀,进行转发

@RequestMapping("/param")
public String getParam(User user){
    return "success";
}

2、内部转发视图(不常用)

前缀加 forward:

@RequestMapping("/view/forward")
public String InternalResourceView(){
    return "forward:/param";
}

3.、重定向视图

前缀加 redirect:

@RequestMapping("/view/forward")
public String InternalResourceView(){
    return "redirect:/param";
}

4、视图控制器

仅用来使用页面跳转,可以将处理器方法使用view-controller标签表示

<!--  开启mvc的主键驱动  -->
<mvc:annotation-driven/>

<mvc:view-controller path="/" view-name="index"></mvc:view-controller>

可以在springmv.xml文件中添加

path:处理路径

view-name:跳转路径的逻辑视图

二、SpringMVC处理ajax请求

1、axios的使用介绍

封装了ajax,使用方法类似

​ data是以JSON格式进行封装

axios({
    url:"",
    method:"",
    params:{},
    data:{}
}).then(response=>{
    console.log(response.data);
})

一般使用axios.get/axios.post

image-20220815163228741

  • axios.get
  • axios.post

二者使用唯一区别是post可以传递data格式(JSON)参数,其他都一样

axios.post(
    //这里注意因为是js代码,需要写全路径
    "/SpringMVC/ajax?id=1",
    {username:"admin",password:"123"}
).then(response=>{
    console.log(response.data());
});

2、获取ajax发送的data数据@RequestBody

1.从请求体中获取data(数据在上方)

data中的数据是在请求体中以JSON格式存储的

@RequestBody获取请求体,如下

此处id自动装配为 /SpringMVC/ajax?id=1中的id值

@RequestMapping("/ajax")
public void testAjax(Integer id, @RequestBody String requestBody, HttpServletResponse response) throws IOException {
        System.out.println(requestBody);
        System.out.println("id:"+id);
        response.getWriter().print("hello,ajax");
}

请求体中传递的JSON格式数据

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-pzYqMVfy-1660701613737)(…/…/myimage/image-20220815205223937.png)]

2.处理JSON数据,使其变为java对象

1.导入Jackson依赖

2.开启注解驱动mvc:annotation-driven/

3.处理请求的参数位置,设置对应格式的请求参数

使用@RequestBody注解标志

testJson(){
    axios.post(
        "/SpringMVC/ajax/json",
        {username:"admin",password:"123"}
    ).then(response=>{
        console.log(response.data);
    })
}

如果有实体类(User):

@RequestMapping("/ajax/json")
public void testJson(@RequestBody User user,HttpServletResponse response) throws IOException {
    System.out.println(user);
    response.getWriter().write("hello,json");
}

如果没有实体类(Map)

@RequestMapping("/ajax/json")
public void testJson(@RequestBody Map<String,Object> map, HttpServletResponse response) throws IOException {
    System.out.println(map);
    response.getWriter().write("hello,json");
}

3、@ResponseBody注解响应JSON格式数据

1.介绍
@RequestMapping("/response/test")
@ResponseBody
public String testResponse(){
    return "success";
}

直接显示服务器响应的内容

image-20220816091907738

如果为Javabean对象需要转化为JSON格式发送

2.响应JSON数据

1.导入Jackson依赖

2.开启注解驱动mvc:annotation-driven/

3.使用@ResponseBody注解,直接回传返回值(会自动转化成JSON格式)

@RequestMapping("/response/test")
@ResponseBody
public String testResponse(){
    User user=new User("cz","123")
    return user;
}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值