在不刷新页面的情况下显示grails中的错误

时间:2012-01-19 15:02:57

标签: grails groovy

我有一个包含动态列表框的页面(从第一个列表中选择值会填充第二个列表框中的值)。

列表框的验证错误正常,但在显示错误消息时,页面正在刷新,所选值已设置为初始状态(需要在列表框中再次选择值)

该页面旨在使用ajax调用添加任意数量的列表框,因此再次添加和选择值将是返工。

你能帮助我显示验证错误并保持所选的值不变(以前我遇到过类似的情况,通过用全局变量替换预处理和后处理的局部变量来解决,这次这个方法没有运气)

任何提示/帮助都会很棒

static constraints = {
        deviceMapping(
                validator: {val, obj ->
                    Properties dm = (Properties) val;
                    def deviceCheck = [:];
                    if (obj.customErrorMessage == null) {
                        for (def device : dm) {
                            if (device.key == null || "null".equalsIgnoreCase(device.key)) {
                                return ["notSelected"];
                            }
                            deviceCheck.put(device.key, "");
                        }

                        if (deviceCheck.size() != obj.properties["numberOfDevices"]) {
                            return ["multipleDevicesError"];
                        }
                    }
                }
        )
        customErrorMessage (
                validator: {
                    if ("sameDeviceMultipleTimes".equals(it)) {
                        return ['sameDeviceMultipleTimes']
                    }

                }
        )
    }
    public LinkedHashMap<String, Object> preProcess(sessionObject, params, request) {
        Submission submission = (Submission) sessionObject;
        def selectedFileName = sessionObject.fileName;
        logger.debug("submission.deviceMapping :"+submission.deviceMapping)
        try {
            Customer customer = Customer.get(submission.customerId);
            OperatingSystem operatingSystem = OperatingSystem.get(submission.operatingSystemId)
            def ftpClientService = new FtpClientService();
            def files = ftpClientService.listFilesInZip(customer.ftpUser, customer.ftpPassword, customer.ftpHost, customer.ftpToPackageDirectory, selectedFileName, operatingSystem, customer.ftpCustomerTempDirectory);
            def terminalService = new TerminalService();

            OperatingSystem os = OperatingSystem.get(submission.getOperatingSystemId());
            def manufacturers = terminalService.getAllDeviceManufacturersForType(os.getType());
            logger.debug("manufacturers after os type :"+manufacturers)
            logger.debug("files in preprocess :"+files)

            def devicesForFiles = [:]
            files.each { file ->
               def devicesForThisFile = [];
               submission.deviceMapping.each { device ->
                  if (device.value == file.fileName) {
                      String manufacturer = terminalService.getManufacturerFromDevice("${device.key}");
                      def devicesForManufacturer = terminalService.getDevicesForManufacturerAndType(manufacturer, os.getType());

                     devicesForThisFile.push([device:device.key, manufacturer: manufacturer, devicesForManufacturer: devicesForManufacturer]);
                  }
               }
               devicesForFiles.put(file.fileName,devicesForThisFile);
            }
            logger.debug("devicesForFiles :"+devicesForFiles)
            return [command: this, devicesForFiles: devicesForFiles, files: files, manufacturers: manufacturers];
        } catch (Exception e) {
            logger.warn("FTP threw exception");
            logger.error("Exception", e);
            this.errors.reject("mapGameToDeviceCommand.ftp.connectionTimeOut","A temporary FTP error occurred");
            return [command: this];
        }
    }
    public LinkedHashMap<String, Object> postProcess(sessionObject, params, request) {
        Submission submission = (Submission) sessionObject;
        Properties devices = params.devices;
        Properties files = params.files;

        mapping = devices.inject( [:] ) { map, dev ->
          // Get the first part of the version (up to the first dot)
          def v = dev.key.split( /\./ )[ 0 ]
          map << [ (dev.value): files[ v ] ]

        }
        deviceMapping = new Properties();

        params.files.eachWithIndex { file, i ->
            def device = devices["${file.key}"];
            if (deviceMapping.containsKey("${device}")) {
                this.errors.reject("You cannot use the same device more than once");
                return [];
                //customErrorMessage = "sameDeviceMultipleTimes";
            }
            deviceMapping.put("${device}", "${file.value}");
        }

        if (params.devices != null) {
            this.numberOfDevices = params.devices.size(); //Used for the custom validator later on
        } else {
            this.numberOfDevices = 0;
        }
        //logger.debug("device mapping :"+deviceMapping);
        submission.deviceMapping = mapping;

        return [command: this, deviceMapping: mapping, devicesForFiles: devicesForFiles ];
    }
}

1 个答案:

答案 0 :(得分:0)

问题出在您的gsp页面中。确保使用值

初始化所有字段
<g:text value="${objectInstance.fieldname}" ... />

它选择值的方式也是通过id,所以一定要设置它:

<g:text value="${objectInstance.fieldname}" id=${device.manufacturer.id} ... />