0

I have a staff record here and I need to let if go through a function and the function spits out the staff record nicely formatted with HTML tags.

example

staff id = 23422
first name = Herbert
surname = Dominos
department = Purchasing

function output would be something like

<label>staff id</label><div class="some class">23422</div>
<label>First namelabel><div class="some class">Herbert</div>
<label>Surname</label><div class="some class">Dominos</div>
<label>Department</label><div class="some class">Purchasing</div>

Seems just concatenating them via string or stringbuilder is not the right way to do it.

Any suggestions? thanks :)

10
  • What is the problem with concatenating strings. I don't see any real problem in it. If the problem is the quotes in 'class="some class"' you can simply escape them by typing \". Commented Dec 12, 2012 at 1:03
  • 1
    "concatenating them via string or stringbuilder" If you mean String & StringBuilder then definitely no to the first. As to whether it is best to use StringBuilder over many of the other ways of generating HTML, e.g. JSP or servlets, jsoup.. - that is another matter. I 'hand role' methods to do simple HTML structures like the cells and rows of tables. Commented Dec 12, 2012 at 1:04
  • If a lot of output remains the same and there are few places which will be filled in by Java ... a good alternative and more sophisticated way is to use "FreeMarker" ... for just a usecase you describe ... it may be an overkill Commented Dec 12, 2012 at 1:07
  • A good case against hand coding the strings can be seen in <label>First namelabel> it should be <label>First name</label> ;) Commented Dec 12, 2012 at 1:10
  • 1
    @AndrewThompson incorrect, after java 6 it doesn't matter which you use. Commented Dec 12, 2012 at 1:12

3 Answers 3

1

Maybe the Java Anti-Template Language (JATL) would work for you. It's coded using fluent API syntax.

Sign up to request clarification or add additional context in comments.

Comments

0

i would use apache velocity to achieve this.

example:

#foreach($staffInfo in $staffList.iterator())
<label>$staffInfo.getStaffId()</label>
#end

you can try something similar to construct your html code.

2 Comments

Yeah, it appears that Texen might do it. Which of the velocity sub-projects are you referring to?
Yes Texen will help you out here. I usually use the engine itself. But Texen will be all that is required in your case.
0

I would do something like this

<label>staff id </label><div class="some class">#staff id#</div>
<label>First namelabel><div class="some class">#First namelabel#</div>
<label>Surname</label><div class="some class">#Surname#</div>
<label>Department</label><div class="some class">#Department#</div>

and then I would use the replace function to replace for excample #Staff id# with 23422.

Quick, dirty, easy.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.