1. 程式人生 > >Spring學習筆記 獲取請求物件和請求頭

Spring學習筆記 獲取請求物件和請求頭

1.效果圖

效果圖

2.新增檔案

《 Spring學習筆記<二> 獲取請求引數和Cookie》中,成功實現了獲取請求引數和cookie的功能,這次在原有的專案基礎上再新增三個檔案:

test_restput.jsp檔案(WebContent資料夾下面):

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>請求方式測試02</title> </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"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<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下載地址:戳我