为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

慕虎7371278 2019-02-16 05:25:48
@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; }
查看完整描述

2 回答

?
慕哥6287543

TA贡献1831条经验 获得超10个赞

这里的SQL,业务逻辑,和视图控制都耦合在一起了。
你这样不好分离也不好重构。
先重构分层一下,看看。

查看完整回答
反对 回复 2019-03-01
?
红颜莎娜

TA贡献1842条经验 获得超12个赞

直接设置新的视图

view.setView(viewName);
查看完整回答
反对 回复 2019-03-01
  • 2 回答
  • 0 关注
  • 510 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信