开发者社区> 问答> 正文

Springmvc中 controller如何返回不同的modelandview?

如下程序,Springmvc中 controller可以在内部加一个if语句,返回不同的modelandview吗?

@RequestMapping(value = "/index")
public ModelAndView index(Page<ZqbpContract> p2, ZqbpContract params,
        HttpServletRequest request,String id,String name,String god,String oneid,String twoid,String s) throws SQLException {
    ModelAndView view = new ModelAndView("/Admin/Contract/contract-index");
    HttpSession session = request.getSession();
    ZqbpUser user = (ZqbpUser) session.getAttribute(ConfigUtil.USER);
    ConnectionSource connectionSource = DBUtil.getConnectionSource();
    Page<ZqbpContract> p;
    
    String l="";
    
        
    
    String sql = "select contract_id,contract_name,contract_filename,contract_money,contract_num,contract_status,contract_type,contract_url,con_money_fangshi,con_xitong,create_time,stop_time,user_id,custom_id,flow_id,(select product_name from zqbp_product where product_id=con_chanpin) as con_chanpin,(select sum(money) from zqbp_con_m where con_id=contract_id) as create_user from zqbp_contract where status='0'";
    
    int a=0;
    if(id!=null&&id!=""){
        a=Integer.parseInt(id.toString());
        if(a!=2&&a!=3&&a!=4&&a!=5){
        sql+=" and custom_id like '%"+id+"%'";
        }
    }
    
    if(god!=""&&god!=null){
        int x=Integer.parseInt(god);
        if(x==1){
        List<ZqbpContract> c=(List<ZqbpContract>)session.getAttribute("list6");
        for(ZqbpContract t:c){
            l+="'"+t.getContractId()+"',";
        }
        if(l.endsWith(",")){
            l=l.substring(0, l.length()-1);
        }
        sql+=" and contract_id in ("+l+")";
        }else if(x==2){
            Page<ZqbpContract> c=(Page<ZqbpContract>)session.getAttribute("ct");
            if(c.getItem()!=null){
            //如果判断为空的话,我想返回不同的页面应该怎么写啊?@@@@@@@@@@@@@@@@@@@
            for(ZqbpContract t:c.getList()){
                System.out.println(123);
                l+="'"+t.getContractId()+"',";
            }
            if(l.endsWith(",")){
                l=l.substring(0, l.length()-1);
            }
            sql+=" and contract_id in ("+l+")";
            }else{
                sql+=" and contract_id in ('zqbp_contract')";    
            }
        }else{
            List<ZqbpContract> c=(List<ZqbpContract>)session.getAttribute("tac");
            
            for(ZqbpContract t:c){
                l+="'"+t.getContractId()+"',";
            }
            if(l.endsWith(",")){
                l=l.substring(0, l.length()-1);
            }
            sql+=" and contract_id in ("+l+")";
            
        }
    }
    if(oneid!=""&&oneid!=null){
        sql+=" and contract_id='"+oneid+"'";
    }
    if(twoid!=""&&twoid!=null){
        sql+=" and contract_id='"+twoid+"'";
    }
    String aa="";
    if(a==3){
        List<ZqbpContract> cc=(List<ZqbpContract>)session.getAttribute("aa");
        if(cc.get(0).getContractId()!=null){
            for(ZqbpContract ccc:cc){
                aa+="'"+ccc.getContractId()+"',";
            }
            if(aa.endsWith(",")){
                aa = aa.substring(0, aa.length() - 1);
            }
            sql+=" and contract_id in ("+aa+")";
        }
    }
    if(a==2){
        List<ZqbpContract> cc=(List<ZqbpContract>)session.getAttribute("list1");
        if(cc.get(0).getContractId()!=null){
            for(ZqbpContract ccc:cc){
                aa+="'"+ccc.getContractId()+"',";
            }
            if(aa.endsWith(",")){
                aa = aa.substring(0, aa.length() - 1);
            }
            sql+=" and contract_id in ("+aa+")";
        }
    }
    if(a==4){
        List<ZqbpContract> cc=(List<ZqbpContract>)session.getAttribute("list2");
        if(cc.get(0).getContractId()!=null){
            for(ZqbpContract ccc:cc){
                aa+="'"+ccc.getContractId()+"',";
            }
            if(aa.endsWith(",")){
                aa = aa.substring(0, aa.length() - 1);
            }
            sql+=" and contract_id in ("+aa+")";
        }
    }
    if(a==5){
        List<ZqbpContract> cc=(List<ZqbpContract>)session.getAttribute("list3");
        if(cc.get(0).getContractId()!=null){
            for(ZqbpContract ccc:cc){
                aa+="'"+ccc.getContractId()+"',";
            }
            if(aa.endsWith(",")){
                aa = aa.substring(0, aa.length() - 1);
            }
            sql+=" and contract_id in ("+aa+")";
        }
    }
    String dat="";
    String u="";
    if(name!=null&&name!=""){
        
            sql+=" and stop_time like '%"+name+"%'";
    }
    if(s!=null&&s!=""){
        
        sql+=" and custom_id like '%"+s+"%'";
}
    if(p2.getPage()==1){
        p2.setPage(0);
    }
    p = DBUtil.getMySqlPageSql(ZqbpContract.class, p2, sql);
    p2.setList(p.getList());
    p2.setPage(p.getPage()<=0?1:p.getPage());
    p2.setPageCount(p.getPageCount());
    p2.setTotalCount(p.getTotalCount());
    
    for(ZqbpContract c:p2.getList()){
        if(c.getCreateUser()!=null && !"".equals(c.getCreateUser())){
            c.setCreateUser(Integer.parseInt(c.getContract_money())-Integer.parseInt(c.getCreateUser())+"");
        }else{
            c.setCreateUser("未收款");
        }
    }
    view.addObject("page", p2);

    return view;
}

展开
收起
蛮大人123 2016-03-06 10:15:54 4120 0
1 条回答
写回答
取消 提交回答
  • 我说我不帅他们就打我,还说我虚伪

    这里的SQL,业务逻辑,和视图控制都耦合在一起了。
    你这样不好分离也不好重构。
    先重构分层一下,看看。
    或者直接设置新的视图
    view.setView(viewName);

    2019-07-17 18:54:08
    赞同 展开评论 打赏
问答地址:
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载