如何将请求重定向到另一个jsp页面/视图

时间:2014-05-09 12:02:43

标签: java spring jsp

我正在尝试创建员工列表创建。在那个创建员工我使用服务。创建后我需要重定向到学生列表页面。我的示例代码如下所示,最后添加的项目在每次刷新页面时都会重复。请指教!

@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>

1 个答案:

答案 0 :(得分:1)

@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";
}

最后一行将使Spring向浏览器返回一个HTTP 302,然后对学生URL执行GET