0

create.html

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="stylesheet" type="text/css" href="/css/main.css" />
</head>
<body>
<a th:href="@{/admin/}">Admin</a>
<br>
<br>


<br>

<h1>Add core phrases to the database</h1>
<br>

<br>
<form method="POST" th:object="${corePhrasesCreateForm}">

    <table>
        <tr>
            <td>Quotation cluster (" "):</td>
            <td><input
                    type="number"
                    id="quotation-cluster"
                    name="quotation-cluster"
                    th:value="${quotationCluster}"/></td>
            <td th:if="${#fields.hasErrors('quotationCluster')}" th:errors="*{quotationCluster}">quotationCluster</td>
        </tr>
        <tr>
            <td>Quotation Exclamation cluster ("!")</td>
            <td><input

                    type="number"
                    id="quotation-exclamation-cluster"
                    name="quotation-exclamation-cluster"
                    th:value="${quotationExclamationCluster}"/></td>
            <td th:if="${#fields.hasErrors('quotationExclamationCluster')}" th:errors="*{quotationExclamationCluster}">quotationExclamationCluster</td>
        </tr>
        <tr>
            <td>Phrases</td>
            <td><textarea rows="5" cols="60" name="value" placeholder="Key phrases" th:text="${value}"></textarea></td>
            <td class="error" th:if="${#fields.hasErrors('value')}" th:errors="*{value}">value</td>
        </tr>
    </table>    


    <br/>
    <input type="submit" value="submit"/>
</form>
</body>
</html>

Validator

import com.example.marketing3.semantics.semanticsClusters.repositories.ClusterRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.validation.Errors;
import org.springframework.validation.Validator;

@RequiredArgsConstructor
@Service
public class CorePhraseFormValidator implements Validator {
    private final ClusterRepository clusterRepository;

    @Override
    public boolean supports(Class<?> clazz) {
        return false;
    }

    @Override
    public void validate(Object target, Errors errors) {
        System.out.println();

}

Controller

@Controller
@RequestMapping("/semantics/phrases/core")
@RequiredArgsConstructor
public class CorePhraseController {

    @GetMapping({"/create", "/create/"})
    public String createGet(CorePhrasesCreateForm corePhrasesCreateForm) {

        return "semantics/phrases/core/create";
    }

    @PostMapping({"/create", "/create/"})
    public String createPost(@Valid CorePhrasesCreateForm corePhrasesCreateForm,
                             BindingResult bindingResult,
                             RedirectAttributes atts) {

        corePhraseFormValidator.validate(corePhrasesCreateForm, bindingResult);

        if (bindingResult.hasErrors()) {
            return "semantics/phrases/core/create";
        }

        atts.addAttribute("message", "Core phrases created");

        String result = "";

        return "redirect:/general/message";
    }

}

The form has been rendered. I entered

Quotation cluster (" "): 1 Quotation Exclamation cluster ("!"): 2

And some text for phrases.

Like this:

enter image description here

The problem:

To createPost quotationCluster and quotationExclamationCluster came as zeroes.

enter image description here

But I entered 1 and 2 respectively.

What have I done wrongly, and how can I correct the situation?

0

1 Answer 1

1

Change

<input type="number" id="quotation-cluster"
       name="quotation-cluster" th:value="${quotationCluster}"/>

To:

<input type="number" id="quotation-cluster" th:field="*{questionCluster}"
       name="quotationCluster" th:value="${quotationCluster}"/>

the name and th:field should be same as the declared attribute in your java class CorePhraseForm. Repeat this for the the next input (Textarea).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.