SpringMVC note 2

Response data and view results

The return value classification

1. The return value is String

The return value is a string type, we will look for the corresponding jsp page based on the returned string

@Controller
@RequestMapping("/user")
public class UserController {
    //返回值类型是String
    @RequestMapping("/testString")
    public String testString(Model model){
        System.out.println("testString方法执行了");
        //模拟从数据库中查询出User对象
        User user = new User();
        user.setAge(20);
        user.setPassword("123");
        user.setUsername("任我行");
        //使用model把对象存起来
        model.addAttribute("user",user);
        return "success";
    }

img

2. The return value is Void

What would request the default path is the path to find the jsp requestsimg

Programs written request forwarding and redirection and direct response

@Controller
@RequestMapping("/user")
public class UserController {
    //返回值类型是String
    @RequestMapping("/testString")
    public String testString(Model model){
        System.out.println("testString方法执行了");
        //模拟从数据库中查询出User对象
        User user = new User();
        user.setAge(20);
        user.setPassword("123");
        user.setUsername("任我行");
        //使用model把对象存起来
        model.addAttribute("user",user);
        return "success";
    }
    //返回值类型是Void
    //请求转发是一次请求:不用编写项目的名称

    @RequestMapping("/testVoid")
    public void testVoid(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("testVoid方法执行了");
        //编写请求转发的程序
       // request.getRequestDispatcher("/WEB-INF/pages/success.jsp").forward(request,response);//转发
        //response.sendRedirect(request.getContextPath()+"/index.jsp");//重定向
       //设置中文乱码
        response.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");
        response.getWriter().print("hello,大笨蛋");
        return;
    }
}

The return value is ModelAndView object (JavaBean objects exist and jump page)

    //返回值类型是ModelAndView
    @RequestMapping("/testModelAndView")
    public ModelAndView testModelAndView(){
        ModelAndView mv = new ModelAndView();
        System.out.println("testModelAndView执行了");
        //模拟从数据库中查询出User对象
        User user = new User();
        user.setAge(20);
        user.setPassword("123");
        user.setUsername("令狐冲");
        //调用mv的方法
        //user对象存储到mv对象中,同时也会把user对象存入到requst对象
        mv.addObject("user",user);
        //想跳转的页面
        mv.setViewName("success");
        return mv;
    }

Forward or redirect

  //返回值类型是ModelAndView
    @RequestMapping("/testForwardOrRedirect")
    public String testForwardOrRedirect(){
        System.out.println("testForwardOrRedirect执行了");
//        return "forward:/WEB-INF/pages/success.jsp";
        
        //不用加项目名称,框架已经加好
        return "redirect:/index.jsp";
    }

Filtering the data value in response to static resources json

<%--
  Created by IntelliJ IDEA.
  User: Yuan
  Date: 2019/7/22
  Time: 14:39
  To change this template use File | Settings | File Templates.
--%>
<%@ 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>
   
    <br>
    <button id="btn">发送ajax请求</button>

</body>
</html>

Click on the above events can not respond because DispatcherServlet the static resources to intercept

solution

Tell the front controller, which does not block static resources

img

Jso response to a request of a data value transmitted ajax

<%--
  Created by IntelliJ IDEA.
  User: Yuan
  Date: 2019/7/22
  Time: 14:39
  To change this template use File | Settings | File Templates.
--%>
<%@ 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(){
               $.ajax({
                   //编写json格式,设置属性和值
                   //url:请求服务器的路径
                   url:"user/testAjax",
                   //contentType:发送内容给服务器是的编码类型
                   contentType:"application/json;charset=UTF-8",
                   //data:发送到服务器的数据
                   data:'{"username":"hehe","password":"123","age":"20"}',
                   //dataType预期服务器返回的类型
                   dataType:"json",
                   //tpye,请求方式
                   type:"post",
                   //success:请求成功后的回调函数
                   success:function(data){
                            //data服务器端响应的json的数据,进行解析


                   }
               });
           });
        });
    </script>
</head>
<body>
    <br>
    <button id="btn">发送ajax请求</button>
</body>
</html>
 @RequestMapping("/testAjax")
    public void testAjax(@RequestBody String body){
        System.out.println("ajax执行了....");
        System.out.println(body);
    }

img

Upload the file upload Principle Analysis

A necessary prerequisite for file uploads

1.form form enctype value must be: multipart / form-data

2.method property values ​​must be Post

3. Provide a file selection field

Guess you like

Origin www.cnblogs.com/train99999/p/11229213.html
Recommended