Controller traditional values page recording method to jsp

Reprinted from: https: //www.cnblogs.com/jpfss/p/8651303.html

SpringMVC controller jumps between redirection method to pass parameters

spring MVC framework between the controller to jump, to be redirected. There are several situations: jump with no arguments, arguments spliced form url jump parameters jump without stitching parameters, the page can be displayed.
Commonly used methods:
(1) to jump from one controller to another method in the method of the controller does not need to pass parameters
way: using ModelAndView
return new new ModelAndView ( "the redirect: / toList");
this can be redirected to another the method of this controller toList
way: returns String
return "the redirect: / toList";
it is redirected without parameters.
(2) the second case, the need to carry when Parameters can be spliced url
way: to manually splicing url
new new ModelAndView ( "redirect:? / ToList param1 =" + value1 + "& param2 =" + value2);
This has the drawbacks, to preach the Chinese might be garbled.
Second way: with RedirectAttributes, this is found in a relatively easy to use a class, SpringMVC own class
using it here addAttribute method, this is actually redirect url after you see the past, it automatically gives you fight your url .
Usage:
public String the Save (RedirectAttributes attr)
attr.addAttribute ( "param", value);
return "the redirect: / namespace / toController";
thus this parameter can be obtained by means of the parameters obtained in this method toController, and then transferred to the page. The past and the way a url or
the same.
Way to get parameters:
request.getParameter ( "productActivityId");
(3) arguments are not spliced url pages can get value (emphasis on this)
general I want to redirect to estimate this way:
@ RequestMapping ( " / Save ")
public String Save (@ModelAttribute (" form ") form Bean, RedirectAttributes attr)
throws Exception {
String code = service.save (form);
IF (code.equals (" 000 ")) {
attr.addFlashAttribute ( "name", form.getName ());
attr.addFlashAttribute ( "success", "Add success!");
return "the redirect: / index";
} the else {
attr.addAttribute ( "the projectName", form.getProjectName () );
attr.addAttribute ( "Enviroment", form.getEnviroment ());
attr.addFlashAttribute ( "MSG", "add error");
return "the redirect: / Maintenance / toAddConfigCenter";
}
}
addFlashAttribute () in which information springMVC3 into the session, the page can be obtained directly after the jump to the page with .session el expression
immediately remove the object. So after you refresh the value will be lost.
Summarizes the
essence or two hops, spring is encapsulated;

Guess you like

Origin www.cnblogs.com/jndx-ShawnXie/p/11565930.html