簡體   English   中英

Spring MVC:錯誤400客戶端發送的請求在語法上是不正確的

[英]Spring MVC: Error 400 The request sent by the client was syntactically incorrect

這似乎是一個常見問題。 我已經完成了SO中給出的所有答案,但無法使其發揮作用。
我正在嘗試將Spring MVC + Freemarker集成到現有的Web應用程序中。 它適用於GET請求,Freemarker Template讀取Controller提供的java對象沒有任何問題。
但表單提交無法觸及Controller方法。 最后我讓log4j工作了。 這是我得到的錯誤:
錯誤

    HandlerMethod details: 
    Controller [application.entry.controller.UserController]
    Method [public void application.entry.controller.UserController.handleSave(java.lang.String)]

    org.springframework.web.bind.MissingServletRequestParameterException: 
Required String parameter 'action' is not present


Freemarker的:

<form method="POST" action="save.html">
  ------------
  <input type="submit" class="btnnew" name="saveWithoutValidation" value="Save Without Validation"></input>
  <input type="submit" class="btnnew" name="submit" value="Submit"></input>
</form>

context-root是PORTAL
為spring-servlet.xml

<bean id="viewResolver" class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
      <property name="cache" value="true"/>
      <property name="prefix" value=""/>
      <property name="suffix" value=".ftl"/>

web.xml中

<servlet>
        <servlet-name>spring</servlet-name>
        <servlet-class>
            org.springframework.web.servlet.DispatcherServlet
        </servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>spring</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

調節器

@RequestMapping(value="/save", method=RequestMethod.POST)
    public void handleSave(@RequestParam String action){

        if( action.equals("submit") ){
            System.out.println("Damn! You clicked submit");
        }
        else if( action.equals("saveWithoutValidation") ){
           System.out.println("Sweet! You want no string attached.");
        }

    }

對於日志,我試圖將log4j.logger.org.springframework.web=DEBUG添加到我現有的log4j.properties,但它不起作用。

@RequestParam String action表明請求中存在一個參數,其中名稱操作在表單中不存在。 你必須:

  1. 提交名為value的參數,例如<input name="action" />
  2. @RequestParam中將required參數設置為false ,例如@RequestParam(required=false)

我也有這個問題,我的解決方案也不同,所以在這里添加任何有類似問題的人。

我的控制器有:

@RequestMapping(value = "/setPassword", method = RequestMethod.POST)
public String setPassword(Model model, @RequestParameter SetPassword setPassword) {
    ...
}

問題是這應該是對象的@ModelAttribute ,而不是@RequestParameter 此錯誤消息與您在問題中描述的相同。

@RequestMapping(value = "/setPassword", method = RequestMethod.POST)
public String setPassword(Model model, @ModelAttribute SetPassword setPassword) {
    ...
}

另一個可能的原因是RequestMapping屬性的順序錯誤。 正如春季醫生所說:

@RequestMapping處理程序方法可以具有非常靈活的簽名。 支持的方法參數和返回值將在下一節中介紹。 除了BindingResult參數之外,大多數參數可以以任意順序使用 這將在下一節中介紹。

如果向下滾動文檔,您將看到BindingResult必須立即在模型屬性之后,因為每個請求可以有多個模型對象,因此可以有多個綁定

Errors或BindingResult參數必須遵循立即綁定的模型對象,因為方法簽名可能有多個模型對象,Spring將為每個模型對象創建一個單獨的BindingResult實例,因此以下示例將不起作用:

這是兩個例子:

BindingResult和@ModelAttribute的排序無效。

@RequestMapping(method = RequestMethod.POST)public String processSubmit(@ModelAttribute(“pet”)Pet pet,Model model,BindingResult result){...}注意,Pet和BindingResult之間有一個Model參數。 要使其正常工作,您必須按如下方式重新排序參數:

@RequestMapping(method = RequestMethod.POST)public String processSubmit(@ModelAttribute(“pet”)Pet pet,BindingResult result,Model model){...}

我遇到了一個類似的問題,發現像Date這樣的一些領域並沒有得到具體的價值,一旦得到價值的東西工作得很好。 請確保您的表單上沒有日期或任何其他需要具體值的字段。

根據錯誤:

Required String parameter 'action' is not present

需要在Spring的請求中存在一個名為action的請求參數,以將請求映射到處理程序handleSave

您粘貼的HTML顯示沒有此類參數。

控制器試圖在bean中找到“action”值,但根據你的例子,你沒有設置任何bean名稱“action”。 嘗試做name =“action”。 @RequestParam總是在bean類中找到。

我的問題是我的模型屬性后缺少BindingResult參數。

 @RequestMapping(method = RequestMethod.POST, value = "/sign-up", consumes = "application/x-www-form-urlencoded")
    public ModelAndView registerUser(@Valid @ModelAttribute UserRegistrationInfo userRegistrationInfo
                                HttpServletRequest httpRequest,
                                HttpSession httpSession) { ... } 

我添加了BindingResult我的控制器就變成了

@RequestMapping(method = RequestMethod.POST, value = "/sign-up", consumes = "application/x-www-form-urlencoded")
    public ModelAndView registerUser(@Valid @ModelAttribute  UserRegistrationInfo userRegistrationInfo, BindingResult bindingResult,
                                HttpServletRequest httpRequest,
                                HttpSession httpSession) { ..}

檢查答案@sashok_bg @sashko_bg Mersi mnogo

您的請求映射是/save ,但您的POST是/save.html 將POST更改為/save應該修復它。

請保留你的

<form method="POST" action="XYZ">

@RequestMapping(value="/XYZ", method=RequestMethod.POST)
    public void handleSave(@RequestParam String action){

您的表單操作屬性值必須與@RequestMapping值匹配,以便Spring MVC可以解析它。

另外,正如您所說的那樣,在更改后給出了404,為此,請您檢查控件是否進入handleSave()方法。

我想,因為你沒有從handleSave()方法返回任何東西,你必須看看它。

如果它仍然不起作用,請你發布你的春季日志。

另外,請確保您的請求應該如此

/PORTAL/save

如果在PORTAL/jsp/save之間有什么東西PORTAL/jsp/save@RequestMapping(value="/jsp/save")的提及@RequestMapping(value="/jsp/save")

@CookieValue(value="abc",required=true) String m

當我將要求從真改變為假時,它就成功了。

在方法中添加BindingResult參數。 如需參考,請參閱下面的代碼。

save(@ModelAttribute Employee employee,BindingResult bindingResult)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM