SpringMVC-返回值

一、普通文本、HTML返回值

// 普通文本
@Controller
public class UserController {
    @RequestMapping(value = "/plainText", produces = "text/plain; charset=UTF-8")
    @ResponseBody
    public String plainText() {
        return "This is 普通文本";
    }
}

// HTML
@RequestMapping(value = "/html", produces = "text/html; charset=UTF-8")
    @ResponseBody
    public String html() {
        return "

This is 普通文本

"; }

二、XML返回值(有两种方式)

  • 手动拼接xml格式,繁琐易错
  // 方法1
    @RequestMapping(value = "/xml1", produces = "application/xml; charset=UTF-8")
    @ResponseBody
    public String xml1() {
        Person person = new Person();
        person.setName("Tom");
        person.setAge(20);

        Car car = new Car();
        car.setName("BWM");
        car.setPrice(1234);
        person.setCar(car);
        // 加上xml的识别
        return ""
                + ""
                + ""
                +"";
    }

  • 引入三方库进行返回如下:
 
        
            javax.xml.bind
            jaxb-api
            2.4.0-b180830.0359
        

        
            javax.xml
            jaxb-impl
            2.1
        

配置文件设置

  
    

类需要加上xml的注解

package com.mj.domain;

import com.sun.xml.txw2.annotation.XmlElement;

import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name = "person")
public class Person {
    private String name;
    private Integer age;
    private Car car;

    @XmlAttribute
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
    @XmlAttribute
    public Integer getAge() {
        return age;
    }

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

    @XmlElement
    public Car getCar() {
        return car;
    }

    public void setCar(Car car) {
        this.car = car;
    }
}

测试类:

    // 方法2
    @RequestMapping(value = "/xml2")
    @ResponseBody
    public Person xml2() {
        Person person = new Person();
        person.setName("Tom");
        person.setAge(20);

        Car car = new Car();
        car.setName("BWM");
        car.setPrice(1234);
        person.setCar(car);
        // 加上xml的识别
        return person;
    }

三、JSON数据格式返回(两种方式)

  • 直接手拼JSON字符串,如下
  @RequestMapping(value = "/json1", produces = "application/json; charset=UTF-8")
    @ResponseBody
    public String json1() {
        return "{\"name\":\"\"Jack, \"age\":20},\"car:{\"name\":\"BWM\",\"price\":1234}}\"";
    }
  • 引入三方库,如下配置
    
        
            com.fasterxml.jackson.core
            jackson-databind
            2.11.2
        

配置文件




    
    
        
        
            
            
                
            

            
            
                
            
            
            
            
                
            
        
    

    
    

测试类:

 @RequestMapping("/json2")
    @ResponseBody // 默认返回JSON
    public Student json2() {

        List list = new ArrayList();
        list.add("haha1");
        list.add("haha2");
        list.add("haha3");
        Student student = new Student();
        student.setName("xiaomage");
        student.setNickNames(list);
        student.setAge(12);

        Dog dog = new Dog();
        dog.setName("haha");
        dog.setPrice(123);
        student.setDog(dog);

        return student;
    }

 四、返回视图

  • 可以利用ModelAndView将数据和视图绑定到一起,返回给客户端
    /*
     在Java代码中,路径问题总结
     1、假设请求路径是:"http://IP地址:端口/context_path/path1/path2/path3"
     2、假设转发路径是:"/page/test.jsp"
       1> 以斜线(/)开头,参考路径是context_path
       2> 最终转发路径是:"http://IP地址:端口/context_path" + "/page/test.jsp"
     3、假设转发路径是:"page/test.jsp"
       1> 不以斜线开头,参考路径是当前请求路径的上一层路径
       2> 最终转发路径是:"http://IP地址:端口/context_path/path1/path2" + "/page/test.jsp"
       
       
      在jsp、html代码中、路径问题总结
       1、假设请求路径是:"http://IP地址:端口/context_path/path1/path2/path3"
       2、假设跳转路径是:"/page/test.jsp"
            1> 以斜线(/)开头,参考路径是"http://IP地址:端口"
            2> 最终转发路径是:"http://IP地址:端口" + "/page/test.jsp"
       3、假设转发路径是:"page/test.jsp"
            1> 不以斜线开头,参考路径是当前请求路径的上一层路径
            2> 最终转发路径是:"http://IP地址:端口/context_path/path1/path2" + "/page/test.jsp"
    */
    
@Controller
public class JspController {

    @RequestMapping("/path1/jsp1")
    public ModelAndView jsp1() {
        ModelAndView mv = new ModelAndView();
        Dog dog = new Dog();
        dog.setName("大黄");
        dog.setPrice(200);
        // 本质就是request.setAttribute
        mv.addObject(dog);
        // 转发
        mv.setViewName("/page/jsp1.jsp");
        return mv;
    }
}

/*
 在Java代码中,路径问题总结
 1、假设请求路径是:"http://IP地址:端口/context_path/path1/path2/path3"
 2、假设转发路径是:"/page/test.jsp"
   1> 以斜线(/)开头,参考路径是context_path
   2> 最终转发路径是:"http://IP地址:端口/context_path" + "/page/test.jsp"
 3、假设转发路径是:"page/test.jsp"
   1> 不以斜线开头,参考路径是当前请求路径的上一层路径
   2> 最终转发路径是:"http://IP地址:端口/context_path/path1/path2" + "/page/test.jsp"
   
   
  在jsp、html代码中、路径问题总结
   1、假设请求路径是:"http://IP地址:端口/context_path/path1/path2/path3"
   2、假设跳转路径是:"/page/test.jsp"
        1> 以斜线(/)开头,参考路径是"http://IP地址:端口"
        2> 最终转发路径是:"http://IP地址:端口" + "/page/test.jsp"
   3、假设转发路径是:"page/test.jsp"
        1> 不以斜线开头,参考路径是当前请求路径的上一层路径
        2> 最终转发路径是:"http://IP地址:端口/context_path/path1/path2" + "/page/test.jsp"
*/

  • 不加@ResponseBody的String返回值,也代表viewName

SpringMVC-返回值_第1张图片

  • 在viewName前面加上"redirect:"表示重定向,比如:"redirect:/page/jsp4.jsp"
  •  
  • 可以使用直接配置请求路径和viewName,如果以下两个都存在,会以java代码优先级高。也就是当没有Controller去处理这个path时才会交给配置文件。
 
 @RequestMapping("/jsp5")
    public String jsp5() {
        // 转发
        return "/WEB-INF/page/jsp4.jsp";
    }

可以通过InternalResourceViewResolver设置试图路径的公共前缀、后缀,受InternalResourceViewResolver影响的有如下:

  • 通过返回值ModelAndView设置viewName
  • 通过返回值String设置的viewName
  • 通过设置的viewName

可以配置多个InternalResourceViewResolver

  • order值越小,优先级越高

SpringMVC-返回值_第2张图片

@Controller
@RequestMapping("/test")
public class Jsp2Controller {

    @RequestMapping("/jsp1")
    public String jsp1() {
        return "jsp1";
    }


    @RequestMapping("/jsp2")
    public ModelAndView jsp2() {
        return new ModelAndView("jsp2");
    }

}

以下两种情况不受InternalResourceViewResolver影响:

  • 在viewName前面加上"forward:"活"redirect:"
  • 通过ModeAndView的setView方法

SpringMVC-返回值_第3张图片

你可能感兴趣的:(Java后台开发,java,spring,mysql)