001/** 002 * Copyright (c) 2015-2022, Michael Yang 杨福海 (fuhai999@gmail.com). 003 * <p> 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * <p> 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * <p> 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package io.jboot.web.controller; 017 018import com.alibaba.fastjson.JSON; 019import com.google.common.collect.Sets; 020import com.jfinal.core.ActionException; 021import com.jfinal.core.Controller; 022import com.jfinal.core.NotAction; 023import com.jfinal.kit.StrKit; 024import com.jfinal.render.RenderManager; 025import com.jfinal.upload.UploadFile; 026import io.jboot.support.jwt.JwtManager; 027import io.jboot.utils.FileUtil; 028import io.jboot.utils.RequestUtil; 029import io.jboot.utils.StrUtil; 030import io.jboot.utils.TypeDef; 031import io.jboot.web.json.JsonBodyParseInterceptor; 032 033import javax.servlet.http.HttpServletRequest; 034import javax.servlet.http.HttpServletRequestWrapper; 035import java.math.BigDecimal; 036import java.math.BigInteger; 037import java.util.*; 038 039 040public class JbootController extends Controller { 041 042 private Object rawObject; 043 private Map jwtParas; 044 private Map<String, Object> jwtAttrs; 045 046 047 @Override 048 protected void _clear_() { 049 super._clear_(); 050 this.rawObject = null; 051 this.jwtParas = null; 052 this.jwtAttrs = null; 053 } 054 055 /** 056 * 是否是手机浏览器 057 * 058 * @return 059 */ 060 @NotAction 061 public boolean isMobileBrowser() { 062 return RequestUtil.isMobileBrowser(getRequest()); 063 } 064 065 /** 066 * 是否是微信浏览器 067 * 068 * @return 069 */ 070 @NotAction 071 public boolean isWechatBrowser() { 072 return RequestUtil.isWechatBrowser(getRequest()); 073 } 074 075 /** 076 * 是否是IE浏览器 077 * 078 * @return 079 */ 080 @NotAction 081 public boolean isIEBrowser() { 082 return RequestUtil.isIEBrowser(getRequest()); 083 } 084 085 /** 086 * 是否是ajax请求 087 * 088 * @return 089 */ 090 @NotAction 091 public boolean isAjaxRequest() { 092 return RequestUtil.isAjaxRequest(getRequest()); 093 } 094 095 /** 096 * 是否是multpart的请求(带有文件上传的请求) 097 * 098 * @return 099 */ 100 @NotAction 101 public boolean isMultipartRequest() { 102 return RequestUtil.isMultipartRequest(getRequest()); 103 } 104 105 106 /** 107 * 获取ip地址 108 * 109 * @return 110 */ 111 @NotAction 112 public String getIPAddress() { 113 return RequestUtil.getIpAddress(getRequest()); 114 } 115 116 /** 117 * 获取 referer 118 * 119 * @return 120 */ 121 @NotAction 122 public String getReferer() { 123 return RequestUtil.getReferer(getRequest()); 124 } 125 126 127 /** 128 * 获取ua 129 * 130 * @return 131 */ 132 @NotAction 133 public String getUserAgent() { 134 return RequestUtil.getUserAgent(getRequest()); 135 } 136 137 138 @NotAction 139 public Controller setJwtAttr(String name, Object value) { 140 if (jwtAttrs == null) { 141 jwtAttrs = new HashMap<>(); 142 } 143 144 jwtAttrs.put(name, value); 145 return this; 146 } 147 148 149 @NotAction 150 public Controller setJwtMap(Map map) { 151 if (map == null) { 152 throw new NullPointerException("Jwt map is null"); 153 } 154 if (jwtAttrs == null) { 155 jwtAttrs = new HashMap<>(); 156 } 157 158 jwtAttrs.putAll(map); 159 return this; 160 } 161 162 163 @NotAction 164 public Controller setJwtEmpty() { 165 jwtAttrs = new HashMap<>(); 166 return this; 167 } 168 169 170 @NotAction 171 public <T> T getJwtAttr(String name) { 172 return jwtAttrs == null ? null : (T) jwtAttrs.get(name); 173 } 174 175 176 @NotAction 177 public Map<String, Object> getJwtAttrs() { 178 return jwtAttrs; 179 } 180 181 182 @NotAction 183 public <T> T getJwtPara(String name, Object defaultValue) { 184 T ret = getJwtPara(name); 185 return ret != null ? ret : (T) defaultValue; 186 } 187 188 @NotAction 189 public <T> T getJwtPara(String name) { 190 return (T) getJwtParas().get(name); 191 } 192 193 194 @NotAction 195 public Integer getJwtParaToInt(String name, Integer defaultValue) { 196 Integer ret = getJwtParaToInt(name); 197 return ret != null ? ret : defaultValue; 198 } 199 200 @NotAction 201 public Integer getJwtParaToInt(String name) { 202 Object ret = getJwtParas().get(name); 203 if (ret instanceof Number) { 204 return ((Number) ret).intValue(); 205 } 206 return ret != null ? Integer.valueOf(ret.toString()) : null; 207 } 208 209 @NotAction 210 public Long getJwtParaToLong(String name, Long defaultValue) { 211 Long ret = getJwtParaToLong(name); 212 return ret != null ? ret : defaultValue; 213 } 214 215 216 @NotAction 217 public Long getJwtParaToLong(String name) { 218 Object ret = getJwtParas().get(name); 219 if (ret instanceof Number) { 220 return ((Number) ret).longValue(); 221 } 222 return ret != null ? Long.valueOf(ret.toString()) : null; 223 } 224 225 226 @NotAction 227 public String getJwtParaToString(String name, String defaultValue) { 228 String ret = getJwtParaToString(name); 229 return StrUtil.isNotBlank(ret) ? ret : defaultValue; 230 } 231 232 233 @NotAction 234 public String getJwtParaToString(String name) { 235 Object ret = getJwtParas().get(name); 236 return ret != null ? ret.toString() : null; 237 } 238 239 240 @NotAction 241 public BigInteger getJwtParaToBigInteger(String name, BigInteger defaultValue) { 242 BigInteger ret = getJwtParaToBigInteger(name); 243 return ret != null ? ret : defaultValue; 244 } 245 246 @NotAction 247 public BigInteger getJwtParaToBigInteger(String name) { 248 Object ret = getJwtParas().get(name); 249 if (ret instanceof BigInteger) { 250 return (BigInteger) ret; 251 } else if (ret instanceof Number) { 252 return BigInteger.valueOf(((Number) ret).longValue()); 253 } 254 return ret != null ? toBigInteger(ret.toString(), null) : null; 255 } 256 257 258 @NotAction 259 public Map getJwtParas() { 260 if (jwtParas == null) { 261 jwtParas = JwtManager.me().parseJwtToken(this); 262 } 263 return jwtParas; 264 } 265 266 267 @NotAction 268 public String createJwtToken() { 269 if (jwtAttrs == null) { 270 jwtAttrs = new HashMap<>(); 271 } 272 return JwtManager.me().createJwtToken(jwtAttrs); 273 } 274 275 /** 276 * 获取当前网址 277 * 278 * @return 279 */ 280 @NotAction 281 public String getBaseUrl() { 282 return RequestUtil.getBaseUrl(getRequest()); 283 } 284 285 286 @NotAction 287 public String getCurrentUrl() { 288 return RequestUtil.getCurrentUrl(getRequest()); 289 } 290 291 292 /** 293 * 接收 Json 转化为 JsonObject 或者 JsonArray 294 * 295 * @return 296 */ 297 @NotAction 298 public <T> T getRawObject() { 299 if (rawObject == null && StrUtil.isNotBlank(getRawData())) { 300 rawObject = JSON.parse(getRawData()); 301 } 302 return (T) rawObject; 303 } 304 305 306 /** 307 * 接收 json 转化为 object 308 * 309 * @param typeClass 310 * @param <T> 311 * @return 312 */ 313 @NotAction 314 public <T> T getRawObject(Class<T> typeClass) { 315 return getRawObject(typeClass, null); 316 } 317 318 319 /** 320 * 接收 json 转化为 object 321 * 322 * @param typeClass 323 * @param jsonKey 324 * @param <T> 325 * @return 326 */ 327 @NotAction 328 public <T> T getRawObject(Class<T> typeClass, String jsonKey) { 329 try { 330 return (T) JsonBodyParseInterceptor.parseJsonBody(getRawObject(), typeClass, typeClass, jsonKey); 331 } catch (Exception ex) { 332 throw new ActionException(400, RenderManager.me().getRenderFactory().getErrorRender(400), ex.getMessage()); 333 } 334 } 335 336 337 /** 338 * 接收 json 转化为 object 339 * 340 * @param typeDef 泛型的定义类 341 * @param <T> 342 * @return 343 */ 344 @NotAction 345 public <T> T getRawObject(TypeDef<T> typeDef) { 346 return getRawObject(typeDef, null); 347 } 348 349 350 /** 351 * 接收 json 转化为 object 352 * 353 * @param typeDef 泛型的定义类 354 * @param jsonKey 355 * @param <T> 356 * @return 357 */ 358 @NotAction 359 public <T> T getRawObject(TypeDef<T> typeDef, String jsonKey) { 360 try { 361 return (T) JsonBodyParseInterceptor.parseJsonBody(getRawObject(), typeDef.getDefClass(), typeDef.getType(), jsonKey); 362 } catch (Exception ex) { 363 throw new ActionException(400, RenderManager.me().getRenderFactory().getErrorRender(400), ex.getMessage()); 364 } 365 } 366 367 368 /** 369 * 接收 Json 转化为 JsonObject 或者 JsonArray 370 * 371 * @return 372 */ 373 @NotAction 374 public <T> T getJsonBody() { 375 if (rawObject == null && StrUtil.isNotBlank(getRawData())) { 376 rawObject = JSON.parse(getRawData()); 377 } 378 return (T) rawObject; 379 } 380 381 382 /** 383 * 接收 json 转化为 object 384 * 385 * @param typeClass 386 * @param <T> 387 * @return 388 */ 389 @NotAction 390 public <T> T getJsonBody(Class<T> typeClass) { 391 return getJsonBody(typeClass, null); 392 } 393 394 395 /** 396 * 接收 json 转化为 object 397 * 398 * @param typeClass 399 * @param jsonKey 400 * @param <T> 401 * @return 402 */ 403 @NotAction 404 public <T> T getJsonBody(Class<T> typeClass, String jsonKey) { 405 try { 406 return (T) JsonBodyParseInterceptor.parseJsonBody(getJsonBody(), typeClass, typeClass, jsonKey); 407 } catch (Exception ex) { 408 throw new ActionException(400, RenderManager.me().getRenderFactory().getErrorRender(400), ex.getMessage()); 409 } 410 } 411 412 413 /** 414 * 接收 json 转化为 object 415 * 416 * @param typeDef 泛型的定义类 417 * @param <T> 418 * @return 419 */ 420 @NotAction 421 public <T> T getJsonBody(TypeDef<T> typeDef) { 422 return getJsonBody(typeDef, null); 423 } 424 425 426 /** 427 * 接收 json 转化为 object 428 * 429 * @param typeDef 泛型的定义类 430 * @param jsonKey 431 * @param <T> 432 * @return 433 */ 434 @NotAction 435 public <T> T getJsonBody(TypeDef<T> typeDef, String jsonKey) { 436 try { 437 return (T) JsonBodyParseInterceptor.parseJsonBody(getJsonBody(), typeDef.getDefClass(), typeDef.getType(), jsonKey); 438 } catch (Exception ex) { 439 throw new ActionException(400, RenderManager.me().getRenderFactory().getErrorRender(400), ex.getMessage()); 440 } 441 } 442 443 444 /** 445 * BeanGetter 会调用此方法生成 bean,在 Map List Array 下,JFinal 446 * 通过 Injector.injectBean 去实例化的时候会出错,从而无法实现通过 @JsonBody 对 map list array 的注入 447 * 448 * @param beanClass 449 * @param beanName 450 * @param skipConvertError 451 * @param <T> 452 * @return 453 */ 454 @NotAction 455 @Override 456 public <T> T getBean(Class<T> beanClass, String beanName, boolean skipConvertError) { 457 if (Collection.class.isAssignableFrom(beanClass) 458 || Map.class.isAssignableFrom(beanClass) 459 || beanClass.isArray()) { 460 return null; 461 } else { 462 return super.getBean(beanClass, beanName, skipConvertError); 463 } 464 } 465 466 467 468 @NotAction 469 public Map<String, String> getParas() { 470 Map<String, String> map = null; 471 Enumeration<String> names = getParaNames(); 472 if (names != null) { 473 map = new HashMap<>(); 474 while (names.hasMoreElements()) { 475 String name = names.nextElement(); 476 map.put(name, getPara(name)); 477 } 478 } 479 return map; 480 } 481 482 483 @Override 484 @NotAction 485 public String getPara() { 486 return tryToTrim(super.getPara()); 487 } 488 489 490 @Override 491 @NotAction 492 public String getPara(String name) { 493 return tryToTrim(super.getPara(name)); 494 } 495 496 497 @Override 498 @NotAction 499 public String getPara(int index, String defaultValue) { 500 return tryToTrim(super.getPara(index, defaultValue)); 501 } 502 503 504 @Override 505 @NotAction 506 public String getPara(String name, String defaultValue) { 507 return tryToTrim(super.getPara(name, defaultValue)); 508 } 509 510 511 @NotAction 512 public String getTrimPara(String name) { 513 return getPara(name); 514 } 515 516 517 @NotAction 518 public String getTrimPara(int index) { 519 return getPara(index); 520 } 521 522 523 @NotAction 524 private String tryToTrim(String value){ 525 return value != null ? value.trim() : null; 526 } 527 528 529 @NotAction 530 public String getEscapePara(String name) { 531 String value = getTrimPara(name); 532 if (value == null || value.length() == 0) { 533 return null; 534 } 535 return StrUtil.escapeHtml(value); 536 } 537 538 539 @NotAction 540 public String getEscapePara(String name, String defaultValue) { 541 String value = getTrimPara(name); 542 if (value == null || value.length() == 0) { 543 return defaultValue; 544 } 545 return StrUtil.escapeHtml(value); 546 } 547 548 549 @NotAction 550 public String getUnescapePara(String name) { 551 String value = getTrimPara(name); 552 if (value == null || value.length() == 0) { 553 return null; 554 } 555 return StrUtil.unEscapeHtml(value); 556 } 557 558 559 @NotAction 560 public String getUnescapePara(String name, String defaultValue) { 561 String value = getTrimPara(name); 562 if (value == null || value.length() == 0) { 563 return defaultValue; 564 } 565 return StrUtil.unEscapeHtml(value); 566 } 567 568 569 @NotAction 570 public String getOriginalPara(String name) { 571 String value = getOrginalRequest().getParameter(name); 572 if (value == null || value.length() == 0) { 573 return null; 574 } 575 return value; 576 } 577 578 579 @NotAction 580 public HttpServletRequest getOrginalRequest() { 581 HttpServletRequest req = getRequest(); 582 if (req instanceof HttpServletRequestWrapper) { 583 req = getOrginalRequest((HttpServletRequestWrapper) req); 584 } 585 return req; 586 } 587 588 589 private HttpServletRequest getOrginalRequest(HttpServletRequestWrapper wrapper) { 590 HttpServletRequest req = (HttpServletRequest) wrapper.getRequest(); 591 if (req instanceof HttpServletRequestWrapper) { 592 return getOrginalRequest((HttpServletRequestWrapper) req); 593 } 594 return req; 595 } 596 597 598 @NotAction 599 public String getOriginalPara(String name, String defaultValue) { 600 String value = getOriginalPara(name); 601 return value != null ? value : defaultValue; 602 } 603 604 605 private BigInteger toBigInteger(String value, BigInteger defaultValue) { 606 try { 607 if (StrKit.isBlank(value)) { 608 return defaultValue; 609 } 610 value = value.trim(); 611 if (value.startsWith("N") || value.startsWith("n")) { 612 return BigInteger.ZERO.subtract(new BigInteger(value.substring(1))); 613 } 614 return new BigInteger(value); 615 } catch (Exception e) { 616 throw new ActionException(400, RenderManager.me().getRenderFactory().getErrorRender(400), "Can not parse the parameter \"" + value + "\" to BigInteger value."); 617 } 618 } 619 620 /** 621 * Returns the value of a request parameter and convert to BigInteger. 622 * 623 * @return a BigInteger representing the single value of the parameter 624 */ 625 @NotAction 626 public BigInteger getParaToBigInteger() { 627 return toBigInteger(getPara(), null); 628 } 629 630 /** 631 * Returns the value of a request parameter and convert to BigInteger. 632 * 633 * @param name a String specifying the name of the parameter 634 * @return a BigInteger representing the single value of the parameter 635 */ 636 @NotAction 637 public BigInteger getParaToBigInteger(String name) { 638 return toBigInteger(getTrimPara(name), null); 639 } 640 641 /** 642 * Returns the value of a request parameter and convert to BigInteger with a default value if it is null. 643 * 644 * @param name a String specifying the name of the parameter 645 * @return a BigInteger representing the single value of the parameter 646 */ 647 @NotAction 648 public BigInteger getParaToBigInteger(String name, BigInteger defaultValue) { 649 return toBigInteger(getTrimPara(name), defaultValue); 650 } 651 652 653 @NotAction 654 public BigInteger getParaToBigInteger(int index) { 655 return toBigInteger(getTrimPara(index), null); 656 } 657 658 659 @NotAction 660 public BigInteger getParaToBigInteger(int index, BigInteger defaultValue) { 661 return toBigInteger(getTrimPara(index), defaultValue); 662 } 663 664 665 /** 666 * Returns the value of a request parameter and convert to BigInteger. 667 * 668 * @return a BigInteger representing the single value of the parameter 669 */ 670 @NotAction 671 public BigInteger getBigInteger() { 672 return toBigInteger(getPara(), null); 673 } 674 675 /** 676 * Returns the value of a request parameter and convert to BigInteger. 677 * 678 * @param name a String specifying the name of the parameter 679 * @return a BigInteger representing the single value of the parameter 680 */ 681 @NotAction 682 public BigInteger getBigInteger(String name) { 683 return toBigInteger(getTrimPara(name), null); 684 } 685 686 /** 687 * Returns the value of a request parameter and convert to BigInteger with a default value if it is null. 688 * 689 * @param name a String specifying the name of the parameter 690 * @return a BigInteger representing the single value of the parameter 691 */ 692 @NotAction 693 public BigInteger getBigInteger(String name, BigInteger defaultValue) { 694 return toBigInteger(getTrimPara(name), defaultValue); 695 } 696 697 698 @NotAction 699 public BigInteger getBigInteger(int index) { 700 return toBigInteger(getTrimPara(index), null); 701 } 702 703 704 @NotAction 705 public BigInteger getBigInteger(int index, BigInteger defaultValue) { 706 return toBigInteger(getTrimPara(index), defaultValue); 707 } 708 709 710 private BigDecimal toBigDecimal(String value, BigDecimal defaultValue) { 711 try { 712 if (StrKit.isBlank(value)) { 713 return defaultValue; 714 } 715 value = value.trim(); 716 if (value.startsWith("N") || value.startsWith("n")) { 717 return BigDecimal.ZERO.subtract(new BigDecimal(value.substring(1))); 718 } 719 return new BigDecimal(value); 720 } catch (Exception e) { 721 throw new ActionException(400, RenderManager.me().getRenderFactory().getErrorRender(400), "Can not parse the parameter \"" + value + "\" to BigDecimal value."); 722 } 723 } 724 725 726 /** 727 * Returns the value of a request parameter and convert to BigDecimal. 728 * 729 * @return a BigDecimal representing the single value of the parameter 730 */ 731 @NotAction 732 public BigDecimal getParaToBigDecimal() { 733 return toBigDecimal(getPara(), null); 734 } 735 736 737 /** 738 * Returns the value of a request parameter and convert to BigDecimal. 739 * 740 * @param name a String specifying the name of the parameter 741 * @return a BigDecimal representing the single value of the parameter 742 */ 743 @NotAction 744 public BigDecimal getParaToBigDecimal(String name) { 745 return toBigDecimal(getTrimPara(name), null); 746 } 747 748 /** 749 * Returns the value of a request parameter and convert to BigDecimal with a default value if it is null. 750 * 751 * @param name a String specifying the name of the parameter 752 * @return a BigDecimal representing the single value of the parameter 753 */ 754 @NotAction 755 public BigDecimal getParaToBigDecimal(String name, BigDecimal defaultValue) { 756 return toBigDecimal(getTrimPara(name), defaultValue); 757 } 758 759 760 /** 761 * Returns the value of a request parameter and convert to BigDecimal. 762 * 763 * @return a BigDecimal representing the single value of the parameter 764 */ 765 @NotAction 766 public BigDecimal getBigDecimal() { 767 return toBigDecimal(getPara(), null); 768 } 769 770 /** 771 * Returns the value of a request parameter and convert to BigDecimal. 772 * 773 * @param name a String specifying the name of the parameter 774 * @return a BigDecimal representing the single value of the parameter 775 */ 776 @NotAction 777 public BigDecimal getBigDecimal(String name) { 778 return toBigDecimal(getTrimPara(name), null); 779 } 780 781 /** 782 * Returns the value of a request parameter and convert to BigDecimal with a default value if it is null. 783 * 784 * @param name a String specifying the name of the parameter 785 * @return a BigDecimal representing the single value of the parameter 786 */ 787 @NotAction 788 public BigDecimal getBigDecimal(String name, BigDecimal defaultValue) { 789 return toBigDecimal(getTrimPara(name), defaultValue); 790 } 791 792 793 /** 794 * 获取所有 attr 信息 795 * 796 * @return attrs map 797 */ 798 @NotAction 799 public Map<String, Object> getAttrs() { 800 Map<String, Object> attrs = new HashMap<>(); 801 for (Enumeration<String> names = getAttrNames(); names.hasMoreElements(); ) { 802 String attrName = names.nextElement(); 803 attrs.put(attrName, getAttr(attrName)); 804 } 805 return attrs; 806 } 807 808 809 /** 810 * 使用 getFirstFileOnly,否则恶意上传的安全问题 811 * 812 * @return 813 */ 814 @NotAction 815 @Override 816 public UploadFile getFile() { 817 return getFirstFileOnly(); 818 } 819 820 821 /** 822 * 只获取第一个文件,若上传多个文件,则删除其他文件 823 * 824 * @return 825 */ 826 @NotAction 827 public UploadFile getFirstFileOnly() { 828 List<UploadFile> uploadFiles = getFiles(); 829 if (uploadFiles == null || uploadFiles.isEmpty()) { 830 return null; 831 } 832 833 if (uploadFiles.size() == 1) { 834 return uploadFiles.get(0); 835 } 836 837 for (int i = 1; i < uploadFiles.size(); i++) { 838 UploadFile uploadFile = uploadFiles.get(i); 839 FileUtil.delete(uploadFile); 840 } 841 842 return uploadFiles.get(0); 843 } 844 845 846 /** 847 * 值返回特定文件,其他文件则删除 848 * 849 * @param name 850 * @return 851 */ 852 @NotAction 853 public UploadFile getFileOnly(String name) { 854 List<UploadFile> uploadFiles = getFiles(); 855 if (uploadFiles == null || uploadFiles.isEmpty()) { 856 return null; 857 } 858 859 UploadFile ret = null; 860 for (UploadFile uploadFile : uploadFiles) { 861 if (ret == null && name.equals(uploadFile.getParameterName())) { 862 ret = uploadFile; 863 } else { 864 FileUtil.delete(uploadFile); 865 } 866 } 867 868 return ret; 869 } 870 871 872 /** 873 * 只返回特定的名称的文件,其他文件则删除 874 * 875 * @param paraNames 876 * @return 877 */ 878 @NotAction 879 public Map<String, UploadFile> getFilesOnly(String... paraNames) { 880 if (paraNames == null || paraNames.length == 0) { 881 throw new IllegalArgumentException("names can not be null or empty."); 882 } 883 return getFilesOnly(Sets.newHashSet(paraNames)); 884 } 885 886 887 /** 888 * 只返回特定的名称的文件,其他文件则删除 889 * @param paraNames 890 * @return 891 */ 892 @NotAction 893 public Map<String, UploadFile> getFilesOnly(Set<String> paraNames) { 894 if (paraNames == null || paraNames.size() == 0) { 895 throw new IllegalArgumentException("names can not be null or empty."); 896 } 897 898 List<UploadFile> allUploadFiles = getFiles(); 899 if (allUploadFiles == null || allUploadFiles.isEmpty()) { 900 return null; 901 } 902 903 Map<String, UploadFile> filesMap = new HashMap<>(); 904 905 for (UploadFile uploadFile : allUploadFiles) { 906 String parameterName = uploadFile.getParameterName(); 907 if (StrUtil.isBlank(parameterName)) { 908 FileUtil.delete(uploadFile); 909 continue; 910 } 911 912 parameterName = parameterName.trim(); 913 914 if (paraNames.contains(parameterName) && !filesMap.containsKey(parameterName)) { 915 filesMap.put(parameterName, uploadFile); 916 } else { 917 FileUtil.delete(uploadFile); 918 } 919 } 920 921 return filesMap; 922 } 923 924 925 @NotAction 926 public String renderToStringWithAttrs(String template) { 927 return super.renderToString(template, getAttrs()); 928 } 929 930 931 @NotAction 932 public String renderToStringWithAttrs(String template, Map data) { 933 if (data == null) { 934 data = getAttrs(); 935 } else { 936 data = new HashMap(data); 937 for (Enumeration<String> names = getAttrNames(); names.hasMoreElements(); ) { 938 String attrName = names.nextElement(); 939 if (!data.containsKey(attrName)) { 940 data.put(attrName, getAttr(attrName)); 941 } 942 } 943 } 944 945 return super.renderToString(template, data); 946 } 947}