1. 程式人生 > >java_springMVC_獲取post請求的引數

java_springMVC_獲取post請求的引數

1. 獲取post請求的引數

其實和get請求的獲取引數方式一樣,在此單列出來主要是為了說明幾種特殊情況,如方式一中有陣列、方式二中使用類、以及使用ajax

為了資料型別更全一點,我的引數中有字串型、數字型,及下拉框、複選框、單選框。

先寫一個addUser.jsp頁面如下:

<form action="" method="post">

<p><input type="test" name="username" placeholder="請輸入使用者名稱。。。" autofocus="autofocus"/></p>

<p><input type

="test" name="age" placeholder="請輸入年齡。。。"/></p>

<p><input type="test" name="phone" placeholder="請輸入電話。。。"/></p>

<p>省份:

<select>

<option value="000001">北京市</option>

<option value="000002">天津市</option>

<option value="000003">上海市</option>

</select>

</

p>

<p>愛好:

<label><input type="checkbox" name="intesters" value="1"/>爬山</label>

<label><input type="checkbox" name="intesters" value="2"/>旅遊</label>

<label><input type="checkbox" name="intesters" value="3"/>打球</label>

</p>

<p>性別:

<label><

input type="radio" name="sex" value="1"/></label>

<label><input type="radio" name="sex" value="2"/></label>

<label><input type="radio" name="sex" value="3"/>保密</label>

</p>

<p><input type="submit" value="提交"/></p>

</form>

Controller類中增加一個addUser方法:

@RequestMapping(value="addUser", method=RequestMethod.GET)

public String addUser(){

return"user/addUser";

}

可得到如下頁面:

 

上面的form表單中的action還沒填值,下面寫上不同的內容,呼叫不同的方法接受表單內容。

方式一:使用getParameter

action的值改為:saveUser

controller中增加方法:

// 獲取post請求引數(getParameter)

@RequestMapping(value="saveUser", method=RequestMethod.POST)

public String saveUser(HttpServletRequest request){

String username= request.getParameter("username");

Integer age = Integer.parseInt(request.getParameter("age"));

String phone = request.getParameter("phone");

String city = request.getParameter("city");

String[] interests = request.getParameterValues("interests");

Integer sex = Integer.parseInt(request.getParameter("sex"));

System.out.println("username : " + username);

System.out.println("age : " + age);

System.out.println("phone : " + phone);

System.out.println("city : " + city);

System.out.println("interests : ");

for(String interest: interests){

System.out.println("    interest : " + interest);

}

System.out.println("sex : " + sex);

return"login/welcome";

}

 

點提交按鈕,後臺輸出:

 

需要注意的是,如果是一個數組,就不能使用getParameter了,如form表單中的興趣愛好,需要使用String[] interest = request.getParameterValues(“interest”);

如果名稱使用中文,會出現亂碼,如,輸入張三,後臺收到:

 

web.xml中新增如下配置即可解決中文亂碼問題:

<!-- 亂碼問題,post請求亂碼 -->

<filter>

<filter-name>encoding</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-mapping>

<filter-name>encoding</filter-name>

<url-pattern>/*</url-pattern>

</filter-mapping>

再次使用中文:  

方式二:直接使用引數

action的值改為:saveUser2

建立一個User類:

 

併為user類的成員變數增加getset方法。

controller類中增加方法:

// 獲取post請求引數(直接使用引數)

@RequestMapping(value="saveUser2", method=RequestMethod.POST)

public String saveUser2(String username,User user){

System.out.println("username - " + username);

System.out.println("user.username - " + user.getUsername());

return"login/welcome";

}

 

點提交,後面輸出如下:

 

說明直接使用username為引數可以接受,直接使用類也可以接受。

注:之所以可以使用類為引數來接受資料,是spring的反射機制。即,form表單中的標籤中的name屬性:

 

要和User類中的成員變數名相同,更準確的說,應該和成員變數對應的set方法對應,即:User類中的setUsername方法將接受form表單中name屬性為username的值。

相關推薦

java_springMVC_獲取post請求引數

1. 獲取post請求的引數其實和get請求的獲取引數方式一樣,在此單列出來主要是為了說明幾種特殊情況,如方式一中有陣列、方式二中使用類、以及使用ajax。為了資料型別更全一點,我的引數中有字串型、數字型,及下拉框、複選框、單選框。先寫一個addUser.jsp頁面如下:&l

springboot 獲取post請求引數

注意,request body中獲取引數時使用流獲取,但是request的流只能使用一次, 給出的辦法就是在獲取流之前對流進行復制否則會出異常 (request body miss) spring請求的鏈式執行順序為Filter-->攔截器-->controller 1:首先自定義一個類繼

【httplistener監聽獲取Post請求引數

使用httplistener監聽來自客戶端的http請求,對於Get請求的資料可以通過Request.QueryString["引數"]獲取 而對於來自客戶端的Post請求則不能使用Request[""]獲取,需要將獲取分析請求流中的資料拿到引數 using System

java獲取GET和POST請求引數

一 獲取請求方式 request.getMethod();    get和post都可用, 二 獲取請求型別 request.getContentType();   get和post都可用,示例值:application/json ,multipart/form-data, application/x

servlet如何獲取post請求引數

通常我們利用request.getParameter(引數名稱)來獲取url上面或者ajax.data提交上來的引數,但是例如用httpClient中的httpPost.setEntry()的方法提交的引數是沒有引數名稱的, 使用request.getParameter(引

Jfinal框架下Controller無法獲取post請求傳入的引數

最近幾天在整合專案,把一個系統要分離成兩個系統,分離過程中出現了問題。 未分離之前,當系統裡的所有訪問都是直接呼叫類方法即可;分離之後,要通過http請求去呼叫相關介面。使用get請求時,通過getPara()可以直接獲取引數,但是使用post沒有引數,除錯發現訪問控制器的

spring boot 常見http get ,post請求引數處理

 在定義一個Rest介面時通常會利用GET、POST、PUT、DELETE來實現資料的增刪改查;這幾種方式有的需要傳遞引數,後臺開發人員必須對接收到的引數進行引數驗證來確保程式的健壯性 GET 一般用於查詢資料,採用明文進行傳輸,一般用來獲取一些無關使用者資訊的資料 POST

Spring MVC(三)控制器獲取頁面請求引數以及將控制器資料傳遞給頁面和實現重定向的方式

首先做好環境配置 在mvc.xml裡進行配置   1.開啟元件掃描   2.開啟基於mvc的標註   3.配置試圖處理器 1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www

js獲取url請求引數

function getQueryString(name) { var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)"); var r = decodeURI(window.location.search).substr

tomcat訪問(access)日誌配置和記錄Post請求引數

一、配置與說明 tomcat訪問日誌格式配置,在config/server.xml裡Host標籤下加上 1 2 3 <Valve className="org.apache.catalina.valves.AccessLogValve" dire

C 獲取post請求的json字串

                引言:丟擲問題        &

SpringMVC中post請求引數註解@requestBody使用問題

  一、httpClient傳送Post 原文https://www.cnblogs.com/Vdiao/p/5339487.html 1 public static String httpPostWithJSON(String url) throws Exception { 2

使用formidable上傳獲取post請求上傳的檔案注意點

總結使用formidable曾經踩過的坑 遇到的問題都是因為使用post上傳檔案讀取不到的files 目前主要遇到的是以下兩種情況 沒有在表單<form></form>上新增enctype="multipart/form-data" 在有type

@ModelAttribute獲取POST請求的FORM表單資料

1.JSP表單如下: <form method="post" action="hao.do"> a: <input id="a" type="text" name="a" /> b: <input id="b" type="text" nam

SpringCloud Gateway獲取post請求體(request body)

獲取spring cloud gateway POST請求體的時候,會有很多坑,網上大多數解決方案是 /** 這種方法在spring-boot-starter-parent 2.0.6.RELEASE + Spring Cloud Finchley.SR2 body 中生效, 但是在spring-boot

Android Retrofit Post請求引數長度限制問題 retrofit sendto failed: ECONNRESET (Connection reset by peer)

retrofit sendto failed: ECONNRESET (Connection reset by peer)【android客戶端Post請求引數長度限制(引數過大)】解決辦法   一、問題的出現 post請求後臺,當引數字元太長超過900字時會報sendto

前後端分離。前端POST請求引數過長,導致400錯誤解決辦法及分析

這兩天做好的功能要上線了。但是測試的時候忽然發現當POST提交資料量多大時,會導致後端報400錯誤。最開始以為瓶頸存在於tomcat,因為tomcat預設能接受的POST請求大小為2M,所以手動修改tomcat server.xml 檔案,將接受POST大小修改為不限制。

get請求和post請求引數中文亂碼的解決

首先出現中文亂碼的原因是tomcat預設的編碼方式是"ISO-8859-1",這種編碼方式以單個位元組作為一個字元,而漢字是以兩個位元組表示一個字元的。 post請求引數中文亂碼的解決辦法   對於post請求,請求中問亂碼的兩種解決辦法。   (1): request.set

java web專案介面請求get,post請求引數中文亂碼解決

java web專案介面請求get,post請求引數中文亂碼解決 在開發過程中,有時候會碰到get,post請求引數中文亂碼。 原因:  Http請求傳輸時將url以ISO-8859-1編碼,伺服器收到位元組流後預設會以ISO-8859-1編碼來解碼成字元

利用swagger-ui頁面傳送POST請求引數遇到的中文引數亂碼的問題

前端時間將同事開發的半成品專案介面做成視覺化介面,即swagger-ui,用於展示。這個如何配置我就不多說了,網上相關的教程應該也是一搜一大堆......  本地測試了相關介面後,除了幾個介面返回: {"resultData":"","serviceTime":15172