2013-02-26 97 views
0

我有一块文本存储在mysql表格中作为longtext,然后使用struts bean进行检索:define for jsp。我想要检索这个文本块在jQuery UI对话框中使用,这意味着文本必须用“\ n”字符分析。目前文本出来像将文本从mysql传递到JSP页面上的javascript

emailMSG[1] = "%fn %ln, 

Our records indicate that certification in %gn, %cn, %sn expire(d) on %dt. 

Please take the refresher course. 

Please visit our portal to log into your training. 

Please ignore any messages from CITI regarding course expiration. DO NOT log in directly to the CITI site."; 

我需要的数据是这种格式:

emailMSG[<%=id%>] = "%fn %ln,\n"+ 
"\n" + 
"Our records indicate that certification in %gn, %cn, %sn expire(d) on %dt.\n"+ 
"\n" + 
"Please take the refresher course at the training site.\n"+ 
"\n" + 
"Please visit our portal to log into the site.\n"+ 
"\n"+ 
"Please ignore any messages from CITI regarding course expiration. DO NOT log in directly to the CITI site."; 

我怎样才能获得“\ n” +数据中的字符出来mysql表的?

这里是我的代码位引用该部分(我删除了不相关的碎片):

<logic:iterate id="cel" name="CourseEmailList" scope="request"> 
    <bean:define id="msg" property="message" name="cel" scope="page" type="java.lang.String"/> 
    <bean:define id="id" property="id" name="cel" scope="page" type="java.lang.String"/> 
<tr> 
     <logic:notEmpty name="cel" property="message" scope="page"> 
      <td><a href="#" onclick="OpenDialog(<bean:write name='cel' property='id' scope="page"/>);return false;">View/Modify</a> 
      <script type="text/javascript"> 
       courseIDs[<%=id%>] = "<%= id%>"; 
       emailMSG[<%=id%>] = "<%= msg%>"; 
      </script> 
      </td> 
     </logic:notEmpty> 
     </tr> 
</logic:iterate> 

回答

0

我找到了解决办法。可能有一个更简单的方法来做到这一点,但这是我做的:

1)我写了一个java函数,用%nl替换数据库中数据的所有/ r/n。当我存储用于jsp页面的查询结果时,我在结果集中的每行上调用它。

public static String formatMessage(String comment) { 
     if(comment != null){ 
      return comment.replaceAll("\r\n", "%nl"); 
     }else{ 
      return ""; 
     } 
    } 

2)然后,我的JSP页面上我把豆:定义数据并将其保存到一个临时变量,然后做了一个str.replace函数\ n globably更换全部%NL。

<script type="text/javascript"> 
     var temp = "<%= msg%>"; 
     var parsed = temp.replace(/%nl/g,'\n'); 
     emailMSG[<%=id%>] = parsed.valueOf(); 
</script> 

这使我的文本在jquery ui对话框文本区域中正确显示。