SpringMVC 返回值类型控制 / 跳转方式 / JSON响应

一、目录结构

SpringMVC 返回值类型控制 / 跳转方式 / JSON响应_第1张图片

二、SpringMVC返回值类型控制/跳转方式/JSON响应

1.创建UserController.java

package com.mollen.controller;

import com.mollen.bean.User;
import com.sun.deploy.net.HttpResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

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

/**
 * @ClassName: HelloController
 * @Auther:     Mollen
 * @CreateTime: 2018-11-09  14:49:58
 * @Description:
 */

@Controller
@RequestMapping(path="/user")
public class UserController {

    /******************************【返回值类型控制】**************************************

    /**
     *
     * 1.返回值为字符串:
     *      视图解析器解析为jsp物理路径:/WEB-INF/pages/success.jsp
     */
    @RequestMapping("respStr1")
    public String respStr1(User user, HttpSession session){
        session.setAttribute("login",user);
        return "success";
    }

    /**
     *
     * 2.使用request转发页面
     */
    @RequestMapping("respStr2")
    public void respStr2(User user, HttpServletRequest  request, HttpServletResponse response) throws ServletException, IOException {
        request.setAttribute("login",user);
        request.getRequestDispatcher("/WEB-INF/pages/success.jsp").forward(request,response);
    }

    /**
     * 3.使用response重定向页面
     */
    @RequestMapping("/respStr3")
    public void respStr3(User user,  HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
        response.sendRedirect(request.getContextPath()+"/index.jsp");
    }

    /**
     * 4.使用response响应json结果
     */
    @RequestMapping("/respStr4")
    public void respStr4(User user,  HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
        //乱码处理
        response.setCharacterEncoding("utf-8");
        response.setContentType("application/json;charset=utf-8");

        response.getWriter().write("jsonData");
    }

    /**
     * 5.返回ModelAndView
     */
    @RequestMapping("/mv")
    public ModelAndView mv(User user) throws ServletException, IOException {
        ModelAndView mv = new ModelAndView();
        //将数据保存到Model中
        mv.addObject("logined", user);
        //设置视图名称
        mv.setViewName("success");
        return mv;
    }


    /******************************【页面跳转方式控制】**************************************/

    /**
     * 1.转发:forward
     */
     @RequestMapping("/forward")
     public String forward(User user,HttpSession session) throws ServletException, IOException {
         session.setAttribute("logined", user);
         return "forward:/WEB-INF/pages/success.jsp";
     }


    /**
     * 2.重定向:redirect
     */
    @RequestMapping("/redirect")
    public String redirect(User user,HttpSession session) throws ServletException, IOException {
        session.setAttribute("logined", user);
        return "redirect:/index.jsp";
    }


    /******************************【响应JSON数据格式】**************************************/

    /**
     * 1.接受请求/使用@RequestBody必须是post请求
     * @return
     */
    @RequestMapping(path="/json1")
    public @ResponseBody User  json(@RequestBody User user) {
        System.out.println(user);
        return user;
    }

}

2.创建response.jsp测试json响应

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


    Title
    
    




testJson

3.web.xml配置



    
    
        dispatcherServlet
        org.springframework.web.servlet.DispatcherServlet

        
        
            contextConfigLocation
            classpath:springmvc.xml
        

        
        1
    

    
        dispatcherServlet
        /
    

    
    
        default
        *.html
    

    
        default
        *.js
    

    
        default
        *.css
    

    
    
        characterEncodingFilter
        org.springframework.web.filter.CharacterEncodingFilter
        
            encoding
            UTF-8
        
    

    
        characterEncodingFilter
        /*
    


4.pom.xml配置:



    4.0.0

    com.mollen
    springmvc_exam
    1.0-SNAPSHOT
    war

    

        
            org.springframework
            spring-context
            5.0.3.RELEASE
        

        
            org.springframework
            spring-web
            5.0.3.RELEASE
        

        
            org.springframework
            spring-webmvc
            5.0.4.RELEASE
        

        
            org.apache.tomcat
            tomcat-api
            9.0.10
            provided
        

        
        
            com.fasterxml.jackson.core
            jackson-databind
            2.9.0
        

        
            com.fasterxml.jackson.core
            jackson-core
            2.9.0
        

        
            com.fasterxml.jackson.core
            jackson-annotations
            2.9.0
        
        
    



5.springmvc.xml配置




    
    

    
    
        
        
    

    
    
        
        
            5242880
        
    

    
    


原文:https://blog.csdn.net/mollen/article/details/83993240

你可能感兴趣的:(后端)