0

mysql テーブルに longtext として保存するテキスト ブロックがあり、jsp の struts bean:define を使用して取得します。このテキスト ブロックを取得して jquery UI ダイアログで使用したいのですが、これはテキストを "\n" 文字で解析する必要があることを意味します。現在、テキストは次のように出てきます

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.";

mysql テーブルから出力されるデータの "\n"+ 文字を取得するにはどうすればよいですか?

この部分を参照するコードの一部を次に示します (関連のない部分を削除しました)。

<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>
4

1 に答える 1

0

解決策を見つけました。もっと簡単な方法があるかもしれませんが、私がやったことは次のとおりです。

1) データベースのデータのすべての /r/n を %nl に置き換える Java 関数を作成しました。JSP ページで使用するクエリ結果を保存するときに、結果セットの各行でこれを呼び出します。

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

2) 次に、jsp ページで bean:define データを取得し、それを一時変数に保存してから、すべての %nl をグローバルに \n に置き換える str.replace 関数を実行しました。

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

これにより、テキストがjquery uiダイアログのテキスト領域に正しく表示されました。

于 2013-02-27T16:14:01.393 に答える