简体   繁体   中英

Spring controller not passing a parameter to JSP to display as input value

I've been working on a tutorial to build a simple spring webapp.

In the app i have a list of tasks displayed from a CRUD repository successfully in the JSP view. In this tasks list view I have the option to UPDATE a single task.

The issue is that the update form view is not retrieving the task attributes in the input value. I'm not getting any error so I'm puzzle on what I'm missing.

The controller is getting the attributes since i can print them in the console.

Controller:

@GetMapping("/update-tasks")
    public String updateTasks(@RequestParam int id, HttpServletRequest req)  {
        req.setAttribute("tasks", taskService.findTask(id));
        System.out.println(taskService.findTask(id).getDescription()); ----> this is just to test if I was getting the description attribute.
        req.setAttribute("mode", "MODE_UPDATE");
        return "index";
    }

JSP update form view:

<c:when test="${mode == 'MODE_NEW'||mode == 'MODE_UPDATE' }" >
            <div class="container text-center">
                <h3>Manage Task</h3>
                <hr>
                <form class="form-horizontal" method="POST" action="save-task">
                    <input type="hidden" name="id" value="${task.id}"/>
                    <div class="form-group row">                        
                        <label class="control-label col-md-3">Name</label>      
                        <div class="col-md-7">              
                            <input type="text" class="form-control" name="name" value="${task.name}"/>
                        </div>          
                    </div>
                    <div class="form-group row">
                        <label class="control-label col-md-3">Description</label>
                        <div class="col-md-7">
                            <input type="text" class="form-control" name="description" value="${task.description}"/>
                        </div>              
                    </div>
                    <div class="form-group row">
                        <label class="control-label col-md-3">Finished</label>
                        <div class="col-md-7">
                            <div class="form-row">
                                <input type="radio" class="col-sm-1" name="finished" value="true"/>
                                <div class="col-sm-1">Yes</div>
                                <input type="radio" class="col-sm-1" name="finished" value="false" checked/>
                                <div class="col-sm-1">No</div>
                            </div>
                        </div>              
                    </div>      
                    <div class="form-group">
                        <input type="submit" class="btn btn-primary" value="Save"/>
                    </div>
                </form>
            </div>  
        </c:when>

JSP tasks list view. This section of the JSP does work with JSTL:

<c:when test="${mode == 'MODE_TASKS' }" >
                <div class="container text-center" id="tasksDiv">
                <h3>My Tasks</h3>
                <hr>
                <div class="table-responsive">

                    <table class="table table-striped table-bordered table-hover"">
                        <thead>
                            <tr class="table-dark">
                                <th>Id</th>
                                <th>Name</th>
                                <th>Description</th>
                                <th>Date created</th>
                                <th>Finished</th>
                                <th></th>
                                <th></th>

                            </tr>
                        </thead>

                        <tbody>
                            <c:forEach items="${tasks}" var="task" >
                                <tr>
                                    <td>${task.id}</td>
                                    <td>${task.name}</td>
                                    <td>${task.description}</td>
                                    <td><fmt:formatDate pattern="yyyy-MM-dd HH:mm:ss" value="${task.dateCreated}"/></td>
                                    <td>${task.finished}</td>
                                    <td><a href="update-tasks?id=${task.id}"><i class="fa fa-pencil" aria-hidden="true"></i></a></td>
                                    <td><a href="delete-tasks?id=${task.id}"><i class="fa fa-trash-o" aria-hidden="true"></i></a></td>
                                </tr>
                            </c:forEach>                
                        </tbody>            
                    </table>

                </div>      
            </div>
        </c:when>

Solution

I used ModelAndView object to pass values from conttroller to view, so my update controller looks like this now:

@GetMapping("/update-tasks")
    public ModelAndView updateTasks(@RequestParam int id)  {

        ModelAndView mv = new ModelAndView();
        mv.addObject("task", taskService.findTask(id));
        mv.addObject("mode", "MODE_UPDATE");
        mv.setViewName("index");
        return mv;
    }

You will have to do few changes in order for it to work i believe.

In Controller instead of returning String, you should return Viewable and pass index.jsp in its constructor.

@GetMapping("/update-tasks")
@Produces(MediaType.TEXT_HTML)
public String updateTasks(@RequestParam int id, @Context HttpServletRequest req)  {
    req.setAttribute("tasks", taskService.findTask(id));
    System.out.println(taskService.findTask(id).getDescription()); 
    req.setAttribute("mode", "MODE_UPDATE");
    return new Viewable("/index.jsp", null);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM