博客
关于我
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添加用户权限报1064 - You have an error in your SQL syntax问题解决
查看>>
mysql添加索引
查看>>
mysql添加表注释、字段注释、查看与修改注释
查看>>
mysql清理undo线程_MySQL后台线程的清理工作
查看>>
mysql清空带外键的表
查看>>
MySQL清空表数据
查看>>
mysql源码安装
查看>>
Mysql源码安装过程中可能碰到的问题
查看>>
MySQL灵魂16问,你能撑到第几问?
查看>>
MySQL灵魂拷问:36题带你面试通关
查看>>
mysql状态分析之show global status
查看>>
mysql状态查看 QPS/TPS/缓存命中率查看
查看>>
mysql生成树形数据_mysql 实现树形的遍历
查看>>
mysql用于检索的关键字_Mysql全文搜索match...against的用法
查看>>
MySQL用得好好的,为什么要转ES?
查看>>
MySql用户以及权限的管理。
查看>>
MySQL用户权限配置:精细控制和远程访问的艺术!------文章最后有惊喜哦。
查看>>
mysql用户管理、常用语句、数据分备份恢复
查看>>
MySQL留疑问:left join时选on还是where?
查看>>
mysql登陆慢问题解决
查看>>