springmvc——视图控制器与json转换

1.视图控制器实现页面跳转
试图控制器的页面跳转是用于登录后台页面,后台登录的页面存放于私人文件夹,文法直接访问,当我们不需要后台处理逻辑只要跳转页面时可以用视图控制器

 <mvc:view-controller path="/" view-name="index"/>
	//当我们跳到后台页面的首页后,在次跳转时,无法跳过去,使用下面一行
	//默认会帮我们注册默认处理请求,参数和返回值的类
    <mvc:annotation-driven/>

2.转换json
通过全局配置(MessageConverter),自动转换JSON
两个测试的入口

@RequestMapping("/query_stu/{id}")
    public Student queryStudent(@PathVariable Integer id, HttpServletResponse resp) {
        return studentService.queryStudentById(id);
    }

    @RequestMapping("/query_stus")
    public List<Student> queryStudentList(HttpServletResponse resp) {
        return studentService.queryStudentList();
    }

pojo类

public class Student {
    private Integer id;
    private String sname;
    private Integer age;
    private String nickName;
    private String gender;

    @Override
    public String toString() {
        return "Student{" +
                "sname='" + sname + '\'' +
                ", age=" + age +
                ", nickName='" + nickName + '\'' +
                ", gender='" + gender + '\'' +
                '}';
    }

    public String getSname() {
        return sname;
    }

    public void setSname(String sname) {
        this.sname = sname;
    }

    public Integer getAge() {
        return age;
    }

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

    public String getNickName() {
        return nickName;
    }

    public void setNickName(String nickName) {
        this.nickName = nickName;
    }

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }
}

xml配置

  <mvc:message-converters>
            <bean class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">
                <property name="defaultCharset" value="UTF-8" />
                <property name="fastJsonConfig">
                    <!-- 设置转换JSON的参数 -->
                    <bean class="com.alibaba.fastjson.support.config.FastJsonConfig">
                        <property name="dateFormat" value="yyyy-MM-dd HH:mm:ss" />
                    </bean>
                </property>
                <property name="supportedMediaTypes">
                    <list>
                        <value>text/html;charset=UTF-8</value>
                        <value>application/json;charset=UTF-8</value>
                    </list>
                </property>
            </bean>
        </mvc:message-converters>

servise层

@Service
public class StudentService {

    public Student queryStudentById(Integer id) {
        Student student = new Student();
        student.setId(id);
        student.setSname("张三");
        student.setNickName("老大");
        student.setGender("男");
        student.setAge(8);
        return student;
    }

    public List<Student> queryStudentList() {
        List<Student> students = new ArrayList<>();
        for(int i = 0; i < 10; i++) {
            Student student = new Student();
            student.setId(i+1);
            student.setSname("张三"+i);
            student.setNickName("老大"+i);
            student.setGender("男");
            student.setAge(i+1);
            students.add(student);
        }
        return students;
    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值