這篇文章給大家分享的是有關(guān)Spring mvc結(jié)果跳轉(zhuǎn)方法的示例分析的內(nèi)容。小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,一起跟隨小編過來(lái)看看吧。
專注于為中小企業(yè)提供網(wǎng)站設(shè)計(jì)、成都網(wǎng)站建設(shè)服務(wù),電腦端+手機(jī)端+微信端的三站合一,更高效的管理,為中小企業(yè)塔什庫(kù)爾干塔吉克免費(fèi)做網(wǎng)站提供優(yōu)質(zhì)的服務(wù)。我們立足成都,凝聚了一批互聯(lián)網(wǎng)行業(yè)人才,有力地推動(dòng)了千余家企業(yè)的穩(wěn)健成長(zhǎng),幫助中小企業(yè)通過網(wǎng)站建設(shè)實(shí)現(xiàn)規(guī)模擴(kuò)充和轉(zhuǎn)變。
ModelAndView
設(shè)置ModelAndView對(duì)象 , 根據(jù)view的名稱 , 和視圖解析器跳到指定的頁(yè)面 .
頁(yè)面 : {視圖解析器前綴} + viewName +{視圖解析器后綴}
<!-- 視圖解析器 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="internalResourceViewResolver"> <!-- 前綴 --> <property name="prefix" value="/WEB-INF/jsp/" /> <!-- 后綴 --> <property name="suffix" value=".jsp" /> </bean>
對(duì)應(yīng)的controller類
public class ControllerTest1 implements Controller { public ModelAndView handleRequest(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception { //返回一個(gè)模型視圖對(duì)象 ModelAndView mv = new ModelAndView(); mv.addObject("msg","ControllerTest1"); mv.setViewName("test"); return mv; } }
ServletAPI
通過設(shè)置ServletAPI , 不需要視圖解析器 .
通過HttpServletResponse進(jìn)行輸出
通過HttpServletResponse實(shí)現(xiàn)重定向
通過HttpServletResponse實(shí)現(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); } }
SpringMVC
通過SpringMVC來(lái)實(shí)現(xiàn)轉(zhuǎn)發(fā)和重定向 - 無(wú)需視圖解析器;
測(cè)試前,需要將視圖解析器注釋掉
@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來(lái)實(shí)現(xiàn)轉(zhuǎn)發(fā)和重定向 - 有視圖解析器;
重定向 , 不需要視圖解析器 , 本質(zhì)就是重新請(qǐng)求一個(gè)新地方嘛 , 所以注意路徑問題.
可以重定向到另外一個(gè)請(qǐng)求實(shí)現(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為另一個(gè)請(qǐng)求/ } }
感謝各位的閱讀!關(guān)于“Spring mvc結(jié)果跳轉(zhuǎn)方法的示例分析”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,讓大家可以學(xué)到更多知識(shí),如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到吧!
文章題目:Springmvc結(jié)果跳轉(zhuǎn)方法的示例分析
當(dāng)前路徑:http://m.newbst.com/article12/gdeegc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站策劃、網(wǎng)站收錄、網(wǎng)站制作、網(wǎng)站排名、自適應(yīng)網(wǎng)站、建站公司
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)