简体   繁体   English

我如何将原始HTML从Java Spring Controller传递到JSP视图

[英]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&quot ; 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.

相关问题 我如何使用spring将对象从jsp传递到控制器 - How do i pass an object from jsp to controller with spring 如何在Spring中将值从控制器传递到JSP页面? - How do I pass a value from a controller to a JSP page in Spring? 如何使用Spring MVC从Java中的控制器而不是通过Web浏览器请求HTML视图? - How Do I Request an HTML View from a Controller in Java and not through a Web Browser using Spring MVC? 如何将复杂对象从控制器传递到视图(jsp) - How can I pass a complex object from controller to the view (jsp) 我可以将数据从Spring引导控制器传递到html页面,但是如何使用Java Script访问数据? - I am able to pass the data from Spring boot controller to html page, but how do I access the data in Java Script? 如何在Spring中将数据从Controller发送到JSP页面? - How do I send data from a Controller to a JSP page in Spring? Spring MVC和JSP:如何将参数从控制器传递给JSP? - Spring MVC & JSP: How to pass a parameter from the controller to JSP? 如何在Spring中将变量从java传递给jsp - how to pass variables from java to jsp in Spring 如何将对象从Spring 3.0控制器传递到JSP视图并使用JSTL进行测试 - How to pass an object from Spring 3.0 controller to JSP view and test with JSTL How do I pass an object with thymeleaf to java spring controller? - How do I pass an object with thymeleaf to java spring controller?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM