SpringMVC応答

ModelAndView

@RequestMapping("/testModelAndView")
    public ModelAndView testModelAndView(){
        //创建ModelAndView对象
        ModelAndView mv = new ModelAndView();
        System.out.println("testModelAndView执行了");
        //模拟从数据库中查出User对象
        User user = new User();
        user.setUserName("会有");
        user.setPassWord("636");
        user.setAge(30);
        //那user对象存储到mv对象中,也会把user对象存入到request对象中
        mv.addObject("user",user);
        //跳转到哪个页面
        mv.setViewName("success");
        return mv;
    }

ページの前方とリダイレクトのためのジャンプ

 @RequestMapping("/testForwardAndRedirect")
    public String testForwardAndRedirect(){
        System.out.println("testForwardAndRedirect执行了");

//        return "forward:/WEB-INF/pages/success.jsp";
        return "redirect:/index.jsp";
    }

ResponseBody応答データJSON

@RequestMapping("/testAjax")
    public void testAjax(@RequestBody String body){
        System.out.println("testAjax执行了");
        System.out.println(body);
    }
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>

    <script src="js/jquery-1.11.3.min.js"></script>
    <script>
        $(function () {
            $("#btn").click(function () {
                alert("hello!");
                $.ajax({
                    url:"user/testAjax",
                    contentType:"application/json;charset=utf-8",
                    type:"post",
                    dataType:"json",
                    data:'{"username":"hehe","password":"123456","age":"20"}',
                    success:function (data) {

                    }


                })
            })
        })
        
    </script>
</head>
<body>
    <a href="user/testString">testString</a>
    <br/>
    <a href="user/testVoid">testVoid</a>
    <br/>
    <a href="user/testModelAndView">testModelAndView</a>
    <br/>
    <a href="user/testForwardAndRedirect">testForwardAndRedirect</a>
    <br/>
    <button id="btn">发送ajax请求</button>
</body>
</html>

*使用する必要があるため、JSONデータパケットのJava Beanのパッケージを話すこと:

<dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-databind</artifactId>
      <version>2.9.8</version>
    </dependency>
    <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-core</artifactId>
      <version>2.9.8</version>
    </dependency>
    <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-annotations</artifactId>
      <version>2.9.8</version>
    </dependency>
 $(function () {
            $("#btn").click(function () {
                alert("hello!");
                $.ajax({
                    url:"user/testAjax",
                    contentType:"application/json;charset=utf-8",
                    type:"post",
                    dataType:"json",
                    data:'{"userName":"回复我","passWord":"123456","age":"20"}',
                    success:function (data) {
                        alert(data)
                        alert(data.userName+"---"+data.passWord+"---"+data.age);
                    }
                })
            })
        })
@RequestMapping("/testAjax")
    public @ResponseBody User testAjax(@RequestBody User user){
        System.out.println("testAjax执行了");
        //客户端发送ajax请求,传的时json字符串,后端把json字符串封装到user对象中
        System.out.println(user);
        return user;
    }
公開された31元の記事 ウォンの賞賛1 ビュー241

おすすめ

転載: blog.csdn.net/weixin_41605945/article/details/104530874