欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

【SpringMVC】005-SpringMVC返回值类型

程序员文章站 2022-03-24 15:26:25
...

一、返回值是String类型

1、作用

controller方法返回字符串可以指定逻辑视图名,通过视图解析器解析为物理视图地址;

 

2、示例

User类:

package com.zibo.bean;

import java.io.Serializable;

public class User implements Serializable {
    private String username;
    private String password;
    private int age;

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "User{" +
                "username='" + username + '\'' +
                ", password='" + password + '\'' +
                ", age=" + age +
                '}';
    }
}

HelloController类:

package com.zibo.controller;

import com.zibo.bean.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

//控制器
@Controller
public class HelloController {
    //设置路径为"/hello"
    @RequestMapping(value = "/hello")
    public String sayHello(ModelMap modelMap){
        //控制台打印"Hello Spring MVC!"
        System.out.println("Hello Spring MVC!");
        //创建一个对象,假装是从数据库查过来的
        User user = new User();
        user.setUsername("大哥");
        user.setPassword("123");
        user.setAge(20);
        //存入request域对象
        modelMap.addAttribute("user",user);
        //返回success.jsp页面
        return "success";
    }
}

index.jsp页面:

<%--
  Created by IntelliJ IDEA.
  User: ZiBo
  Date: 2020/8/9
  Time: 14:52
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
    <h3>入门程序!</h3>
    <a href="hello">点我调转到success.jsp页面</a>
</body>
</html>

success.jsp页面:

<%--
  Created by IntelliJ IDEA.
  User: ZiBo
  Date: 2020/8/9
  Time: 15:05
  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>
    <h1>入门成功!</h1>
<%--    取数据--%>
    ${ requestScope.get("user").username }
    <br/>
    ${ user.username }
    <br/>
    ${ user.password }
    <br/>
    ${ user.age }
</body>
</html>

备注:其他代码(环境)参考SpringMVC入门程序;

运行结果:

【SpringMVC】005-SpringMVC返回值类型

 

二、返回值是void

1、说明

我们可以通过Servlet原始API中的request和response控制响应结果,进行页面跳转;

 

2、简单示例

HelloController类:

package com.zibo.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

//控制器
@Controller
public class HelloController {
    //设置路径为"/hello"
    @RequestMapping(value = "/hello")
    public void sayHello(){
        //控制台打印"Hello Spring MVC!"
        System.out.println("Hello Spring MVC!");
    }
}

说明:

只写这么多,服务器默认回去访问XXX/hello.jsp页面,这是不可取的,运行结果如下图:

【SpringMVC】005-SpringMVC返回值类型

 

3、通过request跳转页面

HelloController类:

package com.zibo.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

//控制器
@Controller
public class HelloController {
    //设置路径为"/hello"
    @RequestMapping(value = "/hello")
    public void sayHello(HttpServletRequest request, HttpServletResponse response) throws Exception {
        //控制台打印"Hello Spring MVC!"
        System.out.println("Hello Spring MVC!");
        //使用request进行请求转发
        request.getRequestDispatcher("/WEB-INF/pages/success.jsp").forward(request,response);
    }
}

success.jsp页面:

<%--
  Created by IntelliJ IDEA.
  User: ZiBo
  Date: 2020/8/9
  Time: 15:05
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
    <h1>入门成功!</h1>
</body>
</html>

运行结果:

【SpringMVC】005-SpringMVC返回值类型

 

4、通过重定向跳转页面

HelloController类:

package com.zibo.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

//控制器
@Controller
public class HelloController {
    //设置路径为"/hello"
    @RequestMapping(value = "/hello")
    public void sayHello(HttpServletRequest request, HttpServletResponse response) throws Exception {
        //控制台打印"Hello Spring MVC!"
        System.out.println("Hello Spring MVC!");
        //使用request进行请求转发
//        request.getRequestDispatcher("/WEB-INF/pages/success.jsp").forward(request,response);
        //重定向跳转,重定向不能直接定向到/WEB-INF/pages/下的success.jsp
        response.sendRedirect(request.getContextPath() + "/redirect.jsp");
    }
}

redirect.jsp页面:

<%--
  Created by IntelliJ IDEA.
  User: ZiBo
  Date: 2020/8/10
  Time: 17:38
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
    <h1>重定向到达的页面!</h1>
</body>
</html>

运行结果:

【SpringMVC】005-SpringMVC返回值类型

 

5、直接进行响应

HelloController类:

package com.zibo.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

//控制器
@Controller
public class HelloController {
    //设置路径为"/hello"
    @RequestMapping(value = "/hello")
    public void sayHello(HttpServletResponse response) throws Exception {
        //控制台打印"Hello Spring MVC!"
        System.out.println("Hello Spring MVC!");
        //使用request进行请求转发
//        request.getRequestDispatcher("/WEB-INF/pages/success.jsp").forward(request,response);
        //重定向跳转,重定向不能直接定向到/WEB-INF/pages/下的success.jsp
//        response.sendRedirect(request.getContextPath() + "/redirect.jsp");
        //直接进行响应
        //设置编码,解决中文乱码
        response.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");
        //响应
        response.getWriter().println("<h1>直接进行响应</h1>");
    }
}

运行结果:

【SpringMVC】005-SpringMVC返回值类型

 

三、返回值是ModelAndView类型

1、作用

ModelAndView对象是SpringMVC提供的一个对象,可通过视图解析器跳转页面;

 

2、示例

HelloController类:

package com.zibo.controller;

import com.zibo.bean.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

//控制器
@Controller
public class HelloController {
    //设置路径为"/hello"
    @RequestMapping(value = "/hello")
    public ModelAndView sayHello() {
        //控制台打印"Hello Spring MVC!"
        System.out.println("Hello Spring MVC!");
        //使用ModelAndView实现页面跳转
        ModelAndView mv = new ModelAndView();
        //创建一个对象,假装是从数据库查过来的
        User user = new User();
        user.setUsername("大哥");
        user.setPassword("123");
        user.setAge(20);
        mv.addObject("user",user);
        //设置要跳转的页面
        mv.setViewName("success");
        return mv;
    }
}

success.jsp页面:

<%--
  Created by IntelliJ IDEA.
  User: ZiBo
  Date: 2020/8/9
  Time: 15:05
  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>
    <h1>入门成功!</h1>
    ${ user.username }<br/>
    ${ user.password }<br/>
    ${ user.age }
</body>
</html>

运行结果:

【SpringMVC】005-SpringMVC返回值类型

 

四、使用关键字的方式进行转发和重定向(不常用)

1、转发示例

HelloController类:

package com.zibo.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

//控制器
@Controller
public class HelloController {
    //设置路径为"/hello"
    @RequestMapping(value = "/hello")
    public String sayHello() {
        //控制台打印"Hello Spring MVC!"
        System.out.println("Hello Spring MVC!");
        //转发
        return "forward:/WEB-INF/pages/success.jsp";
    }
}

success.jsp页面:

<%--
  Created by IntelliJ IDEA.
  User: ZiBo
  Date: 2020/8/9
  Time: 15:05
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
    <h1>入门成功!</h1>
</body>
</html>

运行结果:

【SpringMVC】005-SpringMVC返回值类型

 

2、重定向示例

HelloController类:

package com.zibo.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

//控制器
@Controller
public class HelloController {
    //设置路径为"/hello"
    @RequestMapping(value = "/hello")
    public String sayHello() {
        //控制台打印"Hello Spring MVC!"
        System.out.println("Hello Spring MVC!");
        //转发
//        return "forward:/WEB-INF/pages/success.jsp";
        //重定向-注意不能重定向到WEB-INF文件夹下,也不需要再写项目名称
        return "redirect:redirect.jsp";
    }
}

运行结果:

【SpringMVC】005-SpringMVC返回值类型