Request method 'POST' not supported in Spring MVC
Please help me solve the problem
Jsp
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<c:if test="${currentRoom.getLight()}">
<form method="post" action="/room/allroom/">
<input type="radio" name="status" value="turnOff">
<button type="submit">Выключить</button>
</form>
</c:if>
<c:if test="${!currentRoom.getLight()}">
<form method="post" action="/room/allroom/">
<input type="radio" name="status" value="turnOn">
<button type="submit">Включить</button>
</form>
</c:if>
</body>
Controller
@Controller
@RequestMapping(path = "/room")
public class RoomController {
@GetMapping(path = "allroom/{roomNumber}")
public ModelAndView getRoomById(@PathVariable("roomNumber") Integer roomNumber, ModelAndView modelAndView, HttpServletRequest httpServletRequest){
modelAndView.setViewName("currentRoom");
List<Room> roomList = roomService.allListRooms();
Room room = roomList.get(roomNumber);
httpServletRequest.getSession().setAttribute("currentRoom", room);
return modelAndView;
}
@PostMapping(path = "allroom/{roomNumber}")
public ModelAndView light(@PathVariable("roomNumber") Integer roomNumber, @RequestParam("status") String status, ModelAndView modelAndView, HttpServletRequest httpServletRequest){
modelAndView.setViewName("currentRoom");
if(status.equals("turnOn")){
roomService.lightOn(roomNumber);
return modelAndView;
}
if(status.equals("turnOff")){
roomService.lightOff(roomNumber);
return modelAndView;
}
return modelAndView;
}
Mistake: Message Request method 'POST' not supported
Description The method received in the request-line is known by the origin server but not supported by the target resource.
0
1 answers
You will submit by following the link action="/room/allroom/", although you have added two "/room/allroom/{roomNumber} "
Add a number to the link. So that the request is sent to"/room/allroom / 1 " say. Or remove {roomNumber} from the controller
2
Author: Serhii Dikobrazko, 2019-04-13 21:29:03