博客
关于我
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: 413 – Request Entity Too Large Error and Solution
查看>>
nginx: [emerg] getpwnam(“www”) failed 错误处理方法
查看>>
nginx: [emerg] the “ssl“ parameter requires ngx_http_ssl_module in /usr/local/nginx/conf/nginx.conf:
查看>>
nginx: [error] open() “/usr/local/nginx/logs/nginx.pid“ failed (2: No such file or directory)
查看>>
nginx:Error ./configure: error: the HTTP rewrite module requires the PCRE library
查看>>
Nginx:objs/Makefile:432: recipe for target ‘objs/src/core/ngx_murmurhash.o‘解决方法
查看>>
nginxWebUI runCmd RCE漏洞复现
查看>>
nginx_rtmp
查看>>
Vue中向js中传递参数并在js中定义对象并转换参数
查看>>
Nginx、HAProxy、LVS
查看>>
nginx一些重要配置说明
查看>>
Nginx一网打尽:动静分离、压缩、缓存、黑白名单、跨域、高可用、性能优化......
查看>>
Nginx下配置codeigniter框架方法
查看>>
Nginx与Tengine安装和使用以及配置健康节点检测
查看>>
Nginx中使用expires指令实现配置浏览器缓存
查看>>
Nginx中使用keepalive实现保持上游长连接实现提高吞吐量示例与测试
查看>>
Nginx中如何配置WebSocket代理?
查看>>
Nginx中实现流量控制(限制给定时间内HTTP请求的数量)示例
查看>>
nginx中配置root和alias的区别
查看>>
nginx主要流程(未完成)
查看>>