java - HTTP 状态 405 - 当我尝试更新数据库中的房间表时,不支持请求方法“POST”。当我选择要上传的图像时

标签 java spring model-view-controller

        <%@include file="../header.jsp" %>
        <h1>Edit Room</h1>
        <form action="save"  method="post" enctype="multipart/form-data">
            <div class="form-group">
                <label>Room Type</label>
                <input type="text" name="roomType" value="${Room.room_type}" required="required" class="form-control"/>
            </div>
            <div class="form-group">
                <label>Room Description</label>
                <input type="text" name="roomDescription" value="${Room.room_description}" required="required" class="form-control"/>
            </div>

            <div class="form-group">
                <label>Room Number</label>
                <input type="number" name="roomNumber" value="${Room.room_number}" required="required" class="form-control"/>
            </div>
            <div class="form-group">
                <label>Room Image</label>
                <input type="file" name="file"   required="required" class="form-control"/>
            </div>
            <form:hidden path="ro_id" />
            <div class="form-group"> 
            <button type="submit" class="btn btn-success" >Save</button>
            </div>
        </form>
        <%@include  file="../footer.jsp" %>

我在执行 CRUD 时遇到错误。在名为“dispatcher”的 DispatcherServlet 中未找到带有 URI [/Hotels] 的 HTTP 请求的映射 警告:不支持请求方法“POST”

This is the Room controller

@ Controller @RequestMapping(value = "/admin/room")

    public class Roomcontroller {

        @Autowired
        private RoomService roomService;

            @RequestMapping(method = RequestMethod.GET)
            public String index(ModelMap map) throws SQLException {
            map.addAttribute("Room", roomService.getAll());
            return "admin/room/index";
            }
            @RequestMapping(value = "/addroom", method = RequestMethod.GET)
        public String addRoom() throws SQLException {
            return "admin/room/addroom";
        }
            @RequestMapping( value = "/editroom/{ro_id}", method = RequestMethod.GET)
            public @ResponseBody ModelAndView edit(@PathVariable("ro_id") int ro_id) throws SQLException {
            ModelAndView mv = new ModelAndView("admin/room/editroom");
            mv.addObject("Room", roomService.getById(ro_id));
            return mv;
            }
            @RequestMapping(value = "/deleteroom/{ro_id}", method = RequestMethod.GET)
            public String delete(@PathVariable("ro_id") int ro_id) throws SQLException {
            roomService.delete(ro_id);
            return "redirect:/admin/room";
            }
            @RequestMapping(value = "/save", method = RequestMethod.POST)
            public String save(@RequestParam("roomType") String roomType,
        @RequestParam("roomDescription") String roomDescription, @RequestParam("roomNumber") int roomNumber
            ,@RequestParam("file") MultipartFile multipartFile,HttpServletRequest req) throws SQLException, IOException {

             Room room = new Room();
             room.setRoom_type(roomType);
         room.setRoom_description(roomDescription);
         room.setRoom_number(roomNumber);

            // TO DO : Save room, fetch the id of saved room and set it through
            // setter in above object.

             if(room.getRo_id()==0){

         File roomImageDirectory = new File("D:\\Hotels\\uploadedImages");
             if (!roomImageDirectory.exists()) {
         roomImageDirectory.mkdirs();
        }
          String[] fileNameToken = multipartFile.getOriginalFilename().split("\\.");
                    // You can change file name to be saved.
          String newFileName = "room-" + room.getRoom_number() + "." + fileNameToken[fileNameToken.length - 1];
                    File roomImage = new File(roomImageDirectory, "/" + newFileName);
            roomImage.createNewFile();
            multipartFile.transferTo(roomImage);
                    room.setImage(newFileName);

                    roomService.insert(room);  
                      }
                else
                      {
                    roomService.update(room);
                }
            return "redirect:/admin/room";  
            }
            }

最佳答案

您输入的JSP的form action不正确,您需要更改如下:

<form action="/admin/room/save"  method="post" enctype="multipart/form-data">

关于java - HTTP 状态 405 - 当我尝试更新数据库中的房间表时,不支持请求方法“POST”。当我选择要上传的图像时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40583369/

相关文章:

java - 将 Twitter 搜索结果中的 JSON 字符串获取到 Java 中进行解析

java - JButton + AbstractAction 重置一堆 JCheckBox - Swing

java - 基于Java开发Android游戏的SDK

java - 在 Tomcat 中使用 Websockets

java - 通过计划任务进行批量操作的正确方法?

jquery - 如何在 ASP 形式的 MVC Core 上使用日期选择器

swift - 我试图从 VC 中的另一个 View 禁用 tableview 上的 userInteraction,但它不会让我调用该函数

java - 如何使用maven和JAVA在robotframework中重新运行测试失败的套件

java - 使用实体监听器测量数据库操作所花费的时间

Java - 使用类作为伪数据库