springmvc返回json数据以及session的使用

在之前写过同步请求与异步请求的区别,这里就不写了,在servlet阶段,使用异步请求是通过HttpServletRespone对象获取输出流对象,直接输出的

在springmvc中使用json需要导入一下jar包:

然后修改控制器内容:

package com.zs.controller;

import com.zs.entity.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

@Controller
@RequestMapping("/user")
public class UserController {

    /**
     * 添加responseBody时 就会将这个请求转为异步请求,
     * 转为异步请求后,return 返回就不是视图了,而是这个请求要返回的数据
     * @param user
     * @return
     */
    @RequestMapping(value = "/login",method = {RequestMethod.GET,RequestMethod.POST})
    @ResponseBody
    public String login(User user) {
        String username = user.getUsername();
        return username;
    }

    /**
     * 使用异步请求除了返回字符串,还可以返回对象,
     * 返回对象时,spring的json插件会将对象转为json字符串
     */
    @RequestMapping(value = "/yibu")
    @ResponseBody
    public User fun1() {
        User user = new User();
        user.setUid(1);
        user.setUsername("zhaoshuai");
        user.setPassword("123456");
        return user;
    }

}

然后在浏览器发送请求,运行结果如下:

使用ajax来发送请求:编辑登陆页面

<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2019/6/25
  Time: 20:21
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
    String path = request.getContextPath();
    String basePath=request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<html>
<head>
    <base href="<%=basePath%>>">
    <title>Title</title>
    <script src="js/jquery-1.8.3.min.js"></script>
</head>
<body>
<form action="" method="post">
    用户名:<input type="text" name="username" id="username">
    密码:<input type="password" name="password" id="password">
    <input type="button" onclick="check()" value="登录">
</form>
<script>
    function check() {
        $.post("user/login.do",{"username":username.value,"password":password.value},function(data){
            alert(data);
        });
    }
</script>
</body>
</html>

注意别忘了导入jq包

然后运行测试,在运行tomcat过程中出现问题,我点击登录按钮不管用,查看后发现加载不到jq文件,可以打开编译后文件夹target文件夹,查看是否有没有编译进来的文件,如果有的话,就关闭tomcat,删掉文件夹,重新编译生成一个文件夹,还有导入jsonjar包时,如果出错也按这个处理。

二、在springmvc中使用session

我们之前在servlet阶段都是使用HttpServletRequest对象来获取session,在springmvc中,也可以使用这种方式,不过在使用springmvc中,我们大多数情况使用的都是模型传参,不适用HttpServletRequest传参,因此使用model传参时,怎么使用session呢

修改controller:

package com.zs.controller;

import com.zs.entity.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/user")
@SessionAttributes("user")
public class UserController {


    @RequestMapping(value = "/login",method = {RequestMethod.GET,RequestMethod.POST})
    public String login(User user,Model model) {
        /**
         * 将对象放入共享数据,如果类上加了@SessionAttributes
         * 方法执行结束后,会检查model中的共享数据名是否与类上注解的名一样,判断是否需要将数据放到session中,
         * 如果需要就在session中放一份
         */
        model.addAttribute("user", user);
        return "index";
    }

    /**
     * 测试在上一个请求放入的session在另一个请求中能否获取
     * @param session
     * @param model
     * @return
     */
    @RequestMapping("/test")
    public String fun2(HttpSession session,Model model) {
        /**
         * 获取session数据的方法第一种(推荐使用)
         */
        Object user = session.getAttribute("user");
        System.out.println(user);
        /**
         * 获取session数据的第二种方法
         */
        Object user1 = model.asMap().get("user");
        System.out.println(user1);
        return "index";
    }
}

 

posted @ 2019-06-27 16:32  Zs夏至  阅读(358)  评论(0编辑  收藏  举报