Spring MVC Request method 'POST' not supported

0

Witam. Dopiero zaczynam naukę ze Springiem MVC i mam problem. A więc tak, mam na początek taki prościutki formularz.

<form action="/show"  method="post">
    <input name="username" />
    <input value="submit" type="submit" />
</form>

oraz prościutki kontroler

@PostMapping("/show")
    public String show(@RequestParam String username){
        System.out.println(username);
        return "index";
    }

Po wypełnieniu formularza pojawia się error

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

Jednak w konsoli nazwa podana przez użytkownika normalnie wyświetla się czyli metoda normalnie się wywołała. Problem jest chyba ze zwróceniem z powrotem strony index.html. W pliku application.properties mam podany prefix i suffix. Plik index.html umieściłem w folderze static w którym dodałem folder webapp i tam znajduje się owy plik. A prefix i suffix prezentują się tak

spring.mvc.view.prefix=/webapp/
spring.mvc.view.suffix=.html

Dodam, że na metodzie GET która również zwraca tą stronę wszystko normalnie działa.

1
  1. Skąd pomysł żeby to było w static? o_O To na pewno nie jest dobrze. Taka klasyczna lokalizacja to src/main/webapp/WEB-INF/pages
  2. Zamiast printów dać breakpoint i zobacz czy faktycznie tam wchodzisz a nie że masz printy w innym miejscu w kodzie ;)
0

1.Tak, masz racje już poprawiłem lokalizacje jednak error dalej się pojawia .
2.Tak sprawdzałem metoda jest normalnie wywoływana

1

Wklej poma

0
krancki napisał(a):

Wklej poma

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.1.6.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>


<properties>
    <java.version>11</java.version>
</properties>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-configuration-processor</artifactId>
        <optional>true</optional>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-tomcat</artifactId>
        <scope>provided</scope>
    </dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>
1

Spróbuj tego

<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-webmvc</artifactId>
    <version>5.1.8.RELEASE</version>
</dependency>

i wstaw gita do projektu

0

Dobra zastosowałem redirect do drugiego GET-a a ten GET zwraca stronę i działa. Chociaż to nie zmienia tego, że nie mogę normalnie zwrócić strony z POST-a nie stosując przekierowań

0

Dej ten projekt na githuba i popaczymy

1 użytkowników online, w tym zalogowanych: 0, gości: 1