无法击中想要的控制器的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条评论
登录后参与评论

相关文章

来自分类Dev

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

来自分类Dev

无法从控制器上的 AJAX post 方法获取参数

来自分类Dev

无法访问控制器方法

来自分类Dev

无法调用控制器的HttpPost方法

来自分类Dev

无法在AngularJS中使用$ .post调用MVC控制器方法以获取更大的数据

来自分类Dev

DNN无法访问DNN Api控制器中的POST方法

来自分类Dev

无法使用POST方法Symfony3将数据发送到控制器

来自分类Dev

RSpec控制器测试无法通过POST创建

来自分类Dev

在控制器中无法访问Rails POST参数

来自分类Dev

控制器中的get和post方法。Ruby on Rails

来自分类Dev

MVC不同的Get和Post到控制器方法

来自分类Dev

如何为POST方法设置WebAPI控制器

来自分类Dev

API 控制器 Post 方法返回类型/值

来自分类Dev

FormData 未进入 API 控制器的 Post 方法

来自分类Dev

无法生成控制器

来自分类Dev

无法路由到控制器中的方法

来自分类Dev

无法从调用异步方法的控制器返回ActionResult

来自分类Dev

无法在控制器中包含和使用组件的方法

来自分类Dev

MVC控制器无法执行Async方法

来自分类Dev

无法通过指令调用父控制器方法

来自分类Dev

Breeze无法解决Web API控制器方法

来自分类Dev

laravel 4无法进入指定控制器方法

来自分类Dev

我无法从控制器静态调用Laravel Cart方法

来自分类Dev

控制器无法识别正确的处理程序方法

来自分类Dev

Grails集成测试无法识别控制器保存方法

来自分类Dev

无法从显示的视图控制器访问委托方法

来自分类Dev

无法获取Web API控制器方法的URL

来自分类Dev

控制器方法无法识别模型参数

来自分类Dev

无法从控制器中的ApplicationHelper模块调用方法

Related 相关文章

热门标签

归档