无法击中想要的控制器的POST方法

军团

当用户输入新注释时,我想保存它,并且我的控制器中有一个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";
    }
}
普雷德拉格·马里奇(Predrag Maric)

尝试@RequestParam("issueId") final Long issueId从方法签名中删除,我看不到您在添加注释时发送了它,并且您没有在方法中使用它。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

Related 相关文章

热门标签

归档