Assume the view resolver is set as follows.
servlet-context.xml
<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<beans:property name="prefix" value="/page/" />
<beans:property name="suffix" value=".jsp" />
</beans:bean>
Copy the code
Assume that on the login page, you want to go to the Home page after the successful login. The controller returns “home”,” redirect:home”, and “forward:home”, respectively.
The return value | action | url |
---|---|---|
“home” | The view parser automatically adds a prefix of /page/ and a suffix of /.jsp/. So forward to /page/home.jsp | The URL is still /login |
“redirect:home” | Returns a request for the corresponding URL (/home). Redirect to another controller for processing | Url into/home |
“forward:home” | Returns a request for the corresponding URL (/home). Forward to another controller for processing | The URL is still /login |
@Controller
public class TestLoginController {
@Autowired
TestLoginService service;
@RequestMapping("login")
public String login(Users user, HttpServletRequest request) {
boolean result = service.login(user);
if (result) {
request.getSession().setAttribute("user", user.getUserid()); } / /return "home"; Go straight back to the view, which is /page/home.jsp. But the URL is still /login //return "redirect:home"; Returns a request for the corresponding URL (/home). Redirect to another controller for processing. The URL changes to /home//return "forward:home"; Returns a request for the corresponding URL (/home). Forward to another controller for processing. But the URL is still /login}}Copy the code