运行Spring Boot应用程序时出错

时间:2018-07-19 14:58:53

标签: spring spring-boot spring-data-jpa

我创建了一个非常简单的Spring Boot应用程序(Dog Rescue应用程序),该应用程序存储了狗的信息,并且在执行该应用程序时出现错误

  

错误信息

     

发生意外错误(类型=不允许使用方法,状态= 405)。   请求方法'GET'不支持

我的index.html文件是

<h2>Add A Dog</h2>
<form action="#" th:action="@{/home}">
    <label>Name<input type="text" name="name" id="name"></input></label>
    <label>Vaccinated<input type="text" name="vaccinated" id="vaccinated"></input></label>
    <label>Rescued<input type="text" name="rescued" id="rescued"></input></label>
    <input type="submit" value="Submit"></input>
</form>
</body>
</html>

我的控制器文件是

package com.dog.resue.controller;

import com.dog.resue.dao.DodRepository;
import com.dog.resue.service.DogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.Date;

@Controller
public class DogController {

    @Autowired
    private DodRepository dodRepository;

    @Autowired
    private DogService dogService;

    @PostMapping(value ="/home")
    public String adddog(@RequestParam("name") String name,
                          @RequestParam("rescued") @DateTimeFormat(pattern = "yyyy-MM-dd") Date rescued,
                          @RequestParam("vaccinated") Boolean vaccinated, Model model)
    {
        dogService.addADog(name, rescued, vaccinated);
        System.out.println("name = " + name + ",rescued = " + rescued + ", vaccinated = " + vaccinated);
        return "redirect:/home";
    }

    }

完整的项目可用here

1 个答案:

答案 0 :(得分:0)

<form .. method="post">应该解决它。