Question

I'm trying to create a Employee list creation. In that to create a employee i use service. after creating i need to redirect to the students list page. My sample code is given below by this, the last added item is getting duplicated for each refresh of the page. Please advice!

@RequestMapping(value = "/students", method = RequestMethod.GET)
    public ModelAndView students() {

        List<Student> students=studentService.getAllStudents();
        ModelAndView view=new ModelAndView();
        view.addObject("objects", students);
        view.setViewName("students");
        return view;
    }


@RequestMapping(value = "/addStudent", method = RequestMethod.POST)
    public ModelAndView addStudent(@ModelAttribute("SpringWeb") @Validated Student student, BindingResult bindingResult, 
            ModelMap model) {
        if(bindingResult.hasErrors())
        {
            return new ModelAndView("numberformat", null);
        }

        model.addAttribute("name", student.getName());
        model.addAttribute("age", student.getAge());
        model.addAttribute("id", student.getId());

        studentService.createStudent(student);
//      return new ModelAndView("result", model) ;
        return students();
    }

Students.jsp

        <tr>
            <td><b>ID</b></td>
            <td><b>Name</b></td>
            <td><b>Age</b></td>
            <td><b>Date of Joining</b></td>
        </tr>
        <c:forEach var="o" items="${objects}">
            <tr>
                <td><c:out value="${o.id}" /></td>
                <td>
                <a
                    href="<c:url value="result">   

                            <c:param name="studId" value="${o.id}"/>   
                        </c:url>   
                    "><c:out value="${o.name}"/></a>
                </td>
                <td><c:out value="${o.age}" /></td>
                <td><c:out value="${o.doj}" /></td>
            </tr>
        </c:forEach>

    </table>
</c:if>

Student.jsp

<body>
    <h2>Employee Information</h2>
<form method="POST" action="/HelloWorld/addStudent">

    <table>

        <tr>
            <td><label for="name">Name</label></td>
            <td><input type="text" name="name" /></td>
        </tr>
        <tr>
            <td><label for="age">Age</label></td>
            <td><input type="number" name= "age" /></td>
        </tr>
        <!-- <tr>
            <td><label for="id">id</label></td>
            <td><input type="number" name= "id" /></td>
        </tr> -->
        <tr>
            <td><label for="doj">Date of Joining</label></td>
            <td><input type="date" name= "doj" /></td>
        </tr>
        <tr>
            <td colspan="2"><input type="submit" value="Submit" /></td>
        </tr>
    </table>
</form>

Was it helpful?

Solution

@RequestMapping(value = "/addStudent", method = RequestMethod.POST)
public String addStudent(@ModelAttribute("SpringWeb") @Validated Student student, BindingResult bindingResult, 
        ModelMap model) {
    if(bindingResult.hasErrors())
    {
        return new ModelAndView("numberformat", null);
    }

    model.addAttribute("name", student.getName());
    model.addAttribute("age", student.getAge());
    model.addAttribute("id", student.getId());

    studentService.createStudent(student);

    return "redirect:students";
}

The last line will make Spring return an HTTP 302 to the browser which will then perform a GET to the students URL

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top