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;
}
}