繁体   English   中英

Spring MVC + Thymeleaf 后单 Object 至 Z9BBF373797BF7CF7BA252C8002368

[英]Spring MVC + Thymeleaf Post Single Object to Controller

我有点震惊,我找不到如何做到这一点的例子。 每次我用谷歌搜索它时,我都会获得有关如何发布一组对象或其他不相关内容的信息。 thymeleaf 文档(我能找到的)似乎也没有太多解释,就像有很多假设的知识一样。

回到我的问题,我只想从表单中发布一个 object (bean)。 我希望我的 controller 映射方法绑定到这个“pojo”bean而不是一堆字符串/整数。

我发现唯一接近的是 StackOverflow 上的东西,其中一半代码在问题中,另一半在答案中,并且总是有人说它对他们不起作用。

任何人都可以用一个简单的老无聊的例子来缓解一下吗?

可以找到下面的代码片段可能对您有帮助。

Controller GET/POST 映射:

@RequestMapping(value = "/registration", method = RequestMethod.GET)
public String registartionPage(Model model) {

Registration registration = new Registration();

model.addAttribute("registration", registration);

return "registarion/registarion";
}

@RequestMapping(value = "/user/new-user-registrn", method = RequestMethod.POST)
public String newUserRegistrn(Model model, @ModelAttribute("registration") 
Registration registration, RedirectAttributes redirectAttributes) {

try {

    StarUser user = starSecurityService.findSysUserName(registration.getUserName());
    if (user != null) {
        throw new Exception("User Already Exist. Please try with different User Name");
    }

    user = (StarUser) starUtilService.save(setStarUser(registration));
    model.addAttribute("registration", registration);
    if (user != null) {

        redirectAttributes.addAttribute("starMessage",
            "Your Account is successfully created !! Login to Access the Application");

        return "redirect:/";
    }

} catch (Exception e) {

    model.addAttribute(STAR_MESSAGE, e.getMessage());
}

return "registarion/registarion";
}

Thymeleaf 内容:

<form class="form-horizontal col-sm-12" method="POST" th:action="@{/user/new-user-registrn}" th:object="${registration}">

<div class="row">

    <div class="form-group col-md-12">
        <div class="star-reg-header">New User Registration</div>
    </div>

    <div class="star-reg-body">

        <div class="form-group col-sm-4">
            <label class="required">First Name: </label>
            <input type="text" class="form-control required" th:field="*{firstName}" required="required" />
        </div>

        <div class="form-group col-sm-4">
            <label class="required">Last Name: </label>
            <input type="text" class="form-control" th:field="*{lastName}" required="required" />
        </div>

        <div class="form-group col-sm-4">
            <label class="required">User Name: </label>
            <input type="text" class="form-control" th:field="*{userName}" required="required" />
        </div>

        <div class="form-group col-sm-4">
            <label class="required">Password: </label>
            <input type="password" class="form-control" th:field="*{password}" required="required" />
        </div>

        <div class="form-group col-sm-4">
            <label class="required">Email: </label>
            <input type="text" class="form-control" th:field="*{email}" required="required" />
        </div>

    </div>

</div>
<div class="form-group col-md-12">
    <label class="col-sm-2"></label>
    <div class="col-sm-10">
        <button type="submit" class="btn btn-info">Submit</button>
    </div>
</div>

Java 豆子 class

public class Registration {

 protected String firstName;

 protected String lastName;

 protected String userName;

 protected String password;

 protected String email;

 //Setter and Getter

}       

在参数中使用@ModelAttribute注解。

像这样的东西。

@RequestMapping(value = "/someurl", method = RequestMethod.POST)
public String savePojo(@ModelAttribute PojoClass pojo, Model model) {
    //Code
}

编辑:这个答案有很好的信息。 Spring MVC 中的@ModelAttribute 是什么?

暂无
暂无

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

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