zoukankan      html  css  js  c++  java
  • springmvc框架找那个@responseBody注解

    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <html>
    <head>
    <title>Title</title>
    <script src="js/jquery.min.js"></script>
    <script>
    //页面加载绑定单击事件
    $(function () {
    $("#btn").click(function () {
    // alert("hello ajax");
    //发送ajax请求
    $.ajax({
    url: "user/testAjax",
    contentType: "application/json;charset=UTF-8",
    data:'{"username": "zmy", "password": "123", "age": 20}' ,
    dataType: "json",
    type: "post",
    success: function (data) {
    //data服务器端响应的json的数据,进行解析
    alert(data);
    alert(data.username);
    alert(data.password);
    alert(data.age);
    }
    });
    });
    });
    </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/testForwardOrRedirect">testForwardOrRedirect</a><br/>

    <button id="btn">发送ajax请求</button>
    </body>
    </html>

    package com.hope.controller;

    import com.hope.domain.User;
    import org.springframework.stereotype.Controller;
    import org.springframework.ui.Model;
    import org.springframework.web.bind.annotation.RequestBody;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.ResponseBody;
    import org.springframework.web.servlet.ModelAndView;

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

    /**
    * @author newcityman
    * @date 2019/11/27 - 20:38
    */
    @Controller("userController")
    @RequestMapping(path = {"/user"})
    public class UserController {
    /**
    * 返回值是String
    *
    * @param model
    * @return
    */
    @RequestMapping(path = "/testString")
    public String testString(Model model) {
    System.out.println("testString执行成功");
    User user = new User();

    user.setUsername("zmy");
    user.setPassword("123");
    user.setAge(12);
    model.addAttribute("user", user);
    return "success";
    }

    /**
    * 测试testVoid方法
    *
    * @param
    * @return
    */

    @RequestMapping(path = "/testVoid")
    public void testVoid(HttpServletRequest request, HttpServletResponse response) throws Exception {
    System.out.println("testString执行成功");
    //请求转发
    // request.getRequestDispatcher("/WEB-INF/pages/success.jsp").forward(request,response);
    //重定向(重定向是不能够直接发送请求去访问WEB-INF下的页面的)
    // response.sendRedirect(request.getContextPath()+"/index.jsp");
    //直接流进行响应
    //设置中文乱码
    response.setCharacterEncoding("UTF-8");
    response.setContentType("text/html;charset=UTF-8");
    response.getWriter().print("hello 张三");
    return;
    }


    /**
    * 返回值是ModelAndView
    *
    * @param
    * @return
    */
    @RequestMapping(path = "/testModelAndView")
    public ModelAndView testModelAndView() {
    ModelAndView mv = new ModelAndView();
    System.out.println("testString执行成功");
    User user = new User();

    user.setUsername("tomcat");
    user.setPassword("987");
    user.setAge(5);
    mv.addObject("user", user);
    mv.setViewName("success");
    return mv;
    }

    /**
    * 测试forward和redirect关键字
    *
    * @param
    * @return
    */
    @RequestMapping(path = "/testForwardOrRedirect")
    public String testForwardOrRedirect() {
    System.out.println("testForwardOrRedirect执行成功");
    //请求转发
    // return "forward:/WEB-INF/pages/success.jsp";
    //重定向
    return "redirect:/index.jsp";
    }

    /**
    * 模拟异步请求响应
    */
    @RequestMapping(path = "/testAjax")
    public @ResponseBody User testAjax(@RequestBody User user) {
    System.out.println("testAjax执行成功");
    //客户端发送ajax的请求,传递的是json字符串,后端把json字符串封装到user对象中
    System.out.println(user);
    //做响应,模拟查询数据库
    user.setUsername("light");
    user.setPassword("70578331");
    user.setAge(18);
    return user;
    }



    }



  • 相关阅读:
    配置samba
    extern c
    剑指offer 孩子们的游戏
    剑指offer 扑克牌顺子
    剑指offer 翻转单词顺序列
    剑指offer 左旋转字符串
    mysql查看或显示当前存在多少数据库
    vim替换
    平衡二叉树
    将employees表的所有员工的last_name和first_name拼接起来作为Name,中间以一个空格区分
  • 原文地址:https://www.cnblogs.com/newcityboy/p/11946608.html
Copyright © 2011-2022 走看看