编辑时:HTTP 状态 405 - 不支持请求方法 'POST'

While editing: HTTP Status 405 - Request method 'POST' not supported

我不知道是什么问题...我想在模态中编辑一些信息window...并收到此消息..输入是通过类似的模态进行的,唯一的区别是按钮名称和标题。 所以,我的控制器的代码是:

@Controller
public class GroupController {

    @Autowired
    private GroupService groupService;

    @Autowired
    private ConversationService conversationService;

    @ModelAttribute("group")
    public Group constructGroup() {
        return new Group();
    }

    @RequestMapping(value = "/groups", method = RequestMethod.POST)
    public String doAddGroup(Model model,
            @ModelAttribute("group") @Valid final Group group,
            final BindingResult result, RedirectAttributes redirectAttributes,
            HttpSession httpSession) {
        if (result.hasErrors()) {
            redirectAttributes.addFlashAttribute(
                    "org.springframework.validation.BindingResult.group",
                    result);
            redirectAttributes.addFlashAttribute("group", group);
            redirectAttributes.addFlashAttribute("groupNotCreated", true);
            return "redirect:/groups";
        }
        groupService.create(group);
        redirectAttributes.addAttribute("id", group.getId()).addFlashAttribute(
                "groupCreated", true);
        return "redirect:/group?id={id}";
    }

    @RequestMapping("/group/remove/{id}")
    public String removeGroup(@PathVariable Long id) {
        Group group = groupService.findOne(id);
        groupService.delete(group);
        return "redirect:/groups";
    }

    @RequestMapping(value = "/groups", method = RequestMethod.GET)
    public String groupCreationShow(Model model) {
        Set<Group> groups = groupService.findAll();
        model.addAttribute("groups", groups);
        return "groups";
    }

    @RequestMapping(value = "/group", method = RequestMethod.GET)
    public String showGroups(@RequestParam("id") long id, Model model) {
        Group group = groupService.findOne(id);
        model.addAttribute("group", group);
        addLatestConversationsByGroupId(model, id);
        return "group";
    }

    @RequestMapping(value = "/group-editing", method = RequestMethod.GET)
    public String groupEditingShow(@RequestParam(value = "id") Long id,
            Model model) {
        Group group = groupService.findOne(id);
        model.addAttribute("group", group);
        return "group-editing";
    }

    @RequestMapping(value = "/group-editing", method = RequestMethod.POST)
    public String editGroup(@ModelAttribute("group") Group group,
            @RequestParam("id") Long id) {
        group.setId(id);
        groupService.update(group);
        return "redirect:/group?id={id}";
    }

    private void addLatestConversationsByGroupId(Model model, Long id) {
        List<ConversationDto> conversations = conversationService
                .findLatestConversationsDtoByGroupId(id);
        Long conversationsSize = conversationService.countByGroupId(id);
        model.addAttribute("conversationDtos", conversations);
        model.addAttribute("conversationsSize", conversationsSize);
    }
}

服务实现:

@Service
public class GroupServiceImpl implements GroupService {

    @Autowired
    private GroupRepository groupRepository;

    @Override
    @Transactional
    public Set<Group> findAll() {
        List<Group> list = (List<Group>) groupRepository.findAll();
        Set<Group> items = new HashSet<Group>(list);
        return items;
    }

    @Override
    @Transactional
    public Group findOne(Long id) {
        return groupRepository.findOne(id);
    }

    @Override
    public void delete(Group group) {
        groupRepository.delete(group);
    }

    @Override
    public void create(Group group) {
        group.setCreatedAt(new Date());
        groupRepository.save(group);
    }

    @Override
    public void update(Group group) {
        groupRepository.saveAndFlush(group);
    }
}

我尝试编辑的模态形式是:

<form:form modelAttribute="group" cssClass="form-horizontal groupForm">
        <!-- Modal -->
        <div class="modal fade" id="groupEditModal" tabindex="-1"
            role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
            <div class="modal-dialog">
                <div class="modal-content">
                    <div class="modal-header">
                        <button type="button" class="close" data-dismiss="modal"
                            aria-label="Close">
                            <span aria-hidden="true">&times;</span>
                        </button>
                        <h4 class="modal-title" id="myModalLabel">Group editing...</h4>
                    </div>
                    <div class="modal-body">

                        <div class="form-group">
                            <label for="groupName" class="col-sm-2 control-label">
                                Name: </label>
                            <div class="col-sm-10">
                                <form:input path="groupName" cssClass="form-control" />
                                <form:errors path="groupName" />
                            </div>
                        </div>

                        <div class="form-group">
                            <label for="groupDescription" class="col-sm-2 control-label">
                                Description: </label>
                            <div class="col-sm-10">
                                <form:input path="groupDescription" cssClass="form-control" />
                                <form:errors path="groupDescription" />
                            </div>
                        </div>

                    </div>
                    <div class="modal-footer">
                        <input type="submit" class="btn btn-success"
                            value="Finish editing!" />
                        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                    </div>
                </div>
            </div>
        </div>
    </form:form>

我已经阅读了很多关于类似问题的解决方案,但没有任何帮助。

哦,而且 Eclipse 控制台没有错误。

如果您需要更多信息以更好地理解它 - 请问我! 提前致谢!

我发现了一个问题...

而不是控制器中的 2 个方法:

    @RequestMapping(value = "/group-editing", method = RequestMethod.GET)
    public String groupEditingShow(@RequestParam(value = "id") Long id,
            Model model) {
        Group group = groupService.findOne(id);
        model.addAttribute("group", group);
        return "group-editing";
    }

    @RequestMapping(value = "/group-editing", method = RequestMethod.POST)
    public String editGroup(@ModelAttribute("group") Group group,
            @RequestParam("id") Long id) {
        group.setId(id);
        groupService.update(group);
        return "redirect:/group?id={id}";
    }

我只写了1个方法:

    @RequestMapping(value = "/group", method = RequestMethod.POST)
    public String editGroup(@ModelAttribute("group") Group group) {
        groupService.update(group);
        return "group";
    }

问题是第一次很容易理解(对我来说!)。编辑按钮在我要编辑的组的页面上!所以,那样的话,我不需要其他方法来获取这个对象,我已经有了!由于组页面上的模式window,我不需要任何@RequestParam ...我只用一个对象在同一页面上进行更改! Modal windows 对于这样的任务来说真的很酷!我希望它可以帮助某人。