简体   繁体   English

使用java创建JS文件时出错

[英]Error while creating JS file using java

I am writing a Java code through which I am creating a JavaScript file. 我正在编写一个Java代码,通过它我创建一个JavaScript文件。 In script file, I want to write 在脚本文件中,我想写

var alertData = "Following characters are not allowed for value. [ $ & + , / : ; =? @ \" < > #    % { } | \\ ~ ^ [ ] ` ]";

When I compile code and call method to create a file, 当我编译代码并调用方法来创建文件时,

Syntax Error: syntax error: > is not valid.

Can someone help? 有人可以帮忙吗?

More code 更多代码

StringBuilder sb = new StringBuilder(100);
sb.append("var validationAlertForValues = \"");
sb.append(("Following characters are not allowed for value. [ $ & + , / : ; =? @ \" < > # % { } | \\ ~ ^ [ ] ` ]"));
sb.append("\";\n    var validationAlertForKeys = \"");
sb.append(("Following characters are not allowed for key name.  [ $ & + , / : ; =? @ \" # % { } | ~ ^ [ ] ` &lt;space&lt; ] "));

 FileWriter fstream = null;
 BufferedWriter out = null;
 try {
   fstream = new FileWriter(filePath);
   out = new BufferedWriter(fstream);
   out.write(sb.toString());
        } catch (Exception e) {
            log.error("Exception in making JS", e);
        }

Use this Apache CommonLang StringEscapeUtils. 使用此Apache CommonLang StringEscapeUtils。

There are classes there to escape different type of input. 那里有类可以逃避不同类型的输入。 Here is a sample i found. 这是我发现的一个例子。

import org.apache.commons.lang.StringEscapeUtils;

public class MainClass {
    public static void main(String[] args) {
        String strHTMLInput = "<P>MyName<P>";
        String strEscapeHTML = StringEscapeUtils.escapeHtml(strHTMLInput);
        String strUnEscapeHTML = StringEscapeUtils.unescapeHtml(strEscapeHTML);
        System.out.println("Escaped HTML >>> " + strEscapeHTML);
        System.out.println("UnEscaped HTML >>> " + strUnEscapeHTML);
    }
}

你使用了\\"哪个是转义符号。试试用\\\\"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM