[英]How do i pass raw HTML from java spring controller to jsp view
Im just trying something for feasibility purposes. 我只是出于可行性目的尝试一些东西。
I am trying to pass raw HTML code in my Spring controller to a specific view. 我试图将Spring控制器中的原始HTML代码传递给特定视图。 However i want to know if its possible to convert raw html to a string and then pass the HTML string to the view.
但是我想知道是否有可能将原始html转换为字符串,然后将HTML字符串传递给视图。 Theres errors in "String message" because i havented formatted the HTML into a string.
“ String message”中存在错误,因为我尚未将HTML格式化为字符串。
@Controller
public class TestController {
@RequestMapping(value = "/test", produces = "text/html;charset=UTF-8")
public ModelAndView test (ModelMap model) {
//Map<String, Object> cat = (Map<String, Object>) incidentService.getIncidentCategories();
ModelAndView mv = new ModelAndView("test2.jsp");
String message = <p><img alt="alt text" src="http://localhost:8080/aston.jpg" style="height:974px; width:2386px" />This is my textarea to be replaced with CKEditor.</p> //
return new ModelAndView("welcome", "message", message);
return mv;
}
}
You can use use @RestController
or @ResponseBody
, which will not go thru viewResolver
您可以使用use
@RestController
或@ResponseBody
,它们不会通过viewResolver
@RestController
public class TestController {
@RequestMapping(value = "/test")
public String text1() {
return "<p>hello world</p>";
}
}
OR 要么
@Controller
public class TestController {
@RequestMapping(value = "/test")
public @ResponseBody String text2() {
return "<p>hello world again!</p>";
}
}
There are 2 ways to return HTML from Spring controller. 有两种方法可以从Spring控制器返回HTML。
If you are using latest version of Spring.Use " produces " attribute from @RequestMapping 如果您使用的是Spring的最新版本,请使用@RequestMapping中的“ 产生 ”属性
@RequestMapping(value = { "/testUrl" }, method = RequestMethod.GET,
produces = MediaType.TEXT_HTML_VALUE)
if you are using older versions of Spring, simple 如果您使用的是较旧版本的Spring,那么简单
response.setContentType("text/html");
response.getWriter().println(...) // print your html here
So i basically come to this solution. 所以我基本上来解决这个问题。 In spring controller, i am passing encoded escaped html to the view.
在spring控制器中,我将编码的转义html传递给视图。
mv.addObject("Description", HtmlUtils.htmlEscape(eknow.get(0).getDescription())); mv.addObject(“ Description”,HtmlUtils.htmlEscape(eknow.get(0).getDescription()));
unescaped:" 未转义:”
testing 12345 测试12345
escaped:<p>testing 12345</p> 转义:<p>测试12345 </ p>
<p><img alt="alt text" src=" http://localhost:8080/logo.gif" ; style="height:303px; <p> <img alt =“替代文字” src =“ http:// localhost:8080 / logo.gif&quot ; style =” height:303px; width:1191px" /></p>
宽度:1191px“ /> </ p>
And then on the view i store the escaped value in a hidden input. 然后在视图上,将转义的值存储在隐藏的输入中。
And then i get the element by jquery and reverse the action from escaped to unescaped. 然后我通过jquery获取元素并将操作从转义转换为未转义。
var testing = $("#test").val(); alert(decodeURIComponent(testing)); CKEDITOR.instances.description.setData(decodeURIComponent(testing));
This will then set and render html raw code for my HTML editor. 然后,这将为我的HTML编辑器设置并呈现html原始代码。
If you know of any way to optimize this, please let me know. 如果您知道有任何优化方法,请告诉我。 But this solution works.
但是此解决方案有效。
You can use <div class="well"><c:out value="${message}" escapeXml="false"></c:out></div>
您可以使用
<div class="well"><c:out value="${message}" escapeXml="false"></c:out></div>
you also need to escape quotes in ur message String message = "<p><img alt=\\"alt text\\" src=\\"http://localhost:8080/aston.jpg\\" style=\\"height:974px; width:2386px\\" />This is my textarea to be replaced with CKEditor.</p>"
您还需要在ur消息中对引号进行转义
String message = "<p><img alt=\\"alt text\\" src=\\"http://localhost:8080/aston.jpg\\" style=\\"height:974px; width:2386px\\" />This is my textarea to be replaced with CKEditor.</p>"
String message = "<p><img alt=\\"alt text\\" src=\\"http://localhost:8080/aston.jpg\\" style=\\"height:974px; width:2386px\\" />This is my textarea to be replaced with CKEditor.</p>"
add the <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
to your jsp page. 将
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
到您的jsp页面。
assuming that ur using jsp page. 假设您使用jsp页面。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.