如何在JSP上使用撇号或引号(由JavaScript使用) [英] How to escape apostrophe or quotes on a JSP (used by JavaScript)

查看:138
本文介绍了如何在JSP上使用撇号或引号(由JavaScript使用)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用户表单。如果用户在'中键入一个字符串,那么我没有问题,表单被提交并正确保存到数据库中,我的问题是当我重新加载页面(所有条目都可以修改,并在显示之前被加载到JSP中的列表中)。加载页面时,我会收到一条错误:

 缺少)参数列表'Caroline的消息',\\\

我需要做什么来转义这个字符串才能在前端显示?



这是我正在使用的代码在前端阅读数据并将其存储在JavaScript对象中,我不完全确定我需要逃脱的位置,导致问题的字段是c.getComName:

  communication [<%= i%>] [1] = new CommObject('<%= c.getComId()%>','<%= c .getComName()%>'); 

使用HTML生成更新:

  com munications [0] [1] = new CommObject('101','Caroline's Message'); 


解决方案

使用Apache StringEscapeUtils.escapeJavaScript 函数。


 使用JavaScript字符串规则解除字符串中的字符。 

将其找到的任何值转义为JavaScript字符串形式。
正确地引用了引号和控件字符(tab,反斜杠,cr,ff等)

所以一个选项卡变成字符'\\'和't'。



I have a user form. If the user types in a string with ' or " as part of it I have no problem. The form is submitted and saved correctly to the database. My problem is when I reload the page (all entries can be modified and are loaded into a list in the JSP before being displayed). On loading the page I get an error saying:

missing ) after argument list 'Caroline's message', \n

What do I need to do to escape this string for displaying it on the frontend?

Here is the code I am using on the frontend to read in the data and store it in a JavaScript object. I am not fully sure where I need to escape. The field causing the problem is c.getComName:

communications[<%=i%>][1] = new CommObject('<%=c.getComId()%>', '<%=c.getComName()%>');

UPDATED WITH HTML GENERATED:

communications[0][1] = new CommObject('101', 'Caroline's Message');

解决方案

Use the Apache StringEscapeUtils.escapeJavaScript function.

Escapes the characters in a String using JavaScript String rules.

Escapes any values it finds into their JavaScript String form.
Deals correctly with quotes and control-chars (tab, backslash, cr, ff, etc.)

So a tab becomes the characters '\\' and 't'.

这篇关于如何在JSP上使用撇号或引号(由JavaScript使用)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆