• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Saving newline to database

 
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am using the struts framework in my project.
I have a just like this (Add new topic of javaranch) page.
What i do is save text in the subject and message field to my database as strings.
i get the value of message property as

and then save it to database where Message is a data field like this
Every thing is fine till this point. Problem is when i try to display this message on a different page
i get the message from database as

saving it to request

and finally display it in my JSP page using EL

it displays the message but without any new lines i.e the message appears as a single line not like multilines the way it was actually typed.
My Question is how do we preserve the formatting of a message
like , new lines or may be font properties like color , weight etc?

Thanks for reading this.
-Prakash
 
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In order to preserve carriage returns, the wrap attribute of the textarea must be set to either OFF or PHYSICAL. See this link for more details.

Unfortunately, the Struts html:textarea tag does not pass this attribute on to the rendered tag. This means that you will have to use the regular HTML <textarea> tag with a bean:write tag to get the current value from the form bean. Example:

[ March 20, 2008: Message edited by: Merrill Higginson ]
 
prakash chauhan
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the reply Merrill
I tried using TEXTAREA tag but still when i pull up the message from the database it shows one long string only.
Dont know if its relevent , if i look at the source of my jsp page that shows the message( using View Source option of browser) it does show me the message in seperate lines so to me it seems like the lines delimiters are there in database.

thnks for the help.
 
Merrill Higginson
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I suspect, then, that the problem lies in how you are storing the contents of the text area to the database. You may want to look at the documentation for your particular database to see how it handles carriage returns in columns. You might also try a different data type. For example, in Oracle, VARCHAR2 is different than VARCHAR.
 
prakash chauhan
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am using MySql as my DBMS. i did some google and came through an article that says ,'retrieve the String from the database and then convert the new line characters to the BR tag of html so that a BR would be printed when a new line character is encountered, i am not sure as i was not able get that far.
But lets see , i will try to get it working this weekend.
Thanks for your help again.
 
prakash chauhan
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well i still am facing the problem:
What i tried was

and it still doesnt recognize the new lines
however if i do

it prints the text with newline (seperate lines) as expected.
so i know that the database is holding the new line character.
but the browser is not interpreting them
any help?

Thanks in advance.
 
Merrill Higginson
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
About the only thing I can think of left to try is to implement a process in your getMessage method that looks for newline characters and replaces them with "<br>".
 
prakash chauhan
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
this is the work around which worked for me


i wanted to avoid using scriplets , any suggestions?

-Thanks in advance
 
Merrill Higginson
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just put the replacement logic in your getMessage method. Then you can just use a bean:write or EL expression in the JSP. Example:
<bean:write name="TEAMUPDATE" property="message" filter="false" />
 
prakash chauhan
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Thanks
Yes i tried and it worked , but i was thinking what if the output is not read by a browser but by some other program like the cmd console..
It's working now but will again need change when display tool is changed.
 
Merrill Higginson
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you expect the getMessage method to be used in other contexts, then just have it show the plain text and have another method: getHtmlMessage() or something like that displaying the message with the new line characters replaced with "<br>".
 
prakash chauhan
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Thanks..
yeah thats what i guess i ll have to do, have two methods for two types of requesting output consoles.
Thanks for all your help.
 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi all,
i want to display the new line while displaying message. I tried replacing "\n" character with BR(tag- i have mentioned br tag , this way otherwise while displaying my request the tag is not getting displayed..in my jsp page i have used the correct format of br tag) text but in my case what is happening is that in the display page the message is getting displayed wid "BR" text....
i mean if the test is "hello
how
are
you?"
its getting displayed as hello"<"BR">"how"<"BR">"are"<"BR">"you
please help
 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please start new threads instead of using threads that are almost a year old.

Does JSP EL escape HTML? In any case, this isn't a Struts issue.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic