HTTP状态400 - 必需字符串参数“xx”不存在 [英] HTTP Status 400 - Required String parameter 'xx' is not present

查看:988
本文介绍了HTTP状态400 - 必需字符串参数“xx”不存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to make a simple Spring MVC application.

这是我的 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>

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

I'm getting the following error when I try to run the application:

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

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

推荐答案

您的使用案例需要两个操作:

Your use case requires two actions:


  1. 查看和编辑< form>

  2. 提交< form>

  1. View and edit the <form>
  2. Submit the <form>

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

This should be mapped to two handler methods

@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设置为 / 。当您提交表单时,您也可以执行GET到 / ,但是您还需要其他东西来区分请求。在这里,我使用了 params =submit。因此,您需要将提交输入更改为

When you want to access the form, you make a GET to /. When you submit the form, you can also do a GET to /, but you would need something else to distinguish the request. Here, I've used params="submit". So you need to change your submit input to

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

或者只是放置你已经使用过的参数( params = { xx} )。

or just put the parameter you're already using (params={"xx"}).

这篇关于HTTP状态400 - 必需字符串参数“xx”不存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆