SpringMVC 三 處理請求資料

2022-01-12 14:15:57 字數 3270 閱讀 9095

1.form元件

action屬性表示提交位址

method屬性表示提交方式

表單內容可以有< input>、< textarea>、< button>、< select>、< option>、< optgroup>、< fieldset>、< label>等標籤

2.傳遞字面量引數

(1)在處理請求的方法中,加入相對應的形參,保證形參的名字 和 傳遞資料的名 保持一致,就可以自動賦值。

(2)如果名字不一樣,可以用@requestparam註解 標識 哪個形參對應哪個資料。

public string param(@requestparam(value="name",required=false,defaultvalue="守林鳥")string username,string password,string age)

<

form

action

="param"

method

="post"

>

username:

<

input

type

="text"

name

="name"

/>

<

br />

password:

<

input

type

="text"

name

="password"

/>

<

br />

age:

<

input

type

="text"

name

="age"

/>

<

br />

<

input

type

="submit"

value

="提交"

>

form

>

顯然,如果引數太多則**臃腫

4.非自定義的資料則直接用相關註解修飾形參

5.傳遞pojo型別

要求實體類物件中的屬性名要和表單中的name屬性值一致,支援級聯關係(pojo封裝pojo)。

public

string param(user user)

<

form

action

="param"

method

="post"

>

username:

<

input

type

="text"

name

="username"

/>

<

br />

password:

<

input

type

="text"

name

="password"

/>

<

br />

age:

<

input

type

="text"

name

="age"

/>

<

br />

province:

<

input

type

="text"

name

="address.province"

/>

<

br />

city:

<

input

type

="text"

name

="address.city"

/>

<

br />

<

input

type

="submit"

value

="提交"

>

form

>

通過[.]來實現級聯關係,如果還有下一層,再來乙個[.]。

6.獲取servletapi

直接在響應方法裡寫形參,例如httpservletrequest、httpservletresponse、httpsession

7.傳遞陣列

直接用(型別 形參)就可以,在表單中相同名字的都被弄進陣列

8.傳遞集合

不支援這種形參方式,需要將集合放在乙個類裡作為屬性封裝。例如乙個list型別在form表單中寫list[0].username、list[1].password。

9.往作用域中放值

10.中文亂碼問題

在web.xml中配置編碼過濾器

<

filter

>

<

filter-name

>characterencodingfilter

filter-name

>

<

filter-class

>

org.springframework.web.filter.characterencodingfilter

filter-class

>

<

init-param

>

<

param-name

>encoding

param-name

>

<

param-value

>utf-8

param-value

>

init-param

>

filter

>

<

>

<

filter-name

>characterencodingfilter

filter-name

>

<

url-pattern

>/*

url-pattern

>

>

SpringMvc原始碼(二) 處理請求過程

過程 1.請求首先進入到frameworkservlet的processrequest中。2.呼叫dispatcherservlet中的doservice方法,對請求進行預設定,doservice方法在frameworkservlet為抽象方法。3.最後呼叫dispatcherservlet的dod...

SpringMvc原始碼(二) 處理請求過程

過程 1.請求首先進入到frameworkservlet的processrequest中。2.呼叫dispatcherservlet中的doservice方法,對請求進行預設定,doservice方法在frameworkservlet為抽象方法。3.最後呼叫dispatcherservlet的dod...

SpringMVC學習個人總結(一) 處理請求引數

1.搭建環境 在web.xml中配置視 析器 dispatcherservlet org.springframework.web.servlet.dispatcherservlet 宣告mvc配置檔案的位置 contextconfiglocation classpath springconfigur...