javascript
(SpringMVC)数据处理及跳转
文章目錄
- 1. 結(jié)果跳轉(zhuǎn)方式
- 1.1 ModelAndView
- 1.2 ServletAPI
- 1.3 SpringMVC
- 2. 數(shù)據(jù)處理
- 2.1 處理提交數(shù)據(jù)
- 2.2 數(shù)據(jù)顯示到前端
1. 結(jié)果跳轉(zhuǎn)方式
1.1 ModelAndView
設(shè)置ModelAndView對象 , 根據(jù)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>對應(yīng)的controller類
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;} }1.2 ServletAPI
通過設(shè)置ServletAPI , 不需要視圖解析器 .
1、通過HttpServletResponse進(jìn)行輸出
2、通過HttpServletResponse實現(xiàn)重定向
3、通過HttpServletResponse實現(xiàn)轉(zhuǎn)發(fā)
@Controller public class ResultGo {@RequestMapping("/result/t1")public void test1(HttpServletRequest req, HttpServletResponse rsp) throws IOException {rsp.getWriter().println("Hello,Spring 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 {//轉(zhuǎn)發(fā)req.setAttribute("msg","/result/t3");req.getRequestDispatcher("/WEB-INF/jsp/test.jsp").forward(req,rsp);}}1.3 SpringMVC
通過SpringMVC來實現(xiàn)轉(zhuǎn)發(fā)和重定向 - 無需視圖解析器;
測試前,需要將視圖解析器注釋掉
@Controller public class ResultSpringMVC {@RequestMapping("/rsm/t1")public String test1(){//轉(zhuǎn)發(fā)return "/index.jsp";}@RequestMapping("/rsm/t2")public String test2(){//轉(zhuǎn)發(fā)二return "forward:/index.jsp";}@RequestMapping("/rsm/t3")public String test3(){//重定向return "redirect:/index.jsp";} }通過SpringMVC來實現(xiàn)轉(zhuǎn)發(fā)和重定向 - 有視圖解析器;
重定向 , 不需要視圖解析器 , 本質(zhì)就是重新請求一個新地方嘛 , 所以注意路徑問題.
可以重定向到另外一個請求實現(xiàn) .
@Controller public class ResultSpringMVC2 {@RequestMapping("/rsm2/t1")public String test1(){//轉(zhuǎn)發(fā)return "test";}@RequestMapping("/rsm2/t2")public String test2(){//重定向return "redirect:/index.jsp";//return "redirect:hello.do"; //hello.do為另一個請求/}}2. 數(shù)據(jù)處理
2.1 處理提交數(shù)據(jù)
1、提交的域名稱和處理方法的參數(shù)名一致
提交數(shù)據(jù) : http://localhost:8080/springmvc_04_controller_war_exploded/t3?name=zh
處理方法:
2、提交的域名稱和處理方法的參數(shù)名不一致
提交數(shù)據(jù) : http://localhost:8080/springmvc_04_controller_war_exploded/t3?username=zh
處理方法:
3、提交的是一個對象
提交數(shù)據(jù) : http://localhost:8080/springmvc_04_controller_war_exploded/t3?id=1&name=zh&age=3
處理方法:
創(chuàng)建一個實體類
2.2 數(shù)據(jù)顯示到前端
第一種 : 通過ModelAndView
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
@RequestMapping("/hello") public String hello(@RequestParam("username") String name, ModelMap model){//封裝要顯示到視圖中的數(shù)據(jù)//相當(dāng)于req.setAttribute("name",name);model.addAttribute("name",name);System.out.println(name);return "hello"; }第三種 : 通過Model
@RequestMapping("/ct2/hello") public String hello(@RequestParam("username") String name, Model model){//封裝要顯示到視圖中的數(shù)據(jù)//相當(dāng)于req.setAttribute("name",name);model.addAttribute("msg",name);System.out.println(name);return "test"; }對比
-
Model 只有寥寥幾個方法只適合用于儲存數(shù)據(jù),簡化了新手對于Model對象的操作和理解;
-
ModelMap 繼承了 LinkedMap ,除了實現(xiàn)了自身的一些方法,同樣的繼承 LinkedMap 的方法和特性;
-
ModelAndView 可以在儲存數(shù)據(jù)的同時,可以進(jìn)行設(shè)置返回的邏輯視圖,進(jìn)行控制展示層的跳轉(zhuǎn)。
總結(jié)
以上是生活随笔為你收集整理的(SpringMVC)数据处理及跳转的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: (SpringMVC)RestFul和C
- 下一篇: (SpringMVC)Controlle