0

I'm currently struggling with the following problem. In my Spring web application I have different content types (e.g. text, images or code). Depending on the content type I need to display it in different ways:

text: <p>some text</p>

image: <img src="path/to/my.img" />

code <pre>some code</pre>

The HTML tags should be concatened to the actual content. The problem is, if I simply build the output text in my Java class, the HTML tags won't be resolved in my view, so that <p>some text</p> will be displayed.

Is it somehow possible that the HTML tags can be resolved in my view?

2
  • There is no such thing as "resolving HTML tags" (at least not in this context). Do you mean "I want to resolve EL in my strings"? Or do you mean "help, my tags get escaped"? Achieving something is hard if you don't even know what it is. Commented Apr 9, 2013 at 8:12
  • I guess "help, my tags get escaped" is what I mean then. Commented Apr 9, 2013 at 8:18

2 Answers 2

1

If it's just the escaping part that is the problem, use:

<c:out value="${model.snippets.html12}" escapeXml="false" />

(I am assuming your HTML string is in the model.snippets.html12).

Of course, the whole idea is bad. I am not affiliated with the MVC Police, but what is the point of using a MVC framework if you feel that it's a good idea to generate HTML inside your controller and pass it, as a string - into a view? From my point of view it's a bit of a schizophrenia.

You can save a lot of sanity by just rendering the whole thing in a switch, inside the template. I mean like:

  <c:choose>

  <c:when test="${thing.type == 'CODE'}">
     <div> some code: ${thing.content} </div>
  </c:when>

  <c:when test="${thing.type == 'IMAGE'}">
     <img src="${thing.src}" alt="${thing.whatever}" />
  </c:when>


  <!-- some other choices -->


  </c:choose>

Even better, create a simple tag file that will let you reuse the logic anywhere you need it.

Or ditch MVC - be honest.

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

Comments

1

If you do have jquery, set the content type in your model. Set it to the HTML.

<input type = hidden id = contentType value = "${yourmodel.contentType}"

Add span to your elements

<span id = "textspan" style = "display:none"><p>some text</p></span>
<span id = "imgspan><img src="path/to/my.img" /></span>
<span id = "codespan><pre>some code</pre></span>

write a jquery

  if($("contentType").val() == text){
      $("#textspan").show();
    }else if($("contentType").val() == img){
   $("#imgspan").show();
    }else{
     $("#codespan").show();
    }

2 Comments

Thank you! But that would only be a workaround, is there maybe a solution which is directly provided by Spring MVC?
I thought you were looking for a logic for the same .. :-) .. You dont need Spring MVC to do this I believe. You can use c:out with escapeXML. Please check this java67.blogspot.com/2012/10/…. Hope this helps.

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.