如下程序,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;
}


--cut--


直男一隻在2015-11-11 19:50:55回答到:


這裡的SQL,業務邏輯,和視圖控制都耦合在一起瞭。

你這樣不好分離也不好重構。

先重構分層一下,看看。


Infinite在2015-11-11 16:07:18回答到:


直接設置新的視圖


view.setView(viewName);

要回復問題請先登錄註冊