${actionB...">

In JSP, how can I dynamically set contentType?

I have a very simple JSP that looks like this:

<%@ page contentType="application/json" %>${actionBean.response} 

actionBean.response returns a String . Sometimes this line is json, which has a contentType for "application / json", but sometimes this jsonp line is of type contentType "application / javascript". But I can’t figure out how to dynamically set the contentType value.

  • I tried using <c:choose> around contentType, but it gives me a message stating that I cannot set contentType twice.
  • I tried using EL for the attribute value, but it does not expand.

Is there a way to dynamically set this value?

+4
source share
2 answers

You can try using scripts (not perfect, but I'm not sure there is another way), for example:

 <% if (actionBean.isJson()) { response.setContentType("application/json"); } else if (actionBean.isJsonp()) { response.setContentType("application/javascript"); } %> 

Edit: And since Joop mentions in the comments, make sure you don't set contentType using the @page directive.

+4
source

I do not think jsp is for this. You would capture the JSON response in a javascript function without having to declare a content type.

I also never saw any jsp page turn into a javascript file.

You can use <s:property name="something" escapeHtml="false" escapeJavascript="false"/> for both.

0
source

Source: https://habr.com/ru/post/1492363/


All Articles