Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6956123
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T14:49:38+00:00 2026-05-27T14:49:38+00:00

I have a Struts form which contains a Map: private Map<Long, String> questionAnswers =

  • 0

I have a Struts form which contains a Map:

private Map<Long, String> questionAnswers = new TreeMap<Long, String>();

I have the normal getter and setter for this variable (not shown here), and I also have the getter and setter required for Struts to work (using String/Object):

public Object getQuestionAnswer(String questionId) {
    return getQuestionAnswers().get(questionId);
}

public void setQuestionAnswer(String questionId, Object answerText) {
    String answer = (answerText == null) ? "" : answerText.toString();
    getQuestionAnswers().put(Long.valueOf(questionId), answer);
}

In my JSP, I am dynamically generating the textareas that are used to enter the values for the map. This is all working fine. However, when the form is invalid, I need to dynamically generate the textareas again, and put the user text back into the textareas. I am currently repopulating the textareas like so:

<c:forEach items="${myForm.questionAnswers}" var="questionAnswer">
    var textareaBoxName = "questionAnswer(" + '${questionAnswer.key}' + ")";
    var textareaBox = document.getElementsByName(textareaBoxName)[0];
    if (textareaBox) {
        $('textarea[name=' + textareaBoxName + ']').val('${questionAnswer.value}');
    }
</c:forEach>

This works fine, except if you enter a newline in the textarea. Then a JavaScript error complains about an “Unterminated string constant”. I am guessing the newlines are being performed instead of just read.

In the setQuestionAnswer method, I put in some debugging and found that a newline entered in the textarea is being read as 2 characters, which converted into ints are 13 and 10 (which I believe are \r and \n). I tried replacing the the “\r\n” with just “\n” in the setQuestionAnswer method (using the String replaceAll method), but the same error occurred. I then tried replacing the “\r\n” with “%0A” (which I believe is the JavaScript newline). While this got rid of the JavaScript error, the textareas now have the “%0A” displayed instead of a newline. I tried all sorts of escaping and unescaping with no luck (note, I also want special characters to be preserved).

Does anyone have any idea on how to preserve newlines and special characters in the textarea boxes on invalid submits? I need this to work in IE. And I would like to avoid anything hacky like using some special character/string to “represent” a newline which I then replace in JavaScript, etc.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T14:49:38+00:00Added an answer on May 27, 2026 at 2:49 pm

    Since ${questionAnswer.value} is put inside a JavaScript String literal, you need to escape it as you would do if you wanted a newline in a JavaScript literal: the lines Hello and World must be written as ‘Hello\nWorld’. Look at commons-lang StringEscapeUtils escapeECMAScript method. In iddition to escaping the newlines, it will also escape tabs, apostrophes, etc.

    Make this method an EL method, and use it directly into your JSP:

    $('textarea[name=' + textareaBoxName + ']').val('${myFn:escapeJs(questionAnswer.value)}');
    

    You might also generate the text areas statically instead of generating them using JavaScript:

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having an issue with a Struts 1 form, which contains a logic:iterate in
I have a Struts application and I build by form <html:form action=companyProvAdd> ....</html:form> which
I have a form in struts 1.3 which is dynamically generated one and having
I have a struts form that contains many input fields generated using indexed properties.
So in struts I have an action form which has 5 properties. Each property
I have a Struts (1.3x) ActionForm that has several String and boolean properties/fields, but
I have an input in Struts 2 form like this: <s:form action=Action> <input type=text
In a Struts 1.x application, I have a form with a simple single-selection html:select
I have a form with a field that uses a date/time picker which produces
I have a struts application that has a form to edit user details. I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.