Java Can';t击中通缉控制器';s后处理法
当用户输入新的注释时,我想保存它,我的控制器中有一个POST方法可以处理这个问题,但我无法确定该方法的目标。当我调试代码时,它甚至不会进入控制器中的Java Can';t击中通缉控制器';s后处理法,java,spring,jsp,spring-mvc,Java,Spring,Jsp,Spring Mvc,当用户输入新的注释时,我想保存它,我的控制器中有一个POST方法可以处理这个问题,但我无法确定该方法的目标。当我调试代码时,它甚至不会进入控制器中的handleNewComment方法,但我会被重定向到index.jsp页面,尽管在地址栏中有一个有效地址(http://localhost:8080/ycexams-web/showIssue) showIssue.jsp <%@ include file="/common/taglibs.jsp"%> <form:form c
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"/> (${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>
${issue.headline}-
${percentage}%
(${commentCount})
${comment.昵称}
${comment.text}
函数showAddCommentField(){
document.getElementById(“addCommentField”).style.display='block';
document.getElementById(“addCommentButton”).style.display='none';
}
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";
}
}
@控制器
公共类ShowIssueController{
@自动连线
私人发行人管理人;
@自动连线
私人评论经理评论经理;
@InitBinder
公共绑定器(WebDataBinder绑定器){
binder.registerCustomEditor(String.class,新的StringTrimmerEditor(true));
}
@模型属性(“问题”)
私有问题getIssue(最终HttpServletRequest请求,ModelMap模型){
问题=新问题();
注释=新注释();
model.addAttribute(“comment”,comment);
对象issueIdObj=request.getParameter(“issueId”);
如果(issueIdObj!=null){
试一试{
Long issueId=Long.parseLong((字符串)issueIdObj);
issue=issueManager.get(issueId);
List allComments=issue.getComments();
int commentCount=allComments.size();
int normal=issue.getNormal();
int notNormal=issue.getNotNormal();
整数百分比=正常*100/(正常+非正常);
model.addAttribute(“allComments”,allComments);
addAttribute(“commentCount”,commentCount);
model.addAttribute(“百分比”,百分比);
}
捕获(例外e){
model.addAttribute(“异常”、“错误的issueId”);
}
}
退货问题;
}
@RequestMapping(value=“/showIssue”,method=RequestMethod.GET)
公共字符串handleHome(ModelMap模型){
返回“showIssue”;
}
@RequestMapping(value=“/showIssue”,method=RequestMethod.POST)
公共字符串handleNewComment(@modeldattribute(“comment”)最终注释、@RequestParam(“issueId”)最终长度issueId,ModelMap模型){
试一试{
commentManager.save(comment);
}捕获(例外e){
model.addAttribute(“异常”,“保存注释失败,请重试”);
}
返回“showIssue”;
}
}
尝试从方法签名中删除@RequestParam(“issueId”)最终长issueId
,我看不出您在添加注释时发送它,并且您没有在方法中使用它。尝试在您的表单中:
action="<%= request.getContextPath()%>/showIssue"
action=“/showIssue”
实际有效:)但我需要该参数,以便可以将新注释绑定到问题。当我到达那里时,我如何知道问题ID是什么?然后保留参数,并在注释表单中添加
。