Java Spring MVC 发布请求

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20515171/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 02:28:21  来源:igfitidea点击:

Spring MVC Post Request

javaspringspring-mvcpost

提问by Akhil K Nambiar

I have something like

我有类似的东西

@RequestMapping("/property")
@ResponseBody
public String property(@RequestBody UserDto userDto ) {

    System.out.println(userDto.getUsername());
    System.out.println(userDto.getPassword());

    return "Hello";
}

in my controller.

在我的控制器中。

But it gives me an error when I post with

但是当我发布时它给了我一个错误

<form method="post" action="http://localhost:8080/home/property">

    <input name="username"/>
    <input name="password"/>
    <input type="submit"/>
</form>

in my html. Where am I going wrong.

在我的 html 中。我哪里错了。

采纳答案by Jeevan Patil

When you are posting a form, you should use @ModelAttributeannotation.

当您发布表单时,您应该使用@ModelAttribute注释。

Change your code to :

将您的代码更改为:

@RequestMapping("/property")
@ResponseBody
public String property(@ModelAttribute("userDto") UserDto userDto ) {
    System.out.println(userDto.getUsername());
    System.out.println(userDto.getPassword());
    return "Hello";
}

And your HTML / JSP can be :

你的 HTML/JSP 可以是:

<form method="post" name="userDto" action="http://localhost:8080/home/property">
    <input name="username"/>
    <input name="password"/>
    <input type="submit"/>
</form>

回答by Tim B

Request body is for when you are passing in something like a JSON or XML object (or raw data such as byte[]) to the HTTP POST. When you are POSTing form data then that is handled and parsed for you. The simplest way is to use the MVC form:form code with a command object, and then you will just receive a command object with all the entries from the form mapped to the object.

请求正文用于将诸如 JSON 或 XML 对象(或诸如 byte[] 之类的原始数据)之类的内容传递给 HTTP POST。当您发布表单数据时,它会为您处理和解析。最简单的方法是使用 MVC form:form 代码和一个命令对象,然后您将只收到一个命令对象,其中包含映射到该对象的表单中的所有条目。

回答by Abhishek Anand

One way is what Jeevan suggested, or you can modify your spring to accept it like ,

一种方法是 Jeevan 建议的,或者您可以修改您的弹簧以接受它,例如,

UserDto userDto;
@RequestMapping("/property")
@ResponseBody
public String property(@RequestParam("username") userDto.username,  @RequestParam("password") userDto.password) {

    System.out.println(userDto.getUsername());
    System.out.println(userDto.getPassword());

    return "Hello";
}

ofcourse if you have exposed attributes in class, which is not an elegant practice.

当然,如果你在课堂上暴露了属性,这不是一种优雅的做法。

回答by Mahendra

if you are getting http error 500? then try using

如果您收到 http 错误 500?然后尝试使用

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

If some other error please specify.

如果有其他错误请说明。

回答by Ingreatway

Request mapping default method is GET. have to specify url method with RequestMapping.

请求映射默认方法是GET。必须使用 RequestMapping 指定 url 方法。

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