Difference between redirect and view rendering in Spring MVC

末鹿安然 提交于 2020-01-02 07:43:29

问题


In learning about Spring MVC knowledge, there are some things about Spring return types that confuse me.

In this documentation: Mapping Requests With @RequestMapping they return appointments/newand redirect:/appointments.

Code

@RequestMapping(method = RequestMethod.POST)
public String add(@Valid AppointmentForm appointment, BindingResult result) {
    if (result.hasErrors()) {
        return "appointments/new";
    }
    appointmentBook.addAppointment(appointment);
    return "redirect:/appointments";
}

What's is the main difference between these two return types? As I understand it, the first type returns as a forward action, but if I'm right, why do they also publish forward:/ as a return type ?


回答1:


First one returns the view while the later redirects to another controller request mapped action. Lets have alook from the code itself.

@RequestMapping(method = RequestMethod.POST)
public String add(@Valid AppointmentForm appointment, BindingResult result) {
    if (result.hasErrors()) {

Here when the result have errors, it renders the view appointments/new so that the user can enter the correct details and add the apppointment again. URL will not change in the browser

        return "appointments/new";
    }
    appointmentBook.addAppointment(appointment);

But here, when the results has no error, this controller action redirect the website to the URL /appointments. Check the web browser URL which is changed to the redirected URL

    return "redirect:/appointments";
}

Regarding forward: vs redirect:

Quoted from this Satckoverflow answer Why do we use redirect in Spring MVC

Using the redirect prefix in your controller will generate a HTTP response with the 302 status code and the location header pointing to the redirection URL. The browser will then redirect to that URL (model exposed in the first request will be lost, and the browser URL will be the second one).

Using the forward prefix, the forward will be done internally by the servlet, so there's no need of a second request (URL will remain the same). The forward prefix should only be used in requests that can be safely repeated by the browser. That's not the case when you send a form which changes the database state (reloading the browser will lead to duplicate submissions). In these cases you should use redirect and apply the POST-redirect-GET pattern.




回答2:


Doing a return of the view name, such as:

return new ModelAndView(<viewpath>/<viewname>);

Doesn't do a forward or a redirect, instead it actually grabs that file server side and processes it as the browser output.

When you return a "redirect:/somePath" it's actually sending a redirect (not a forward) to the browser. A redirect causes a page refresh and happens in the browser and a forward happens on the server side and doesn't change your URL.




回答3:


Se this link. It explains what the difference is between redirect and forward



来源:https://stackoverflow.com/questions/29560633/difference-between-redirect-and-view-rendering-in-spring-mvc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!