返回ModelAndView
如果前后端不分的开发,大部分情况下,我们返回ModelAndView,即数据模型+视图:
1
2
3
4
5
6
7
8
9
10
|
@Controller
@RequestMapping ( "/user" )
public class HelloController {
@RequestMapping ( "/hello" )
public ModelAndView hello() {
ModelAndView mv = new ModelAndView( "hello" );
mv.addObject( "username" , "javaboy" );
return mv;
}
}
|
Model中,放我们的数据,然后在ModelAndView中指定视图名称
返回Void
没有返回值。没有返回值,并不一定真的没有返回值,只是方法的返回值为 void,我们可以通过其他方式给前端返回。实际上,这种方式也可以理解为 Servlet 中的那一套方案。
注意,由于默认的 Maven 项目没有 Servlet,因此这里需要额外添加一个依赖:
1
2
3
4
5
|
< dependency >
< groupId >javax.servlet</ groupId >
< artifactId >javax.servlet-api</ artifactId >
< version >4.0.1</ version >
</ dependency >
|
通过 HttpServletRequest 做服务端跳转
1
2
3
4
|
@RequestMapping ( "/hello2" )
public void hello2(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.getRequestDispatcher( "/jsp/hello.jsp" ).forward(req,resp); //服务器端跳转
}
|
通过HttpServletRequest做服务跳转
1
2
3
4
|
@RequestMapping ( "/hello2" )
public void hello2(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.getRequestDispatcher( "/jsp/hello.jsp" ).forward(req,resp); //服务器端跳转
}
|
通过HttpServletResponse做重定向
1
2
3
4
|
@RequestMapping ( "/hello3" )
public void hello3(HttpServletRequest req, HttpServletResponse resp) throws IOException {
resp.sendRedirect( "/hello.jsp" );
}
|
也可以自己手动指定响应头去实现重定向:
1
2
3
4
5
|
@RequestMapping ( "/hello3" )
public void hello3(HttpServletRequest req, HttpServletResponse resp) throws IOException {
resp.setStatus( 302 );
resp.addHeader( "Location" , "/jsp/hello.jsp" );
}
|
通过 HttpServletResponse 给出响应
1
2
3
4
5
6
7
8
|
@RequestMapping ( "/hello4" )
public void hello4(HttpServletRequest req, HttpServletResponse resp) throws IOException {
resp.setContentType( "text/html;charset=utf-8" );
PrintWriter out = resp.getWriter();
out.write( "hello javaboy!" );
out.flush();
out.close();
}
|
这种方式,既可以返回 JSON,也可以返回普通字符串。
返回字符串
返回逻辑视图名
前面的 ModelAndView 可以拆分为两部分,Model 和 View,在 SpringMVC 中,Model 我们可以直接在参数中指定,然后返回值是逻辑视图名:
1
2
3
4
5
|
@RequestMapping ( "/hello5" )
public String hello5(Model model) {
model.addAttribute( "username" , "javaboy" ); //这是数据模型
return "hello" ; //表示去查找一个名为 hello 的视图
}
|
服务端跳转
1
2
3
4
|
@RequestMapping ( "/hello5" )
public String hello5() {
return "forward:/jsp/hello.jsp" ;
}
|
forward 后面跟上跳转的路径。
客户端跳转
1
2
3
4
|
@RequestMapping ( "/hello5" )
public String hello5() {
return "redirect:/user/hello" ;
}
|
真的返回一个字符串
上面三个返回的字符串,都是由特殊含义的,如果一定要返回一个字符串,需要额外添加一个注意:@ResponseBody ,这个注解表示当前方法的返回值就是要展示出来返回值,没有特殊含义。
1
2
3
4
5
|
@RequestMapping ( "/hello5" )
@ResponseBody
public String hello5() {
return "redirect:/user/hello" ;
}
|
上面代码表示就是想返回一段内容为 redirect:/user/hello 的字符串,他没有特殊含义。注意,这里如果单纯的返回一个中文字符串,是会乱码的,可以在 @RequestMapping 中添加 produces 属性来解决:
1
2
3
4
5
|
@RequestMapping (value = "/hello5" ,produces = "text/html;charset=utf-8" )
@ResponseBody
public String hello5() {
return "Java 语言程序设计" ;
}
|
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持快网idc。
原文链接:https://www.cnblogs.com/qiuwenli/p/13423948.html
相关文章
- 64M VPS建站:是否适合初学者操作和管理? 2025-06-10
- ASP.NET自助建站系统中的用户注册和登录功能定制方法 2025-06-10
- ASP.NET自助建站系统的域名绑定与解析教程 2025-06-10
- 个人服务器网站搭建:如何选择合适的服务器提供商? 2025-06-10
- ASP.NET自助建站系统中如何实现多语言支持? 2025-06-10
- 2025-07-10 怎样使用阿里云的安全工具进行服务器漏洞扫描和修复?
- 2025-07-10 怎样使用命令行工具优化Linux云服务器的Ping性能?
- 2025-07-10 怎样使用Xshell连接华为云服务器,实现高效远程管理?
- 2025-07-10 怎样利用云服务器D盘搭建稳定、高效的网站托管环境?
- 2025-07-10 怎样使用阿里云的安全组功能来增强服务器防火墙的安全性?
快网idc优惠网
QQ交流群
-
Ubuntu17.04开放下载:支持AMD Ryzen和Intel Kaby Lake处理器
2025-05-25 101 -
2025-05-25 93
-
2025-05-29 36
-
2025-05-25 20
-
2025-05-25 100