SpringMVCエントリ--day03:レスポンス戻り値型

1. String型の戻り値に対応

    @RequestMapping("/testString")
    public String testString(Model model) {
        System.out.println("testString方法执行了。。。");

        //模拟从数据库中查询出User对象
        User user = new User();
        user.setUsername("开发");
        user.setPassword("123");
        user.setAge(30);

        //model对象:通过model将user对象传入"user"key值中
        model.addAttribute("user", user);
        return "success";
    }

2.応答値は、ボイドの戻り値の型があります

①書面による要求転送プログラム

    //返回值是void类型时
    @RequestMapping("/testVoid")
    public void testVoid(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("testVoid方法执行了。。。");
        //编写请求转发的程序
        //前面返回字符串("success")可以找到success.jsp直接跳页面,是因为开启了视图解析器
        //而这里手动写转发的方法不会开启视图解析器,所以这里要写"/WEB-INF/pages/success.jsp"
        request.getRequestDispatcher("/WEB-INF/pages/success.jsp").forward(request, response);
        return;
    }

②リダイレクト

    //返回值是void类型时
    @RequestMapping("/testVoid2")
    public void testVoid2(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("testVoid2方法执行了。。。");
        //重定向只能写成"/xxx.jsp"的格式
        response.sendRedirect(request.getContextPath() + "/index.jsp");
        return;
    }

③ダイレクトレスポンス

    //返回值是void类型时
    @RequestMapping("/testVoid3")
    public void testVoid3(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("testVoid3方法执行了。。。");

        //解决中文乱码
        response.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");

        //直接进行响应
        response.getWriter().print("你好");
        return;
    }

3.戻り値は、応答タイプのModelAndViewあります

@RequestMapping("/testModelAndView")
    public ModelAndView testModelAndView() {
        //创建ModelAndView对象
        ModelAndView mv = new ModelAndView();
        System.out.println("testModelAndView方法执行了。。。");

        //模拟从数据库中查询出User对象
        User user = new User();
        user.setUsername("开发");
        user.setPassword("123");
        user.setAge(30);

        //把user对象存储到mv对象时,它也会把user对象存入到request对象中
        mv.addObject("user", user);
        //跳转到指定页面,此处使用了视图解析器找到success.jsp
        mv.setViewName("success");
        return mv;
    }

ページジャンプの応答転送(フォワード)及びリダイレクション(リダイレクト)の4.

    @RequestMapping("/testForwardOrRedirect")
    public String testForwardOrRedirect() {
        System.out.println("testForwardOrRedirect方法执行了。。。");

        //请求的转发 forward
        //使用forward关键字表示这里采用的是请求转发的方式,不能再使用视图解析器
        return "forward:/WEB-INF/pages/success.jsp";
    }

    @RequestMapping("/testForwardOrRedirect2")
    public String testForwardOrRedirect2() {
        System.out.println("testForwardOrRedirect2方法执行了。。。");

        //重定向
        //使用redirect关键字表示这里采用的是重定向的方式,不能使用视图解析器,且格式只能是/xxx.jsp
        return "redirect:/index.jsp";
    }

データリソースのJSONの静的応答フィルタリング

フロントコントローラのWebアプリケーションに- WEB-INF -でのweb.xml。
ここでは資源に- springmvc.xml傍受を設定されていないリソース。
リソース- springmvc.xml:

    <!--前端控制器哪些资源不拦截-->
    <mvc:resources mapping="/css/**" location="/css/"/>
    <mvc:resources mapping="/images/**" location="/images/"/>
    <mvc:resources mapping="/js/**" location="/js/"/>

たresponse.jsp:
ここで設定したWebアプリケーション-静的リソースの下では、jsの傍受されないjquery.min.jsディレクトリ

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>

    <script src="js/jquery.min.js"></script>
    <script>
        //页面加载,绑定单击事件
        $(function () {
            $("#btn").click(function () {
                alert("hello btn");
            })
        })
    </script>
</head>
<body>
    <button id="btn">发送ajax请求</button>
</body>
</html>

おすすめ

転載: blog.csdn.net/dl674756321/article/details/90754418