0

所以这是我的控制器。首先,这里我有 GET 和 POST 两种请求方法,但由于某些奇怪的原因,GetMapping("/journeys")无法识别。

   @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";
   }



这是 JourneyForm 类

 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());
 }
 } 

最后一部分是journey.html和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> 

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>

这真的很令人沮丧,我无法理解原因

您的帮助将不胜感激,谢谢。

4

0 回答 0