SpringMVC的转发、重定向以及json数据格式

转发以及重定向

package cn.kgc.mvc.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttribute;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * servlet:
 *   转发:request
 *   重定向:response
 *
 * Created by 小保底不歪 
 * 2022/7/7 9:04
 * 解析 mvc架构中的转发和重定向操作
 */
@Controller
@RequestMapping("fr")
@SessionAttributes({"username"})
public class FRController {

    // servlet 方式
    // 转发
    @RequestMapping("f1")
    public void test01(HttpServletRequest request , HttpServletResponse response)throws ServletException , IOException {
        System.out.println("转发到index.jsp");
        request.getRequestDispatcher("/index.jsp").forward(request,response);
    }

    // 重定向
    @RequestMapping("f2")
    public void test02(HttpServletRequest request , HttpServletResponse response)throws ServletException , IOException {
        System.out.println("重定向到index.jsp");
        response.sendRedirect(request.getContextPath() + "/index.jsp");
    }

    // mvc 方式
    // 转发
    @RequestMapping("f3")
    public String test03(){
        System.out.println("test03()");
        // 默认 转发 -> 视图解析器
        return "index";
    }

    // 重定向
    @RequestMapping("f4")
    public String test04(){
        System.out.println("test04()");
        // controller -> .jsp redirect 不会再经过视图解析器
        return "redirect:/index.jsp";
    }

    // 转发 f5 -> f3
    @RequestMapping("f5")
    public String test05(){
        System.out.println("test05()");
        // controller -> controller 转发  不会经过视图解析器
        // return "/fr/f3";  //fr/f3.jsp
        return "forward:/fr/f3";
    }

    // 重定向 f6 -> f4
    @RequestMapping("f6")
    public String test06(){
        System.out.println("test06()");
        // controller -> controller 重定向  不会经过视图解析器
        return "redirect:/fr/f4";
    }

    /*
    * 跳转页面时  如何携带数据
    * 转发: request
    * 重定向:session
    */
    @RequestMapping("f7")
    public ModelAndView test07(ModelAndView mv){// 可以将后续跳转的页面以及页面中需要显示的数据封装成ModelAndView

        // 设置视图名
        mv.setViewName("index");
        // 绑定参数
        mv.addObject("username","tom");

        return mv;
    }

    @RequestMapping("f8")
    public String test08(ModelAndView mv){// 可以将后续跳转的页面以及页面中需要显示的数据封装成ModelAndView

        // 设置视图名
        // mv.setViewName("index");
        // 绑定参数
        mv.addObject("username","tom");

        return "index";
    }

    @RequestMapping("f9")
    public String test09(ModelAndView mv){// 可以将后续跳转的页面以及页面中需要显示的数据封装成ModelAndView

        // 设置视图名
        // mv.setViewName("index");
        // 绑定参数
        mv.addObject("username","tom");

        return "redirect:/index.jsp";
    }

    @RequestMapping("f10")
    public String test10(HttpServletRequest request){// 可以将后续跳转的页面以及页面中需要显示的数据封装成ModelAndView

        // 绑定参数
        request.getSession().setAttribute("username","lucy");
        // sessionScope username:lucy
        return "redirect:/index.jsp";
    }

    @RequestMapping("f11")
    public String test11(Model model){// 通过@SessionAttrubution({}) 完成将model中指定key 对应的值存入session

        // 绑定参数
        model.addAttribute("username","lucy");
        model.addAttribute("code","adc");
        // sessionScope username:lucy
        return "redirect:/index.jsp";
    }

    // 获取session域中存放的值
    @RequestMapping("f12")
    public String test12(@SessionAttribute("code") String code){// 通过@SessionAttrubution("code")

        System.out.println("code = " + code);
        return "redirect:/index.jsp";
    }

}

JSON

public class JSONController {

    // 主流
    @RequestMapping("j1")
    @ResponseBody // 返回值 转换成json格式
    public User test01(){
        System.out.println("ajax请求已接收");
        User user = new User();
        user.setUsername("tom");
        user.setPassword("123");
        user.setAge(20);
        return user;
    }

    @RequestMapping("j2")
    @ResponseBody // 注解一般都是标识 里面没东西  返回值 转换成JSON格式
    public User test02(@RequestBody User user){// 参数接收时,前端传递的参数类型json格式  {"username" ="lisi" }
        System.out.println("user = " + user);
        return user;
    }
}

json.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>发送和接收json格式的数据</title>
    <script src="jquery-1.11.1.js" type="text/javascript"></script>
</head>
<body>

<button id="btn">点击发送ajax请求</button>
<button id="btn2">点击发送ajax请求携带json格式数据</button>

<script type="text/javascript">
    $(function () {
    // 编写发送ajax逻辑代码
        $('#btn').click(function () {
            $.ajax({
                url:'/mvc/json/j1',
                dataType:'json',
                type:'post',
                success:function (res) {
                    console.log(res)
                }
            })
        })

        $('#btn2').click(function () {
            $.ajax({
                url:'/mvc/json/j2',
                data:JSON.stringify(user={
                    username:'jack',
                    password:'123',
                    age:20
                }),
                contentType:'application/json;charset=utf-8',
                dataType:'json',
                type:'post',
                success:function (res) {
                    console.log(res)
                }
            })
        })
    })
</script>
</body>
</html>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值