2024.3.7 Thursday
Contents
- 5. Data Processing and Redirection
- 5.1. Methods of Result Redirection
- 5.1.1. ModelAndView
- 5.1.2. ServletAPI
- 5.1.3. SpringMVC
- 5.1.3.1. Implement forwarding and redirection through SpringMVC without using a view resolver
- 5.1.3.2. Comment out the view resolver
- 5.1.3.3. Create ResultSpringMVC.java
- 5.1.3.4. Use a view resolver and implement forwarding and redirection through SpringMVC
- 5.1.3.5. Uncomment the view resolver
- 5.1.3.6. Create ResultSpringMVC1.java
- 5.1.3.7. Results
5. Data Processing and Redirection
5.1. Methods of Result Redirection
5.1.1. ModelAndView
Set up a ModelAndView object and jump to the specified page according to the name of the view and the view resolver.
Page: {View Resolver Prefix} + viewName + {View Resolver Suffix}
<!-- View Resolver -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"
id="internalResourceViewResolver">
<!-- Prefix -->
<property name="prefix" value="/WEB-INF/jsp/" />
<!-- Suffix -->
<property name="suffix" value=".jsp" />
</bean>
The corresponding controller class
public class ControllerTest1 implements Controller {
public ModelAndView handleRequest(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {
//return a ModelAndView object
ModelAndView mv = new ModelAndView();
mv.addObject("msg","ControllerTest1");
mv.setViewName("test");
return mv;
}
}
5.1.2. ServletAPI
1. Set up ServletAPI without needing a view resolver
- Output via HttpServletResponse
- Redirect via HttpServletResponse
- Forward via HttpServletResponse
2. Create ResultGo.java
package com.kuang.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@Controller
public class ResultGo {
// http://localhost:8080/springmvc_04_controller_war_exploded/result/t1
@RequestMapping("/result/t1")
public void test1(HttpServletRequest req, HttpServletResponse rsp) throws IOException {
rsp.getWriter().println("Hello,Spring BY servlet API");
}
// Entering the following two URLs results in a 404 error
@RequestMapping("/result/t2")
public void test2(HttpServletRequest req, HttpServletResponse rsp) throws IOException {
rsp.sendRedirect("/index.jsp");
}
@RequestMapping("/result/t3")
public void test3(HttpServletRequest req, HttpServletResponse rsp) throws Exception {
//Forwarding
req.setAttribute("msg","/result/t3");
req.getRequestDispatcher("/WEB-INF/jsp/test.jsp").forward(req,rsp);
}
}
5.1.3. SpringMVC
5.1.3.1. Implement forwarding and redirection through SpringMVC without using a view resolver
5.1.3.2. Comment out the view resolver
5.1.3.3. Create ResultSpringMVC.java
package com.kuang.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class ResultSpringMVC {
@RequestMapping("/rsm/t1")
public String test1(){
//Forwarding
return "/index.jsp";
}
@RequestMapping("/rsm/t2")
public String test2(){
//Secondary forwarding
return "forward:/index.jsp";
}
@RequestMapping("/rsm/t3")
public String test3(){
//Redirect, WEB-INF files cannot be accessed through redirect
return "redirect:/index.jsp";
}
}
1. Forwarding
http://localhost:8080/springmvc_04_controller_war_exploded//rsm/t1
http://localhost:8080/springmvc_04_controller_war_exploded//rsm/t2
2. Redirect
- Before pressing Enter:
http://localhost:8080/springmvc_04_controller_war_exploded//rsm/t3 - After pressing Enter, the link changes:
http://localhost:8080/springmvc_04_controller_war_exploded/index.jsp - Result
5.1.3.4. Use a view resolver and implement forwarding and redirection through SpringMVC
5.1.3.5. Uncomment the view resolver
5.1.3.6. Create ResultSpringMVC1.java
package com.kuang.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class ResultSpringMVC1 {
@RequestMapping("/rsm1/t1")
public String test1(){
//Forwarding
return "test";
}
@RequestMapping("/rsm1/t2")
public String test2(){
//Redirect
return "redirect:/index.jsp";
//return "redirect:hello.do"; //hello.do is another request
}
}
5.1.3.7. Results
1. Results
http://localhost:8080/springmvc_04_controller_war_exploded//rsm1/t1
2. Redirect
http://localhost:8080/springmvc_04_controller_war_exploded//rsm1/t2
=> Redirected to redirect:/index.jsp
http://localhost:8080/springmvc_04_controller_war_exploded/index.jsp