繁体   English   中英

HTTP状态400-所需的字符串参数'xx'不存在

[英]HTTP Status 400 - Required String parameter 'xx' is not present

我正在尝试制作一个简单的Spring MVC应用程序。

这是我的HelloController

package com.springapp.mvc;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;


@Controller
@RequestMapping("/")
public class HelloController {
@RequestMapping(method = RequestMethod.GET)
public String printWelcome(ModelMap model,@RequestParam(value = "xx",required
=true)String xx) {

        model.addAttribute("message", "Hello Mahdi");

    return "hello";
}
}

JSP文件:

<html>
<body>
<h1>${message}</h1>
<form action="/" method="GET">
    <input type="text" name="xx">
    <button type="submit">submit</button>
</form>
</body>
</html>

尝试运行应用程序时出现以下错误:

HTTP Status 400 - Required String parameter 'xx' is not present

我是Spring MVC的新手,请帮助。

您的用例需要执行以下两项操作:

  1. 查看和编辑<form>
  2. 提交<form>

这应该映射到两个处理程序方法

@RequestMapping(method = RequestMethod.GET)
public String getForm() {

    model.addAttribute("message", "Hello Mahdi");
    return "hello"; // assume hello.jsp
}

@RequestMapping(params={"submit"}, method = RequestMethod.GET)
public String printWelcome(ModelMap model, @RequestParam(value = "xx",required=true) String xx) {

    /*
     Do something with submitted parameter
     and return some view name
    */
}

当您想要访问表单时,您可以对/进行获取。 提交表单时,您也可以对/进行GET操作,但是您还需要其他方法来区分请求。 在这里,我使用了params="submit" 因此,您需要将提交输入更改为

<input type="submit" name="submit">submit</button>

或仅放置您已经在使用的参数( params={"xx"} )。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM