Spring mvc @RequestBody字符串格式

时间:2016-03-22 07:57:17

标签: java xml spring jsp spring-mvc

我有一个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并单击输入按钮时,我的控制器返回此“requestPerson”字符串:

name=&surname=

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

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

我希望你能帮助我,因为我现在已经有1天了,我找不到一个简单的方法来实现这个目标。

1 个答案:

答案 0 :(得分:1)

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

public String ocaContract(@ModelAttribute Person requestPerson) {