无法击中所需控制器的POST方法

时间:2014-12-18 14:13:53

标签: java spring jsp spring-mvc

当用户输入新评论时我想保存它,并且我的控制器中有一个应该处理的POST方法,但我在定位该方法时遇到问题。当我调试代码时,它甚至不会在控制器中获得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";
    }
}

2 个答案:

答案 0 :(得分:2)

尝试从方法签名中删除@RequestParam("issueId") final Long issueId,我发现在添加评论时您没有发送,并且您未在方法中使用它。

答案 1 :(得分:0)

以您的形式尝试:

action="<%= request.getContextPath()%>/showIssue"