如何保持选中的单选按钮值,以及数据库的下拉列表?

时间:2017-08-28 22:26:27

标签: database spring-boot thymeleaf

这是我的表格:

    <form  method="POST" th:action="@{/registration}" class="form-horizontal" th:object="${user}" role="form">
    <input type="text" th:field="*{name}" class="form-control  input-lg" placeholder="" />
    <input type="text" th:field="*{lastName}" class="form-control  input-lg" placeholder="" />
    <label class="control-label col-sm-3">Gender</label>
                <label class="radio-inline"> <input type="radio"                                        id="femaleRadio" value="F" />Female
                                </label>
                <label class="radio-inline"> <input type="radio"                                id="maleRadio" value="M" />Male
                                </label>

    <input type="date" id="birthDate" class="form-control  input-lg" />
    <input type="text" name="email" value=""class="form-control  input-lg" placeholder="" />
    <input type="password" th:field="*{password}" value="" class="form-control  input-lg" placeholder="" />
    <select class="form-control  input-lg">
                <option value="" selected disabled>Please select an option </option>
                <option value="one"> 1</option>
                <option value="two"> 2</option>
                <option value="three">3</option>

<button type="submit" class="btn btn-primary btn-lg" name="submit">
            </form>

这是我的控制器将其保存到数据库中

@RequestMapping(value = "/registration", method = RequestMethod.POST)
public ModelAndView createNewUser(@Valid User user, BindingResult bindingResult) {
    ModelAndView modelAndView = new ModelAndView();
    User userExists = userService.findUserByEmail(user.getEmail());
    userService.saveUser(user);
    modelAndView.addObject("successMessage", "User has been registered successfully");
    modelAndView.addObject("user", new User());
    modelAndView.setViewName("registration");
    }
    return modelAndView;
}

我使用userRepository.save(user)

问题是,除了输入类型radio,date和select选项之外,我的字段将保存在数据库中。 我需要更改什么才能将所有字段保存到数据库中? 这是我的用户模型:

@Table(name = "user")
public class User {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "user_id")
private Long id;
@Column(name = "email")
private String email;
@Column(name = "password")
private String password;
@Column(name = "name")
private String name;
@Column(name = "last_name")
private String lastName;
@Column(name="gender")
private char gender;
@Column(name="birthday")
private Date birthDate;
@Column(name="selectedOption")
private String selectedOption;

模型中的日期是从java.util.Date导入的; 任何帮助将不胜感激。

0 个答案:

没有答案