springMVC相应之返回值
2024-09-02 06:16:36
string返回值:
<%-- Created by IntelliJ IDEA. User: Administrator Date: 2019/11/12 Time: 16:24 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %> <html> <head> <title>Title</title> </head> <body> <h3>执行成功</h3> ${user.username} ${user.age} </body> </html>
package cn.mepu.service; import cn.mepu.domain.User; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; /** * @User 艾康 * @create 2019-11-12 16:36 */ @Controller @RequestMapping("/service") public class index { @RequestMapping("/index") public String indexName(Model model){ System.out.println("查询用户名执行了"); //用户名存入session对象中 User user = new User(); user.setUsername("艾康"); user.setAge(22); model.addAttribute("user",user); return "success"; } }
void:默认值是请求路径文件
转发实现:
@RequestMapping("/Forward") public void orwardTest(Model model, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println("查询用户名执行了"); //转发 request.getRequestDispatcher("/WEB-INF/pages/success.jsp").forward(request,response); }
重定向
@RequestMapping("/Forward") public void orwardTest(Model model, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println("查询用户名执行了"); //重定向 response.sendRedirect(request.getContextPath()+"/forward.jsp"); }
直接写数据;
@RequestMapping("/Forward") public void orwardTest(Model model, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println("查询用户名执行了");//直接响应 //1.设置中文乱码问题 response.setCharacterEncoding("UTF-8"); response.setContentType("text/html;charset=utf-8"); //2.写入数据 response.getWriter().print("你好"); } }
ModelAndView返回值;
@RequestMapping("/testModelAndView") public ModelAndView testModelAndView(){ ModelAndView mv = new ModelAndView(); //用户名存入session对象中 User user = new User(); user.setUsername("艾康"); user.setAge(22); //存入 mv.addObject("user",user); //跳转会找视图解析器 mv.setViewName("success"); return mv; }
重定向和转发:
/** * 关键字方式跳转和转发 * @return */ @RequestMapping("/testModelAnd") public String testModelAnd(){ //转发 return "forward:/WEB-INF/pages/success.jsp"; //重定向 return "redirect:/forward.jsp"; }
最新文章
- .NET判断是否为AJAX提交
- Windows Server 2008 R2 WEB服务器配置系列文章索引
- (转)对比MS Test与NUnit Test框架
- python课程第一周重点记录
- 创建第一个JBPM6项目并且运行自带的helloword例子(JBPM6学习之三)
- 内存中 OLTP - 常见的工作负荷模式和迁移注意事项(二)
- imx6 android5.1 打开 调试串口
- JDK version
- 297.	Serialize and Deserialize Binary Tree
- 怒学三算法 POJ 2387 Til the Cows Come Home (Bellman_Ford || Dijkstra || SPFA)
- poj2337 欧拉路径
- 在Linux下开始C语言的学习
- BZOJ 1029 建筑抢修
- PL/SQL Developer 与tnsnames.ora
- WindowsForm 增 删 查 改
- 【Untiy3D 游戏开发之一】Unity3D For Window/Mac最新4.2.0版本破解教程
- 基于MFC简单图片裁剪工具
- Java基础之TCP与UDP
- 面试真题--------spring源码解析IOC
- spring mybatics