

以下のエラーを解決したい。

[code]

    There was an unexpected error (type=Method Not Allowed, status=405).
    Request method 'GET' not supported
    org.springframework.web.HttpRequestMethodNotSupportedException: Request method 'GET' not supported
    
[/code]

ログイン画面でログインし検索画面でテキストボックスをクリックすると、ブラウザが固まって上のエラーが出ます。

環境はMac, Eclipse 2020, MySQL

よろしくお願いします。

[code]

    @Controller
    public class UserController {
    
      @Autowired
      UserService sevi;
    
      @Autowired
      UserRepository rep;
      //初期画面
      @RequestMapping(value = "/", method = RequestMethod.GET)
      String login(@ModelAttribute UserForm form) {
        //List<User> list = sevi.findAll();
        //UserForm form1 = new UserForm();
        //model.addAttribute("form", new UserForm());
        //model.addAttribute("list", list);
        return "login";
      }
      //ログイン
      @PostMapping("yeah")
      public String Login(
          @RequestParam String id,
          @RequestParam String pass,
          @Validated @ModelAttribute UserForm form,
          BindingResult result,
          Model model
          ) {
        if (result.hasErrors()) {
          return login(form);
        }
        User u = sevi.Login(id, pass);
        if (null == sevi.Login(u.getId(), u.getPass())) {
          return "login";
        } else {
          return "search";
        }
      }
      //検索
      @PostMapping("search")
      public ModelAndView search(@RequestParam String name,
          ModelAndView mav) {
        List<User> list = sevi.findBynameLike(name);
        mav.addObject("list", list);
        mav.setViewName("/search");
        return mav;
      }```
    
    
[/code]

[code]

    @Getter
    @Setter
    public class UserForm{
      @NotBlank
      private String id;
      @NotBlank
      private String pass;
    }
    
[/code]

[code]

    @Entity
    @Table(name="user")
    @Getter
    @Setter
    public class User{
      @Id
      @Column(name="id")
      @GeneratedValue(strategy=GenerationType.IDENTITY)
      private String id;
      @Column(name = "pass")
      private String pass;
      @Column(name = "name")
      private String name;
      @Column(name = "kana")
      private String kana;
    }
    
[/code]

[code]

    @Repository
    public interface UserRepository extends JpaRepository<User,String>{
    
      List<User> findBynameLike(String name);
    }
    
[/code]

[code]

    @Service
    @Transactional
    public class UserService {
        @Autowired
        UserRepository userRepository;
    
        public List<User> findBynameLike(String name){
        return userRepository.findBynameLike(name + "%");
        }
    
        public List<User> findAll() {
          return userRepository.findAll();
      }
    
        public void insert(User user) {
          userRepository.save(user);
        }
    
        public void update(User user) {
          userRepository.save(user);
        }
    
        public void delete(String id) {
          userRepository.deleteById(id);
        }
    
        public Optional<User> selectById(String id) {
            return userRepository.findById(id);
        }
    
        public User Login(String id,String pass) {
          User u = userRepository.getById(id);
          if (null != u) {
            if (u.getPass().equals(pass)) {
              return u;
            }
          } else {
          }
          return null;
        }
    
[/code]

[code]

    <!DOCTYPE html>
    <html xmlns:th="http://www.thymeleaf.org">
    <head>
    <meta charset="utf-8" />
    <title>Login</title>
    </head>
    <body>
    <h2>ログイン画面</h2>
    <form action="#" th:action="@{/yeah}" th:object="${userForm}" method="post">
    <table>
    <tr>
    <td>id:</td>
    <td><input type="text" name="id" th:field="*{id}" />
    <span th:if="${#fields.hasErrors('id')}" th:errors="*{id}"></span></td>
    </tr>
    <tr>
    <td>pass:</td>
    <td><input type="text" name="pass" th:field="*{pass}" />
    <span th:if="${#fields.hasErrors('pass')}" th:errors="*{pass}"></span></td>
    </tr>
    <tr>
    <td><button type="submit">Submit</button></td>
    </tr>
    </table>
    </form>
    </body>
    </html>
    
[/code]

[code]

    <! DOCTYPE html >
    <html xmlns:th="http://www.thymeleaf.org">
    <head>
    <title>検索画面</title>
    </head>
    <body>
        <p>
        <h2>社員情報検索</h2>
        <h3>※前方一致で検索します</h3>
        <form action="/search" method="post">
            <p>
                ID<input type="text" name="name"><br>
                <!--  名前<input type="text" name="name"><br>
                カナ<input type="text" name="name"><br>-->
                <button type="submit">検索</button>
                <button type="submit">新規登録</button>
        </form>
        <table>
            <tr>
                <th>ID</th>
                <th>名前</th>
                <th>カナ</th>
            </tr>
            <tr th:each="list : ${list}" th:object="${list}">
                <td th:text="*{id}"></td>
                <td th:text="*{name}"></td>
                <td th:text="*{kana}"></td>
            </tr>
        </table>
    </body>
    </html>
    
[/code]

[code]

    spring.datasource.url=jdbc:mysql://localhost:3306/
    spring.datasource.username=root
    spring.datasource.password=
    spring.jpa.database=MYSQL
    spring.jpa.hibernate.ddl-auto=update
    
[/code]


CC BY-SA 4.0
2021-10-28T13:11:04.810
83310
2023-07-10T13:03:05.337
2022-07-07T09:00:05.400
3060
48204
question
java mysql spring-boot
Request method 'GET' not supported のエラーを解決したい


探索画面のDOCTYPEが離れているからでは？  
その他の処理でおかしなところは見当たりませんね。


CC BY-SA 4.0
2022-07-07T08:54:18.493
89807
2022-07-07T08:54:34.710
2022-07-07T08:54:34.710
53419
53419
83310
answer