博客
关于我
SpringMVC系列--数据返回及页面跳转
阅读量:515 次
发布时间:2019-03-07

本文共 2977 字,大约阅读时间需要 9 分钟。

其他网址

jsp文件

<%@ page contentType="text/html;charset=UTF-8" language="java" %>    Title            returnType: String    
returnType: Void
testModelAndView
testForwardOrRedirect

success.jsp 

<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>    Title    

执行成功

${user.username} ${user.password} ${user.age}

一、返回String

①返回页面字符串

@Controller@RequestMapping("/user")public class UserController {    @RequestMapping("/testString")    public String testString(Model model){        System.out.println("testString方法执行了...");        // 模拟从数据库中查询出User对象        User user = new User();        user.setUsername("jack");        user.setPassword("123456");        user.setAge(30);        // model对象        model.addAttribute("user",user);        return "success";    }}

 

 ②重定向

@RequestMapping("/testForwardOrRedirect")public String testForwardOrRedirect(){    System.out.println("testForwardOrRedirect方法执行了...");    // 请求的转发    return "forward:/WEB-INF/pages/success.jsp";    // 重定向    return "redirect:/index.jsp";}

二、无返回值的情况

①请求转发

@RequestMapping("/testVoid")public void testVoid(HttpServletRequest request, HttpServletResponse response) throws Exception {    System.out.println("testVoid方法执行了...");    // 编写请求转发的程序    request.getRequestDispatcher("/WEB-INF/pages/success.jsp").forward(request,response);    return;}

 

②重定向

@RequestMapping("/testVoid")public void testVoid(HttpServletRequest request, HttpServletResponse response) throws Exception {    System.out.println("testVoid方法执行了...");    // 重定向    response.sendRedirect(request.getContextPath()+"/index.jsp");    return;}

 ③直接响应数据

@RequestMapping("/testVoid")public void testVoid(HttpServletRequest request, HttpServletResponse response) throws Exception {    System.out.println("testVoid方法执行了...");    // 设置中文乱码    response.setCharacterEncoding("UTF-8");    response.setContentType("text/html;charset=UTF-8");    // 直接会进行响应    response.getWriter().print("你好");    return;}

 三、返回ModelAndView对象

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

 

四、接收返回异步请求数据

静态资源的过滤,前端控制器DispatcherServlet将会进行拦截,需要在springmvc.xml中进行配置。

 springmvc.xml

 

@ResponseBody@RequestMapping("/testAjax")public User testAjax(@RequestBody User user){    System.out.println("testAjax方法执行了...");    // 客户端发送ajax的请求,传的是json字符串,后端把json字符串封装到user对象中    System.out.println(user);    // 做响应,模拟查询数据库    user.setUsername("rose");    user.setAge(40);    // 做响应    return user;}

 

转载地址:http://lhvjz.baihongyu.com/

你可能感兴趣的文章
mysql5.7免费下载地址
查看>>
mysql5.7命令总结
查看>>
mysql5.7安装
查看>>
mysql5.7性能调优my.ini
查看>>
MySQL5.7新增Performance Schema表
查看>>
Mysql5.7深入学习 1.MySQL 5.7 中的新增功能
查看>>
Webpack 之 basic chunk graph
查看>>
Mysql5.7版本单机版my.cnf配置文件
查看>>
mysql5.7的安装和Navicat的安装
查看>>
mysql5.7示例数据库_Linux MySQL5.7多实例数据库配置
查看>>
Mysql8 数据库安装及主从配置 | Spring Cloud 2
查看>>
mysql8 配置文件配置group 问题 sql语句group不能使用报错解决 mysql8.X版本的my.cnf配置文件 my.cnf文件 能够使用的my.cnf配置文件
查看>>
MySQL8.0.29启动报错Different lower_case_table_names settings for server (‘0‘) and data dictionary (‘1‘)
查看>>
MYSQL8.0以上忘记root密码
查看>>
Mysql8.0以上重置初始密码的方法
查看>>
mysql8.0新特性-自增变量的持久化
查看>>
Mysql8.0注意url变更写法
查看>>
Mysql8.0的特性
查看>>
MySQL8修改密码报错ERROR 1819 (HY000): Your password does not satisfy the current policy requirements
查看>>
MySQL8修改密码的方法
查看>>