我正在开发一个Spring MVC应用程序,我遇到了以下问题。
进入 FreeMarker 视图(但使用FreeMarker制作视图并不重要)我有这样的表格:
<form id="reg-form" name="reg-form" action="<@spring.url '/iscrizioneStep2' />" method="post">
<fieldset>
<div class="form-group">
<label for="cf">Codice fiscale:</label>
<input type="text" id="cf" name="codiceFiscale" class="form-control input-mm" placeholder="Inserisci il tuo codice fiscale" data-validation="[NOTEMPTY, NOSPACE, L==16, CF]" data-validation-label="codice fiscale" aria-required="true" tabindex="10">
</div>
<div class="form-group">
<div class="g-recaptcha" data-sitekey="6LcOfhcTAAAAAE3D2hsa3UcyTQ0PI4upcZ759FDa" tabindex="20"></div>
</div>
<button type="submit" class="btn btn-block submit-btn" aria-label="prosegui la registrazione" tabindex="30">Passaggio 2</button>
</fieldset>
</form>
正如您所见,表单已提交给此操作: action =“&lt; @ spring.url'/ iscrizioneStep2'/&gt;”执行 POST 请求
这会生成一个针对此URL的POST请求(我使用FireBug看到它):
http://localhost:8080/iam-ssum-public/iscrizioneStep2?codiceFiscale=AAAAAAAAAAAAAAAA&g-recaptcha-response=
所以我认为它应该将具有 name =“codiceFiscale”的输入字段发送到POST请求中。
然后我有这个控制器方法:
@RequestMapping(value = "/iscrizioneStep2", method = RequestMethod.POST)
public String iscrizioneStep2(@RequestBody(required = true) IscrizioneStep1Form iscrizioneStep1Form, Model model)
throws APIException {
/*
* Verifica se l'utenza è attivata per il codice fiscale
*/
String codiceFiscale = iscrizioneStep1Form.getCodiceFiscale();
..............................................................
..............................................................
..............................................................
return "myView";
}
因此,在post请求中发送的数据应该放在 IscrizioneStep1Form iscrizioneStep1Form 参数中,即:
public class IscrizioneStep1Form {
/**
* Codice fiscale
*/
private String codiceFiscale;
public String getCodiceFiscale() {
return codiceFiscale;
}
public void setCodiceFiscale(String codiceFiscale) {
this.codiceFiscale = codiceFiscale;
}
}
但问题是这个HTTP POST请求不是由 iscrizioneStep2()方法处理的。当我提交表单时,请不要进入此方法并进入Eclipse控制台,我会收到以下错误消息:
11:55:43,949 WARN [org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver] (http-localhost/127.0.0.1:8080-6) Handler execution resulted in exception: Content type 'application/x-www-form-urlencoded' not supported
为什么呢?我错过了什么?我该如何解决这个问题?
答案 0 :(得分:0)
尝试将heders
param添加到RequestMapping
注释:
@RequestMapping(value = "/iscrizioneStep2", method = RequestMethod.POST,
headers = "content-type=application/x-www-form-urlencoded")
答案 1 :(得分:0)
尝试从您的请求方法中删除@RequestBody(required = true)
这里Spring Mvc Rest Webservice jstl form submittion HTTP Status 415 Content type 'application/x-www-form-urlencoded' not supported也有同样的问题。
答案 2 :(得分:0)
没有内置转换器知道如何将'application/x-www-form-urlencoded
类型的内容转换为IscrizioneStep1Form
。只需省略@RequestBody
即可。表单数据自动映射到对象。
答案 3 :(得分:0)
问题在于,当我们使用 application / x-www-form-urlencoded 时,Spring并不将其理解为RequestBody。所以,如果我们想要使用它 我们必须删除 @RequestBody 注释。
然后尝试以下方法:
@RequestMapping(value = "/iscrizioneStep2", method = RequestMethod.POST, consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
public String iscrizioneStep2(IscrizioneStep1Form iscrizioneStep1Form, Model model) throws APIException {
//method body where
}
请注意,删除了注释 @RequestBody
回答:Http Post request with content type application/x-www-form-urlencoded not working in Spring