简体   繁体   中英

Request method 'POST' not supported in Spring

This is my controller:

@Controller
public class UserController {

@Autowired
UserService userService;

@Autowired
SaveBatchService saveBatchService;

@Autowired
MassSMSFormValidator massSMSFormValidator;

@Autowired
SingleSMSFormValidator singleSMSFormValidator;

@RequestMapping("/userRole/sms/mass")
public String massSMSPage(Map<String, Object> map,
        @ModelAttribute("massSMSForm") MassSMSForm massSMSForm) {

    map.put("title", "Массовая рассылка");

    return "massSMS";
}    

@RequestMapping(value="/userRole/sms/mass" ,method = RequestMethod.POST)    
public String massSMSProcess(Map<String, Object> map,
        @ModelAttribute("massSMSForm") MassSMSForm massSMSForm,
        BindingResult result) throws IOException {
    InputStream inputStream = null;    
    MultipartFile file = massSMSForm.getFile();
    massSMSFormValidator.validate(massSMSForm, result);

    if (result.hasErrors()) {
        map.put("errorFlag", true);
        return massSMSPage(map, massSMSForm);
    }

    //and so on
}

And this is form:

<form:form method="post" enctype="multipart/form-data"
    modelAttribute="massSMSForm">

    <c:if test="${!empty errorFlag}">
        <div class="alert alert-danger">
            <b>Ошибка. </b>
            <form:errors path="file" />
        </div>
    </c:if>

    <table>
        <tr>
            <td><input type="file" name="file" accept="text/xml, text/plain" /></td>
        </tr>
        <tr>    
            <td><br /> <input type="submit" value="Загрузить" /></td>

        </tr>
    </table>
</form:form>

This page must upload file and check its name. As you can see there is a special method to handle POST requests. But my server says Request method 'POST' not supported after submitting. What is wrong? Do I have to use command attr., instead of modelAttribute or what?

UPD:

Generated HTML :

<form id="massSMSForm" action="/smsc/userRole/sms/mass" method="post" enctype="multipart/form-data">    

    <table>
        <tr>
            <td><input type="file" name="file" accept="text/xml, text/plain"/></td>
        </tr>
        <tr>

            <td><br /> <input type="submit" value="Загрузить" /></td>

        </tr>
    </table>
<input type="hidden" name="_csrf" value="0cd9d283-2ca7-4adc-af4a-ce72a09ceaae" />
</form>

Also, if I delete enctype="multipart/form-data" , this message doesn't appear. I was trying to add headers = "content-type=multipart/form-data" , but no effect.

You have repeated two times the same PATH without specify in the first one the request type.

Change the first one for

      @RequestMapping(value="/userRole/sms/mass" ,method = RequestMethod.GET) 

Looks like is getting the first path

And looking the path of your html I guess you have a controller like

 @Controller
 @RequestMapping("/smsc/")
 public class MyController{}

Also in your method change the modelAndAttribute for

   MultipartHttpServletRequest defaultMultipartHttpServletRequest

And remove from the form as well. you´re sending a file not a entity

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM