Home>

I am writing an issue tracker in Spring MVC, there is an entity Issue and there may be several comments for it (entity IssueComment). In the DB, each IssueComment has a foreign key to the issue.id. The controller method viewAndComment returns a jsp page where the task and a list of comments to it are displayed.

@RequestMapping ("/viewandcomment")
    public String viewAndComment (@RequestParam ("issueId") int id, Model model) {
        List <
IssueComment >
 listComment= issueService.getCommentList (id);
        Issue issue= issueService.getIssueById (id);
        IssueComment issueComment= new IssueComment ();
        model.addAttribute ("listComment", listComment);
        model.addAttribute ("issue", issue);
        model.addAttribute ("issueComment", issueComment);
        return "view-and-comment";
}

A list of Issue-specific listComments is passed to the model, the Issue object itself, and a new IssueComment object that will be populated with the input in the form.

<
h2 >
<
$ {issue.name} >
<
/h2 >
<
h2 >
<
a href= "/" >
Back to Issues list <
/a >
<
/h2 >
<
br >
<
br >
<
br >
<
div >
<
strong >
Status: <
/strong >
$ {issue.status} <
/div >
<
div >
<
strong >
Start date: <
/strong >
$ {issue.dateOfCreation} <
/div >
<
div >
<
strong >
Author: <
/strong >
$ {issue.author} <
/div >
<
div >
<
strong >
Description: <
/strong >
<
/div >
<
div >
$ {issue.description} <
/div >
<
div >
$ {issue.id} <
/div >
<
br >
<
div >
<
strong >
Comments: <
/strong >
<
/div >
<
br >
<
c: forEach var= "listComment" items= "$ {listComment}" >
    <
div >
Update by: $ {listComment.commentAuthor} $ {listComment.dateOfComment} <
/div >
    <
div >
Status changed to: $ {listComment.statusOfComment} <
/div >
    <
div >
$ {listComment.comment} <
/div >
    <
br >
<
/c: forEach >
<
br >
<
div >
<
strong >
Add comment: <
/strong >
<
/div >
<
form: form action= "/addComment" modelAttribute= "issueComment" >
    <
form: hidden path= "issue" />
    <
table border= "1" width= "400" >
        <
tr >
            <
td >
Status: <
/td >
 <
td >
 <
form: select path= "statusOfComment" >
            <
form: option value= "Created" label= "Created" />
            <
form: option value= "Resolved" label= "Resolved" />
            <
form: option value= "Closed" label= "Closed" />
                    <
/form: select >
        <
/td >
        <
/tr >
                     <
tr >
            <
td >
Author: <
/td >
            <
td >
<
form: input path= "commentAuthor" />
<
/td >
<
form: errors path= "commentAuthor" />
        <
/tr >
        <
tr >
            <
td >
Text: <
/td >
            <
td >
<
form: textarea path= "comment" />
<
/td >
            <
form: errors path= "comment" />
        <
/tr >
        <
td colspan= "3" >
<
input type= "submit" value= "Add" />
<
/td >
    <
/table >
<
/form: form >
<
/body >
<
/html >

Entity IssueComment

private int id;
@Column (name= "comment_author")
@NotEmpty (message= "author is required")
private String commentAuthor;
@Column (name= "date_of_comment")
private String dateOfComment;
@Column (name= "status_of_comment")
private String statusOfComment;@Column (name= "comment") @ NotEmpty (message= "comment is required")
private String comment;
@ManyToOne (cascade= {CascadeType.PERSIST, CascadeType.REFRESH, CascadeType.DETACH, CascadeType.MERGE})
@JoinColumn (name= "issue_id")
    private Issue issue;

However, when the /addcomment method is called from the jsp form with the entered fields, an empty Issue object is added to the IssueComment object (respectively, id= 0), and not the one that is specified in the model and has an id to which this comment will refer. How can you get in the "addComment" method the id of the Issue object to which it should be attached and which was passed in the viewAndComment method?

@RequestMapping ("/addComment")
    public String addComment (@Valid @ModelAttribute ("issueComment") IssueComment issueComment, BindingResult bindingResult) {
        if (bindingResult.hasErrors ()) {
            return "view-and-comment";
        }
    int issueId= issueComment.getIssue (). getId ();
issueCommentService.saveNewIssueComment (issueComment, issueId);
    return "redirect: /";
}

Questions asking for help with debugging ("why isn't this code working?") Should include the desired behavior, the specific problem or bug, and the minimum code to reproduce it right in the question. Questions without an explicit description of the problem are of no use to other visitors. See How to create a minimal, self-contained, and reproducible example.

Roman C2021-11-29 13:29:54

how to create a minimal, self-contained and reproducible example

aleksandr barakin2021-11-29 18:21:53