java - 无法命中所需 Controller 的 POST 方法

标签 java spring jsp spring-mvc

当用户输入新评论时,我想保存它,并且我的 Controller 中有一个应该处理该问题的 POST 方法,但我在定位该方法时遇到问题。当我调试代码时,它甚至无法访问 Controller 中的 handleNewComment 方法,但我会重定向到index.jsp页面,尽管在地址栏中有一个有效的地址(http://localhost:8080/ycexams-web/showIssue).

showIssue.jsp

<%@ include file="/common/taglibs.jsp"%>

<form:form commandName="issue" id="issueForm">
    <form:hidden path="id"/>    
    <form:label path="headline" id="issueHeadline">${issue.headline}-</form:label>
    <form:label path="headline" id="issuePercentage">${percentage}%</form:label>
    <form:input cssClass="form-control" path="text" id="issueText" />
    <form:errors path="text" cssClass="error"/>
    <input type="hidden" value="${issue.id}" name="issueId"/>       
    <br/>
</form:form>    

<button id="addCommentButton" onclick="showAddCommentField()"><fmt:message key="issue.addComment"/></button>
<br/>

<div id="addCommentField" style="display:none">
    <form:form commandName="comment" method="post" action="showIssue" id="commentForm"> 

        <fmt:message key="comment.nickname"/><br/>
        <form:input path="nickname" id="commentNickname" /><br/><br/>

        <fmt:message key="comment.text"/><br/>
        <form:input cssClass="form-control" path="text" id="commentText" />

        <form:errors path="text" cssClass="error"/>
        <br/>

        <button type="submit" class="btn btn-primary" name="save">
            <i class="icon-ok icon-white"></i> <fmt:message key="comment.button.save"/>
        </button>
    </form:form>
</div>

<c:if test="${commentCount != 0 && commentCount!= null}">           
    <fmt:message key="issue.comments"/> &nbsp;(${commentCount})
    <c:forEach var="comment" items="${allComments}" varStatus="status">
         <div style="padding: 10px;">
            ${comment.nickname}<br>
            ${comment.text}
         </div>     
    </c:forEach>
</c:if>

<script>
    function showAddCommentField() {
        document.getElementById("addCommentField").style.display='block';
        document.getElementById("addCommentButton").style.display='none';
    }
</script>

ShowIssueController.java

@Controller
public class ShowIssueController {

    @Autowired
    private IssueManager issueManager;

    @Autowired
    private CommentManager commentManager;

    @InitBinder
    public void initBinder(WebDataBinder binder) {
        binder.registerCustomEditor(String.class, new StringTrimmerEditor(true));
    }

    @ModelAttribute("issue")
    private Issue getIssue(final HttpServletRequest request, ModelMap model) {

        Issue issue = new Issue();
        Comment comment = new Comment();
        model.addAttribute("comment", comment);

        Object issueIdObj = request.getParameter("issueId");
        if (issueIdObj != null) {
            try {
                Long issueId = Long.parseLong((String)issueIdObj);
                issue = issueManager.get(issueId);

                List<Comment> allComments = issue.getComments();
                int commentCount = allComments.size();
                int normal = issue.getNormal();
                int notNormal = issue.getNotNormal();
                int percentage = normal * 100 / (normal + notNormal);
                model.addAttribute("allComments", allComments);
                model.addAttribute("commentCount", commentCount);
                model.addAttribute("percentage", percentage);
            }
            catch(Exception e) {
                model.addAttribute("exception", "Wrong issueId");
            }
        }

        return issue;
    }

    @RequestMapping(value = "/showIssue", method = RequestMethod.GET)
    public String handleHome(ModelMap model) {  
        return "showIssue";
    }

    @RequestMapping(value = "/showIssue", method = RequestMethod.POST)
    public String handleNewComment(@ModelAttribute("comment") final Comment comment, @RequestParam("issueId") final Long issueId, ModelMap model) {
        try{
            commentManager.save(comment);
        } catch(Exception e) {
            model.addAttribute("exception", "Saving comment failed, please try again.");
        }
        return "showIssue";
    }
}

最佳答案

尝试从方法签名中删除 @RequestParam("issueId") Final Long IssueId ,我没有看到您在添加评论时发送它,并且您没有在方法中使用它.

关于java - 无法命中所需 Controller 的 POST 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27548562/

相关文章:

java - 将外部 .exe 嵌入到 Maven 项目中的 .jar 文件中

java - 未找到提供程序 com.sun.xml.rpc.client.ServiceFactoryImpl

java - Spring-远程基本身份验证

mysql - 将图像保存到 mysql 数据库 - JSP 和 Servlet

java - LinkedList 程序无响应;可能的空指针错误

java - JPA JOIN 查询列表仅给出一项而不是所有项

spring - 在Spring Boot中使用Web Client Mono获取API响应错误消息

java - Spring电子邮件模板,隐藏收件人

java - 无论如何我可以将 Map 转换为 POJO 然后我可以在 JSP EL 中使用吗?

javascript - 当输入类型 "file"不为空时,如何在JS中显示按钮类型?