响应之返回值是ModelAndView类型

it2025-10-08  4

响应之返回值是ModelAndView类型

1.在response.jsp编写如下代码:

<%-- Created by IntelliJ IDEA. User: Adair Date: 2020/7/2 0002 Time: 10:11 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>返回响应</title> </head> <body> <a href="user/testModelAndView" >testModelAndView</a> </body> </html>

2.创建返回响应控制器类控制器类的代码如下:

package com.txw.controller; import com.txw.domain.User; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.servlet.ModelAndView; /** * 返回响应控制器类 * @author Adair */ @Controller @RequestMapping(value = "/user") @SuppressWarnings("all") // 注解警告信息 public class UserController { /** * 返回ModelAndView * @return */ @RequestMapping("/testModelAndView") public ModelAndView testModelAndView(){ // 创建ModelAndView对象 ModelAndView mv = new ModelAndView(); System.out.println("testModelAndView方法执行了..."); // 模拟从数据库中查询出User对象 User user = new User(); user.setUserName("Adair"); user.setPassWord("123456"); user.setAge(24); // 把user对象存储到mv对象中,也会把user对象存入到request对象 mv.addObject("user",user); // 跳转到哪个页面 mv.setViewName("success"); return mv; } }

3.使用TomCat运行结果如图: 4.通过浏览器访问http://localhost:8080/response.jsp结果如图所示: 5.点击testModelAndView会跳转到如图所示的界面: 6.控制台打印结果如图所示:

最新回复(0)