国产成人精品亚洲777人妖,欧美日韩精品一区视频,最新亚洲国产,国产乱码精品一区二区亚洲

您的位置:首頁技術文章
文章詳情頁

Spring mvc結果跳轉方法詳解

瀏覽:5日期:2023-09-15 17:30:40

ModelAndView

設置ModelAndView對象 , 根據view的名稱 , 和視圖解析器跳到指定的頁面 .

頁面 : {視圖解析器前綴} + viewName +{視圖解析器后綴}

<!-- 視圖解析器 --><bean id='internalResourceViewResolver'> <!-- 前綴 --> <property name='prefix' value='/WEB-INF/jsp/' /> <!-- 后綴 --> <property name='suffix' value='.jsp' /></bean>

對應的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; }}

ServletAPI

通過設置ServletAPI , 不需要視圖解析器 .

通過HttpServletResponse進行輸出 通過HttpServletResponse實現重定向 通過HttpServletResponse實現轉發(fā)

@Controllerpublic 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 { //轉發(fā) req.setAttribute('msg','/result/t3'); req.getRequestDispatcher('/WEB-INF/jsp/test.jsp').forward(req,rsp); }}

SpringMVC

通過SpringMVC來實現轉發(fā)和重定向 - 無需視圖解析器;

測試前,需要將視圖解析器注釋掉

@Controllerpublic class ResultSpringMVC { @RequestMapping('/rsm/t1') public String test1(){ //轉發(fā) return '/index.jsp'; } @RequestMapping('/rsm/t2') public String test2(){ //轉發(fā)二 return 'forward:/index.jsp'; } @RequestMapping('/rsm/t3') public String test3(){ //重定向 return 'redirect:/index.jsp'; }}

通過SpringMVC來實現轉發(fā)和重定向 - 有視圖解析器;

重定向 , 不需要視圖解析器 , 本質就是重新請求一個新地方嘛 , 所以注意路徑問題.

可以重定向到另外一個請求實現

@Controllerpublic class ResultSpringMVC2 { @RequestMapping('/rsm2/t1') public String test1(){ //轉發(fā) return 'test'; } @RequestMapping('/rsm2/t2') public String test2(){ //重定向 return 'redirect:/index.jsp'; //return 'redirect:hello.do'; //hello.do為另一個請求/ }}

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網。

標簽: Spring
相關文章:
主站蜘蛛池模板: 德格县| 南投市| 太仆寺旗| 泗水县| 庆元县| 周至县| 通许县| 宁南县| 万安县| 潜江市| 双鸭山市| 义马市| 湘乡市| 永胜县| 龙泉市| 周至县| 泸水县| 威宁| 上思县| 白朗县| 抚宁县| 北票市| 中山市| 澄江县| 中方县| 临清市| 湖州市| 大埔县| 阿荣旗| 秦安县| 云南省| 无锡市| 长汀县| 信宜市| 富宁县| 霸州市| 桐乡市| 教育| 天台县| 高尔夫| 岗巴县|