結果跳轉和數據處理
結果跳轉方式
ModelAndView
設置 ModelAndView
對象,根據 view
的名稱 和 視圖解析器 跳到指定的頁面。
跳轉的頁面:{視圖解析器前綴} + viewName + {視圖解析器後綴}
<!-- 視圖解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"
id="internalResourceViewResolver">
<!-- 前綴 -->
<property name="prefix" value="/WEB-INF/jsp/" />
<!-- 後綴 -->
<property name="suffix" value=".jsp" />
</bean>
@Controller
public class ControllerTest1 implements org.springframework.web.servlet.mvc.Controller {
@Override
public ModelAndView handleRequest(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {
// 返回一個模型視圖對象
ModelAndView mv = new ModelAndView();
mv.addObject("msg", "ControllerTest1");
mv.setViewName("test");
return mv;
}
}
Servlet API
通過設置 ServletAPI
,不需要視圖解析器。
- 通過
HttpServletResponse
進行 輸出 - 通過
HttpServletResponse
實現 重定向 - 通過
HttpServletResponse
實現 轉發
@Controller
public class ResultGo {
@RequestMapping("/result/t1")
public void test1(HttpServletRequest req, HttpServletResponse rsp) throws IOException {
// ServletAPI 進行輸出
rsp.getWriter().println("Hello, SpringMVC By Servlet API");
}
@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 {
// 轉發
req.setAttribute("msg", "/result/t3");
req.getRequestDispatcher("/WEB-INF/jsp/test.jsp").forward(req, rsp);
}
}
SpringMVC — 無視圖解析器
通過 SpringMVC 來實現轉發和重定向(無需視圖解析器)。
測試前,首先將視圖解析器註釋掉。
@Controller
public class ResultSpringMVC {
@RequestMapping("/rsm/t1")
public String test1() {
// 轉發
return "/index.jsp";
}
@RequestMapping("/rsm/t2")
public String test2() {
// 轉發二
return "forward:/index.jsp";
}
@RequestMapping("/rsm/t3")
public String test3() {
// 重定向
return "redirect:/index.jsp";
}
}
SpringMVC — 有視圖解析器
通過 SpringMVC 來實現轉發和重定向(需要視圖解析器);
重定向不需要視圖解析器,本質就是重新請求一個新地方,所以注意路徑問題。
不一定要重定向到某個頁面,也可以重定向到另外一個請求實現。
@Controller
public class ResultSpringMVC2 {
@RequestMapping("/rsm2/t1")
public String test1() {
// 轉發
return "test";
}
@RequestMapping("/rsm2/t2")
public String test2() {
// 重定向
return "redirect:index.jsp";
// return "redirect:hello.do"; // hello.do 爲另一個請求
}
}
處理提交數據
1、提交的域名稱和處理方法的參數名一致
提交數據 : http://localhost:8080/hello/h1?name=zhenyu
@RequestMapping("/hello/h1")
// 提交的域名稱和處理方法的參數名一致
public String hello(String name) {
System.out.println(name);
return "hello";
}
2、提交的域名稱和處理方法的參數名不一致
提交數據:http://localhost:8080/hello/h2?username=zhenyu
@RequestMapping("/hello/h2")
// 提交的域名稱和處理方法的參數名不一致
public String hello2(@RequestParam("username") String name) {
System.out.println(name);
return "hello";
}
3、提交的是一個對象
提交數據:http://localhost:8080/user/u1?id=1&name=zhenyu&age=21
實體類:
@Data
@NoArgsConstructor
@AllArgsConstructor
@ToString
public class User {
private int id;
private String name;
private int age;
}
提交方法:
@RequestMapping("/u1")
public String user(User user) {
System.out.println(user);
return "hello";
}
數據顯示到前端
通過 ModelAndView
利用 ModelAndView
的 addObject
方法:
public class ControllerTest1 implements Controller {
public ModelAndView handleRequest(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {
// 返回一個模型視圖對象
ModelAndView mv = new ModelAndView();
mv.addObject("msg", "ControllerTest1");
mv.setViewName("test");
return mv;
}
}
通過 ModelMap
利用 ModelMap
的 addAttribute
方法:
@RequestMapping("/hello")
public String hello(@RequestParam("username") String name, ModelMap model){
// 封裝要顯示到視圖中的數據
// 相當於 req.setAttribute("name", name);
model.addAttribute("name", name);
System.out.println(name);
return "hello";
}
通過 Model
利用 Mode
的 addAttribute
方法:
@RequestMapping("/ct2/hello")
public String hello(@RequestParam("username") String name, Model model){
// 封裝要顯示到視圖中的數據
// 相當於 req.setAttribute("name", name);
model.addAttribute("msg", name);
System.out.println(name);
return "test";
}
三者對比
對於新手而言簡單來說使用區別就是:
- Model 只有寥寥幾個方法只適合用於儲存數據,簡化了新手對於 Model 對象的操作和理解;
- ModelMap 繼承
LinkedMap
,除了自身的一些方法,同樣的繼承LinkedMap
的方法和特性; - ModelAndView 在儲存數據的同時,可以進行設置返回的邏輯視圖,進行控制展示層的跳轉。
當然更多的以後開發考慮的更多的是性能和優化,就不能單單僅限於此的瞭解。
請使用 80% 的時間打好紮實的基礎,剩下 18% 的時間研究框架,2% 的時間去學點英文,框架的官方文檔永遠是最好的教程!