zoukankan      html  css  js  c++  java
  • springMVC中跳转问题

      

    在使用SpringMVC时遇到了这个跳转的问题很头疼。现在总结出来,对以后的开发有所帮助。

    1、可以采用ModelAndView:

    @RequestMapping("test1")
    public ModelAndView test(){
        ModelAndView view = new ModelAndView();
        view.addAllObjects(map);
        view.setViewName("redirect:http://localhost:8080/springMVC1/test2");		
        return view;
    }
    

    如果需要在Controller之间传递参数的时候,这种方式只能通过在访问路径后面加上参数以及将参数放到session域中的方式传递参数。

    这种方式也可用于实现Controller到页面之间的跳转。并且可以传参数

    public ModelAndView handleRequest(HttpServletRequest arg0,
                HttpServletResponse arg1) throws Exception {
            System.out.println("hello springMVC");
            Map<String,String> map = new HashMap<String,String>();
            map.put("msg", "你好Springmvc");
            return new ModelAndView("/welcome","map",map);
        }

    ModelAndView构造方法中第一个参数是页面的访问路径(前面加上一个“/”表示从根路径开始。)后面两个参数就相当于键值对。在页面中通过el表达式取出键为map的值(map)${map}

    ModelAndView跳转页面和跳转Controller的差距在于地址上跳转Controller有一个redirect:表示。跳转页面则没有

    2、通过request.getRequestDispatch的方式跳转

    public ModelAndView test(HttpServletRequest request,HttpServletResponse response){
        try {
            request.getRequestDispatcher("/test2").forward(request, response);
        } catch (ServletException e) {
            e.printStackTrace(); 
        } catch (IOException e) {
        e.printStackTrace();
        }
    
    }

    这种方式跳转到另一个页面时可以通过request.setAttribute(key,value)的方式将参数传到另一个Controller中。并且如果执行这个跳转以后,程序后面的代码就不会再执行了。
    如果是想从Controller跳转到页面则只需要将地址改为页面的地址就可以了。

    注意:

    因为在项目里我需要从一个页面的Ajax请求到一个Controller进行处理该Ajax的请求,然后根据处理的结果跳转到不同的页面。我之前的做法是在处理Ajax请求的Controller里进行下一个页面的跳转。试了从处理该Ajax请求的Controller里直接跳转到下个页面,或是跳到下一个Controller在跳转到需要的页面中。使用了ModelAndView的方法和Request.getRequestDispatch 的方法都没有用,最终的目的都不会跳转到目的页面,出现两种问题:

    1、使用Request的方法时,跳转一直跳到本页面

    2、使用ModelAndView时,响应到了目的页面的内容,但是界面没有跳转

    最后得出结论。使用这两中方方式都不会跳转,具体原因不知道。我想可能是使用Ajax请求一定会有一个放回的数据,所以SpringMVC不知道怎么处理放回数据和跳转页面的关系?

    得出的一个解决办法是:

    function test(){
            $.ajax({
                url:'testSkip/test3.do',
                type:'post',
                dataType:'json',
                success:function(data){
                    console.info(data);
                    var url = data.url ;
                    alert(url);
                    window.location.href=url ;
                },
                error:function(){
                    alert("error");
                }            
            });
        }
    @RequestMapping("test3")
        @ResponseBody
        public Object test3(){
            Result result = new Result();
            result.setUrl("testSkip2/test3.do");
            return result ;
        }
    }
    @Controller
    @RequestMapping("testSkip2")
    public class TestController2 {

    @RequestMapping("test3") public ModelAndView test2(){ System.out.println("跳转页面2.2"); return new ModelAndView("/company/company_on_line","result","数据"); }
    }

    在Ajax请求在两个Controller或是Controller与页面之间跳转之前插入一步回到原来的Ajax请求页面通过  window.location.href=url ; 再次发送请求。这是该请求的地址可以使一个Controller地址和页面地址。这是跳转就没问题。

  • 相关阅读:
    第十五节课:习题讲解
    第十四节课:字典
    Python第十三节课-文件的读取和写入
    Python第十二节课--循环语句与注释
    Python第十一节课--字符串的格式化
    Python第十节课==对象的方法
    Python第九节课--初识函数
    初识函数--文件的读取和打开,已一节课一节课分开,可不看
    刷题647. Palindromic Substrings
    刷题617. Merge Two Binary Trees
  • 原文地址:https://www.cnblogs.com/fsh1542115262/p/3800273.html
Copyright © 2011-2022 走看看