-1

我需要使用javascript在jsp中编码一个字符串,并将这个字符串发送到spring控制器并在java中解码这个字符串,目前正在做这样的事情

javascript - 在jsp中

 if(/^.%[a-zA-Z0-9- ]*$/.test(comments) == false) {
       // alert('Your comments contains illegal characters.');
        var encodedComments = encodeURI(comments);
        //alert(encodedComments);
    } else{
        var encodedComments = comments;
        //alert(encodedComments);
        }

在控制器中

String regex = "[a-zA-Z0-9-/.^%$/ ]*";
        boolean m = Pattern.matches(regex, data[6]);
        if (m == true) {
           System.out.println("There is a sp. character in the string");
           try {
            comments = URLDecoder.decode(data[6], "UTF-8");
            logger.info("The decoded string comments============>>>>>>>>>>>"+comments);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        } else {
            System.out.println("There is no sp. char.");
                comments = data[6];
                logger.info("The remarks with no special characters is ============>>>>>>>>>>>"+comments);
        }

例如:我在 textarea 中给出了评论:比如得分 6.08%,编码的字符串正确出现,但解码的字符串不起作用,它只是打印得分 6 而已。如何解码用 javascript 编码的 java 中的字符串?我在这里错过了什么吗?

任何帮助都会很棒。

提前致谢。

4

1 回答 1

0

at javascript side you need always do encoding via encodeURIComponent at java side you need always do decoding, if reading directly from QUERY_STRING, if you're reading from request.getParameter - decoding is not needed at all

于 2013-10-23T03:02:00.870 回答