Redirection and forwarding
Redirect (URL change)
The redirect is performed on the browser, because the browser cannot access things in the WEB-INF directory, so when you use redirect, you cannot redirect to that directory and cannot redirect. usereturn "redirect:*";
, the view resolver fails
Forward (forward, url unchanged)
- SpringMVC uses forward by default and can forward to a file in the WEB-INF directory.
- Used when a view parser is available
return "forward:*";
, the view resolver fails
The sample
- View parser
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="resourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
Copy the code
- controller
@Controller
public class RedirectForward {
@GetMapping( "/add")
public String test(Model model){
model.addAttribute("msg"."redirect");
return "redirect:/f/h.html";
}
@GetMapping( "/t/add")
public String test1(Model model){
model.addAttribute("msg"."redirect");
return "redirect";
}
@GetMapping( "/t1/add")
public String test2(Model model){
model.addAttribute("msg"."redirect");
return "forward:/WEB-INF/jsp/redirect.jsp"; }}Copy the code
Receive parameter and data echo
The sample
Use directly
use@RequestParam
How to pass objects
- Suppose an object is passed, matching the name of the field in the object; If the names are the same, OK; otherwise, no match can be found
null
severalModel
The difference between
- Model has only a few methods suitable for storing data, simplifying the operation and understanding of Model objects for beginners;
- ModelMap inherits LinkedMap. In addition to implementing some of its own methods, ModelMap also inherits LinkedMap methods and features;
- ModelAndview can store data at the same time, can set the returned logical view, control the jump of the display layer.