So here is my Controller. First of all, here I have both request methods GET and POST but for some strange reason the GetMapping("/journeys")
is not being recognized.
@Controller
@GetMapping("/journeys")
String journeys(Model model , @Valid JourneyForm form) {
model.addAttribute("journeys", journeys.findAll()) ;
model.addAttribute("form", form);
return "journeys" ;}
@GetMapping("/jounreyCreate")
String createJourney(Model model , JourneyForm form){
model.addAttribute("form", form) ;
return "journeyCreate" ;
}
@PostMapping("/journeyCreate")
String createJourney(Model model ,Errors errors,@Valid @ModelAttribute("form") JourneyForm form ) {
if (errors.hasErrors()) {
return journeys(model , form);
}
journeys.save( form.createJourney() ) ;
return "redirect:/journeys";
}
and here is the JourneyForm Class
public class JourneyForm {
private String name;
public JourneyForm(String name) {
this.name = name ;
}
public String getName() {
return name;
}
public Journey createJourney() {
return new Journey(getName());
}
}
and for the last part the journey.html
and journeyCreate.html
<body>
<h2 th:text="#{journeys.title}">Reise</h2>
<a href="/journeyCreate">
<button class="ui button">Journey erstellen</button>
</a>
<a href="/journeyEdit">
<button class="ui button">Reise bearbeiten</button>
</a>
<div th:each="journey : ${journeys}">
<p th:text="'Reise: '+${journey.name}">Reise</p>
</div>
and journeyCreate.html
<a href="/journeys">
<button class="ui button">Zurück zur Reise</button>
</a>
<form role="form" class="ui form" id="form" th:object="${form}" th:method="post" th:action="@{/journeyCreate}">
<div class="field">
<label for="journeyName">Name</label>
<input id="journeyName" name="journeyName" type="text" th:field="*{name}" th:errorclass="is-invalid" required="required"/><br/>
<div th:if="${#fields.hasErrors('name')}" class="invalid-feedback">Please provide a name.</div>
</div>
<input type="submit" value="Submit">
</form>
it is really frustrating and I can't get my hand on the cause
Your help will be appreciated, thank you.