博客
关于我
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/

你可能感兴趣的文章
Mysql分表后同结构不同名称表之间复制数据以及Update语句只更新日期加减不更改时间
查看>>
mySql分页Iimit优化
查看>>
MySQL分页查询
查看>>
WebDriverException:未知错误:对于旧版本的 Google Chrome,在 Python 中找不到带有 Selenium 的 Chrome 二进制错误
查看>>
mysql列转行函数是什么
查看>>
mysql创建函数报错_mysql在创建存储函数时报错
查看>>
mysql创建数据库和用户 并授权
查看>>
mysql创建数据库指定字符集
查看>>
MySql创建数据表
查看>>
MySQL创建新用户以及ERROR 1396 (HY000)问题解决
查看>>
MySQL创建用户与授权
查看>>
MySQL创建用户报错:ERROR 1396 (HY000): Operation CREATE USER failed for 'slave'@'%'
查看>>
MySQL创建索引时提示“Specified key was too long; max key length is 767 bytes”
查看>>
mysql初始密码错误问题
查看>>
MySQL删除数据几种情况以及是否释放磁盘空间【转】
查看>>
Mysql删除重复数据通用SQL
查看>>
mysql判断某一张表是否存在的sql语句以及方法
查看>>
mysql加入安装策略_一键安装mysql5.7及密码策略修改方法
查看>>
mysql加强(1)~用户权限介绍、分别使用客户端工具和命令来创建用户和分配权限
查看>>
mysql加强(2)~单表查询、mysql查询常用的函数
查看>>