文章目录
1.返回值是一个字符串(底层也会选择ModelAndView方式)2.返回值:void3.返回ModelAndView4.使用关键字的方式进行转发或者重定向(1)WEB-INF目录下的文件客户端不能直接访问到(2)forward和redirect区别
1.返回值是一个字符串(底层也会选择ModelAndView方式)
@Controller
@RequestMapping
("/user")
public class UserController
{
/**
* 1.返回值是一个字符串
(底层也会选择ModelAndView方式
)
* controller 方法在提供了 String 类型的返回值之后,默认就是请求转发。
* controller 方法返回字符串可以指定逻辑视图名,通过视图解析器解析为物理视图地址。
* 指定逻辑视图名,经过视图解析器解析为 jsp 物理路径:/WEB-INF/pages/success.jsp
* @param model
* @return
*/
@RequestMapping
("/testString")
public String testString
(Model model
){
System.out.println
("testString方法执行了······");
//模拟从数据库中查询User对象
User user
= new User
();
user.setAge
(20
);
user.setUsername
("妹妹");
user.setPassword
("123");
//model对象
model.addAttribute
("user",user
);
return "success";
}
<%@ page contentType="text/html;charset=UTF-8" language="java"
isELIgnored="false" %>
<html>
<head>
<title>welcome
</title>
</head>
<body>
<h3>Welcome to here
</h3>
<h3>model:${user}
</h3>
</body>
</html>
2.返回值:void
/**
* 2.返回值:void
* 没有返回值默认跳转:
* Message:/springmvc_day02_01_response_war/WEB-INF/pages/user/testVoid.jsp
* 问题:想要没有返回值,也不想让默认跳转,解决方案如下。
*
*/
@RequestMapping
("/testVoid")
public void testVoid
(HttpServletRequest request, HttpServletResponse response
) throws Exception
{
System.out.println
("testVoid方法执行了······");
//1.编写request请求转发的程序(手动转发不会再去解析视图解析器)(一次请求)
//request.getRequestDispatcher
("/WEB-INF/pages/success.jsp").forward
(request,response
);
//2.编写重定向response页面:不能直接请求WEB-INF里边的请求(两次请求)
//response.sendRedirect
(request.getContextPath
()+
"/index.jsp");
//设置中文乱码
response.setCharacterEncoding
("UTF-8");
response.setContentType
("text/html;charset=UTF-8");
//3.直接会进行响应
response.getWriter
().println
("直接响应");
return;
}
index.jsp:
<a href
="user/testVoid">testVoid
</a
><br
>
3.返回ModelAndView
/**
* 3.返回ModelAndView
* 返回字符串底层实现是ModelAndView方式
* ModelAndView 是 SpringMVC 为我们提供的一个对象,该对象也可以用作控制器方法的返回值。
* @return
*/
@RequestMapping
("/testModelAndView")
public ModelAndView testModelAndView
(){
//创建ModelAndView对象
ModelAndView
mv = new ModelAndView
();
System.out.println
("testModeAndView方法执行了······");
//模拟从数据库中查询User对象
User user
= new User
();
user.setAge
(20
);
user.setUsername
("小风");
user.setPassword
("456");
//把user对象存储到mv对象中,也会把user对象存储到request对象
mv.addObject
("user",user
);
//跳转到哪个页面
mv.setViewName
("success");
return mv;
}
index.jsp:
<a href
="user/testModelAndView">testModeAndView
</a
><br
>
4.使用关键字的方式进行转发或者重定向
(1)WEB-INF目录下的文件客户端不能直接访问到
(2)forward和redirect区别
forward 是服务器内部的请求,可以访问web-inf资源 redirect 是服务器外部的请求,不可以访问web-inf资源 forward 是一次请求内部访问,redirect是两次请求外部访问。
@RequestMapping
("/testForwardOrRedirect")
public String testForwardOrRedirect
(){
System.out.println
("testForwardOrRedirect方法执行了······");
//请求的转发
//return
"forward:/WEB-INF/pages/success.jsp";
//重定向
(关键字,重定向不用加项目名,底层已经帮你加上了
)
return "redirect:/index.jsp";
}
index.jsp:
<a href
="user/testForwardOrRedirect">testForwardOrRedirect
</a
>
success.jsp
<body
>
<h3
>执行成功!!
</h3
>
${user}
</body
>
User实体类
public class User implements Serializable
{
private String username
;
private String password
;
private Integer age
;
······················
}