使用ManyToOne与thymeleaf实体关系发布数据

时间:2018-01-29 06:03:53

标签: thymeleaf

在下面的链接中提出了类似的问题: POSTing data with many to one relationship using Thymeleaf

  

错误:envionment_id不能是null

我有两个实体:ApacheServerEnvironment。我创建了一个HTML表单,以便在选择其中一个环境时创建apache server entity

HTML表单

 <form th:action="@{/apache}" method="post" th:object="${apache}">
    <input type="hidden" th:field="*{id}"/>
    <div class="row">
        <div class="col s12">
            <h2>New Apache Server</h2>
        </div>
    </div>
    <div class="divider"></div>
    <div class="row">
        <div class="col s12 l8" th:classappend="${#fields.hasErrors('serverName')}? 'error' : ''">
            <input type="text" th:field="*{serverName}" placeholder="Apache Server Name"/>
            <div class="error-message" th:if="${#fields.hasErrors('serverName')}" th:errors="*{serverName}"></div>
        </div>
    </div>

    <div class="row">
        <div class="col s12 l8">
            <select th:field="*{environment.id}" class="cs-select cs-skin-border">
                <option value="1" disabled="disabled">Select an Environment</option>
                <option th:each="env : ${environments}" th:value="${env.id}" th:text="${env.name}" style="color:#59b3b3"></option>
            </select>
        </div>
    </div>

控制器方法

@RequestMapping(value = "/apache", method = RequestMethod.POST)
    public String addApacheServer(@Valid @ModelAttribute("apache")ApacheServer apacheServer, BindingResult bindingResult, RedirectAttributes redirectAttributes){
        if(bindingResult.hasErrors()){
            // Include validation errors upon redirect
            redirectAttributes.addFlashAttribute("org.springframework.validation.BindingResult.apacheServer",bindingResult);

            // Redirect Back to the form
            redirectAttributes.addFlashAttribute("apache", apacheServer);
            return "redirect:/apache/add" ;
        }
        try {
            System.out.println(apacheServer.toString());
            apacheServerService.save(apacheServer);
            redirectAttributes.addFlashAttribute("flash", new FlashMessage("Apache Server Successfully added!", FlashMessage.Status.SUCCESS));
            return String.format("redirect:/apache/%s", apacheServer.getId());
        }catch (Exception e){
            e.printStackTrace();
            redirectAttributes.addFlashAttribute("flash", new FlashMessage(e.toString(), FlashMessage.Status.FAILURE));
            return "redirect:/apache";
        }

 @RequestMapping("/apache/add")
    public String addApacheServer(Model model){
        if(!model.containsAttribute("apache")) {
            model.addAttribute("apache", new ApacheServer());
            model.addAttribute("environments",environmentService.findAll());
        }
        return "environment/components/Apache/form";
    }

ApacheServer Model Class#

@Entity
public class ApacheServer {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@Column(unique = true)
@NotNull
@Size(min = 1, max = 10)
private String serverName;

@NotNull
@Size(min = 1, max = 30)
private String hostname;
private String ipAddress;
private String configPath;
private String logPath;
private String sslCertificatePath;
private String sslKeyFile;
private String sslWalletPath;
private String qosFile;
private String matchUrlPath;
private String restartScript;

public ApacheServer() {
}

@ManyToOne
@NotNull
private Environment environment;

public Long getId() {
    return id;

}

public void setId(Long id) {
    this.id = id;
}

public String getServerName() {
    return serverName;
}

public void setServerName(String serverName) {
    this.serverName = serverName;
}

public String getHostname() {
    return hostname;
}

public void setHostname(String hostname) {
    this.hostname = hostname;
}

public String getIpAddress() {
    return ipAddress;
}

public void setIpAddress(String ipAddress) {
    this.ipAddress = ipAddress;
}

public String getConfigPath() {
    return configPath;
}

public void setConfigPath(String configPath) {
    this.configPath = configPath;
}

public String getLogPath() {
    return logPath;
}

public void setLogPath(String logPath) {
    this.logPath = logPath;
}

public String getSslCertificatePath() {
    return sslCertificatePath;
}

public void setSslCertificatePath(String sslCertificatePath) {
    this.sslCertificatePath = sslCertificatePath;
}

public String getSslKeyFile() {
    return sslKeyFile;
}

public void setSslKeyFile(String sslKeyFile) {
    this.sslKeyFile = sslKeyFile;
}

public String getSslWalletPath() {
    return sslWalletPath;
}

public void setSslWalletPath(String sslWalletPath) {
    this.sslWalletPath = sslWalletPath;
}

public String getQosFile() {
    return qosFile;
}

public void setQosFile(String qosFile) {
    this.qosFile = qosFile;
}

public String getMatchUrlPath() {
    return matchUrlPath;
}

public void setMatchUrlPath(String matchUrlPath) {
    this.matchUrlPath = matchUrlPath;
}

public String getRestartScript() {
    return restartScript;
}

public void setRestartScript(String restartScript) {
    this.restartScript = restartScript;
}

public Environment getEnvironment() {
    return environment;
}

public void setEnvironment(Environment environment) {
    this.environment=environment;
}

1 个答案:

答案 0 :(得分:0)

我的选择&#39;由于百万富翁需要th:选择属性用于下拉列表,因此字段未填充。

Th:selected是一个布尔字段,它在某些条件下提供,但根据我的要求,我不需要任何条件,我只是以下面的方式定义了这个字段:

个:选择=&#34;真&#34;

它允许我将所选值转发给控制器。