SpringMVC(6)之相关注解及请求方法的返回值

SpringMVC(6)之相关注解及请求方法的返回值

微信搜索 zze_coding 或扫描 👉 二维码关注我的微信公众号获取更多资源推送:

相关注解

@RequestMapping

多路由

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

@Controller
public class TestController {
    /**
     * 请求 /test1 和 /test2 都会指向 test 方法
     */
    @RequestMapping(value = {"test1", "test2"})
    public void test() {

    }
}

配置在类上

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

@Controller
@RequestMapping("test")
public class TestController {
    /**
     * 因为类上使用了 RequestMapping 注解,此时 test 方法的访问路径就为:
     *      /test/test1 和 /test/test2
     */
    @RequestMapping(value = {"test1", "test2"})
    public void test() {

    }
}

限定请求方式

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

@Controller
public class TestController {
    /**
     * 限定请求方法只能为 GET 和 POST
     */
    @RequestMapping(value = "test",method = {RequestMethod.POST,RequestMethod.GET})
    public void test() {

    }
}

@RequestParam

自定义请求参数映射

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

@Controller
public class TestController {
    /**
     * @RequestParam
     *  name 指定所修饰参数对应请求参数名
     *  required 指定所修饰参数是否必须
     *  defaultValue 指定所修饰参数的默认值
     */
    @RequestMapping(value = "test")
    public void test(@RequestParam(name = "pid",required = false,defaultValue = "1") Integer id) {

    }
}

请求方法的返回值

在 SpringMVC 中,请求方法的返回值除了 ModelAndView,还可以有如下几种方式:

返回 String,转发到 JSP

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

@Controller
public class TestController {
    /**
     * 转发到 jsp
     */
    @RequestMapping(value = "test")
    public String test() {
        // 因为配置了视图解析器,这里的 userlist 实际指向 /WEB-INF/jsp/userlist.jsp
        return "userlist";
    }
}

返回 String,转发到 Action

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

@Controller
public class TestController {
    /**
     * 转发到 Action
     */
    @RequestMapping(value = "test")
    public String test() {
        return "forward:/user/list";
    }
}

返回 String,重定向到 Action

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

@Controller
public class TestController {
    /**
     * 重定向到 Action
     */
    @RequestMapping(value = "test")
    public String test() {
        return "redirect:/user/list";
    }
}

无返回值,访问原生 Servlet API

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

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Controller
public class TestController {
    /**
     * 访问原生 request 和 response API
     */
    @RequestMapping(value = "test")
    public void test(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/plain;charset=utf-8");
//        request.getRequestDispatcher("/user/list").forward(request,response); 转发
//        response.sendRedirect("/user/list"); // 重定向
        response.getWriter().write("你好世界");
    }
}

Copyright: 采用 知识共享署名4.0 国际许可协议进行许可

Links: https://www.zze.xyz/archives/springmvc6.html

Buy me a cup of coffee ☕.