【问题标题】:Spring MVC + Thymeleaf Post Single Object to ControllerSpring MVC + Thymeleaf 将单个对象发布到控制器
【发布时间】:2019-10-22 01:45:31
【问题描述】:

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

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

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

任何人都可以在这里举一个简单而无聊的例子来缓解一下吗?

【问题讨论】:

    标签: java spring-mvc thymeleaf


    【解决方案1】:

    可以找到下面的代码 sn-p 可能对你有帮助。

    控制器 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 Bean 类

    public class Registration {
    
     protected String firstName;
    
     protected String lastName;
    
     protected String userName;
    
     protected String password;
    
     protected String email;
    
     //Setter and Getter
    
    }       
    

    【讨论】:

      【解决方案2】:

      在参数中使用@ModelAttribute注解。

      类似的东西。

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

      编辑:这个答案有很好的信息。 What is @ModelAttribute in Spring MVC?

      【讨论】:

        猜你喜欢
        • 2019-05-20
        • 1970-01-01
        • 1970-01-01
        • 2018-06-15
        • 2013-06-13
        • 2018-09-23
        • 2011-08-19
        • 1970-01-01
        • 2015-09-04
        相关资源
        最近更新 更多