gpt4 book ai didi

java - Spring mvc @RequestBody 字符串格式

转载 作者:行者123 更新时间:2023-12-01 10:10:29 25 4
gpt4 key购买 nike

我有一个 Controller :personController.java

@Controller 
public class personController {

private static final Logger LOG = LoggerFactory.getLogger(OcaController.class);

@RequestMapping(value = "/person", method = {RequestMethod.POST, RequestMethod.GET})
public String ocaContract(@RequestBody String requestPerson) {
return requestPerson;
}

1 JSP:person.jsp

<html>
<head>
</head>
<body>
<form class="form-horizontal" METHOD="POST" ACTION="webmvc/person" ENCTYPE="x-www-form-urlencoded">
<div class="controls">
<input type="text" name="name" id="name" value="" placeholder="">
</div>
<div class="controls">
<input type="text" name="surname" id="surname" value="" placeholder="">
</div>
<input type="submit" value="ok"/>
</form>
</body>
</html>

和一个对象类:Person.java

@XmlRootElement(name="Person")
public class Person {

@XmlElement(required = true)
protected String name;
@XmlElement(required = true, nillable = true)
protected String surname;

public String getName() {
return name;
}

public void setName(String value) {
this.name = value;
} ...

当我填充 JSP 并单击输入按钮时,我的 Controller 返回此“requestPerson”字符串:

name=&surname=

是否可以将此字符串作为 POJO ?我的最终结果必须是 XML 格式:

<person>
<name>Lisala</name>
<surname>Lili</surname></person>

我希望你能帮助我,因为我从 1 天开始就在做这件事,但我没有找到一个简单的方法来完成这个任务。

最佳答案

您可以将@RequestBody替换为@ModelAttribute,将String替换为Person

public String ocaContract(@ModelAttribute Person requestPerson) {

关于java - Spring mvc @RequestBody 字符串格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36148903/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com