Spring学习笔记 获取请求对象和请求头

1.效果图

Spring学习笔记 获取请求对象和请求头_第1张图片

2.添加文件

在《 Spring学习笔记<二> 获取请求参数和Cookie》中,成功实现了获取请求参数和cookie的功能,这次在原有的项目基础上再添加三个文件:

test_restput.jsp文件(WebContent文件夹下面):

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

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>请求方式测试02title>
head>
<body>

    <form action="springmvc/put02/testRequestPOJO" method="post">
        username: <input type="text" name="username"><br>
        password: <input type="password" name="password"><br>
        email: <input type="text" name="email"><br> 
        age: <input type="text" name="age"><br>
        city: <input type="text" name="address.city"><br> 
        province: <input type="text" name="address.province"><br> 
        <input type="submit" value="testRequestPOJO">
    form>
    <br />
    <br />


    <form action="springmvc/put02/testRequestHeader">
        <input type="submit" value="testRequestHeader" />
    form>
    <br />
    <br />


    <form action="springmvc/put02/testRestModelView">
        <input type="submit" value="testRestModelView" />
    form>
    <br />
    <br />

body>
html>

success02.jsp文件(WEB-INF/views/文件夹下面):

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

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>成功界面title>
head>
<body>
time: ${requestScope.time}<br><br>
<h4>恭喜您成功了h4>
body>
html>

testRestPut02.Java(在com.shi.springmvc.handlers包下面)

package com.shi.springmvc.handlers;

import java.util.Date;

import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

@RequestMapping("/springmvc/put02")
@Controller
public class TestRestPut02 {

    private String SUCCESS = "success";

    @RequestMapping(value="/testRequestPOJO", method=RequestMethod.POST)
    public String testRequestPOJO(User user){
        System.out.println("用户信息为:" + user);
        return SUCCESS;
    }   

    @RequestMapping(value="/testRequestHeader")
    public String testRequestHeader(@RequestHeader(value="Accept-Language") String language){
        System.out.println("testRequestHeader Accept-Languge:" + language);
        return SUCCESS;
    }

    @RequestMapping(value="/testRestModelView")
    public ModelAndView  testRestModelView(){
        ModelAndView modelAndView = new ModelAndView(SUCCESS+"02");
        modelAndView.addObject("time", new Date());
        System.out.println("testRestModelView执行成功");
        return modelAndView;
    }

}

3.spring mvc获取请求对象

之前获取到的都是简单的字符串,数字什么的请求参数,都仅仅是一个简单的字段,但是如果用户提交的是一个form表单,里面的内容和字段非常多呢?如果我们继续使用获取参数的方法去获取所有请求参数字段,会变得非常麻烦,那么,我们直接把这些请求字段封装成一个对象吧。

    @RequestMapping(value="/testRequestPOJO", method=RequestMethod.POST)
    public String testRequestPOJO(User user){
        System.out.println("用户信息为:" + user);
        return SUCCESS;
    }   

我们根据表单的具体字段建立两个javabean对象:

public class User {

    private Integer id;

    private String username;
    private String password;
    private String email;
    private int age;
    private Address address;

    public Integer getId() {
        return id;
    }

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

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public int getAge() {
        return age;
    }

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

    public Address getAddress() {
        return address;
    }

    public void setAddress(Address address) {
        this.address = address;
    }

    @Override
    public String toString() {
        return "User [id=" + id + ", username=" + username + ", password=" + password + ", email=" + email + ", age="
                + age + ", address=" + address + "]";
    }

}
public class Address {

    private String province;
    private String city;

    public String getProvince() {
        return province;
    }

    public void setProvince(String province) {
        this.province = province;
    }

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    @Override
    public String toString() {
        return "Address [province=" + province + ", city=" + city + "]";
    }
}

使用这种方法,我们就能够轻松把用户请求的表单数据转化成对象,就可以很方便的进行数据操作了。

4.spring mvc获取请求头的内容:@RequestHeader

    @RequestMapping(value="/testRequestHeader")
    public String testRequestHeader(@RequestHeader(value="Accept-Language") String language){
        System.out.println("testRequestHeader Accept-Languge:" + language);
        return SUCCESS;
    }

通过@RequestHeader,我们可以很方便的获取到请求头的具体内容,具体效果请看上面的效果图。

5.将信息写入请求头中,并通过响应视图展示出来:ModelAndView

    @RequestMapping(value="/testRestModelView")
    public ModelAndView  testRestModelView(){
        ModelAndView modelAndView = new ModelAndView(SUCCESS+"02");
        modelAndView.addObject("time", new Date());
        System.out.println("testRestModelView执行成功");
        return modelAndView;
    }

这里将当前时间信息写进了请求域,并通过视图展示出来了,具体效果请看上面的效果图。

最后附上demo下载地址:戳我

你可能感兴趣的:(spring,Spring学习)