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

你可能感兴趣的文章
nginx 代理解决跨域
查看>>
Nginx 做负载均衡的几种轮询策略分析
查看>>
Nginx 入门,一篇搞定!
查看>>
Nginx 利用代理转发请求示例
查看>>
Nginx 动静分离与负载均衡的实现
查看>>
Nginx 反向代理 MinIO 及 ruoyi-vue-pro 配置 MinIO 详解
查看>>
nginx 反向代理 转发请求时,有时好有时没反应,产生原因及解决
查看>>
Nginx 反向代理+负载均衡
查看>>
Nginx 反向代理解决跨域问题
查看>>
Nginx 反向代理配置去除前缀
查看>>
nginx 后端获取真实ip
查看>>
Nginx 多端口配置和访问异常问题的排查与优化
查看>>
Nginx 如何代理转发传递真实 ip 地址?
查看>>
Nginx 学习总结(16)—— 动静分离、压缩、缓存、黑白名单、性能等内容温习
查看>>
Nginx 学习总结(17)—— 8 个免费开源 Nginx 管理系统,轻松管理 Nginx 站点配置
查看>>
Nginx 学习(一):Nginx 下载和启动
查看>>
nginx 常用指令配置总结
查看>>
Nginx 常用配置清单
查看>>
nginx 常用配置记录
查看>>
nginx 开启ssl模块 [emerg] the “ssl“ parameter requires ngx_http_ssl_module in /usr/local/nginx
查看>>