javascript - 使用 Thymeleaf 渲染 Spring ModelAndView 对象

标签 javascript html css spring-mvc thymeleaf

我有一个这样的 Controller :

    @Autowired
    ServletContext servletContext;

    public ModelAndView handleLogin(Locale locale, Model viewModel, HttpServletRequest request)
                    throws Exception {
            [...]
            ModelAndView scheduler = new ModelAndView("scheduler");
            scheduler.addObject("body", SchedulerCreator.getScheduler().render());

            return scheduler;
    }

render() 函数只返回一个带有 html/css/javascript 的字符串,所以我应该将它作为 html 放在页面上。

我试图像这样将它放在我的 Thymeleaf 模板中:

    <html xmlns:th="http://www.thymeleaf.org">
      <head th:include="layout :: htmlhead" th:with="title='DoctorScheduler'"></head>
      <body>
        <div th:replace="layout :: navbar">(navbar)</div>
        <div th:text="${body}" id="scheduler"></div>
        <div th:include="layout :: footer" id="footer">(footer)</div>
      </body>
    </html>

在屏幕上,我可以很好地显示页眉和页脚,但对于调度程序,我只能显示 html 代码,而不会呈现:

    <script src="/javaplanner/dhtmlxscheduler.js" type="text/javascript" charset="utf-8"></script> <script src="/javaplanner/ext/dhtmlxscheduler_url.js" type="text/javascript" charset="utf-8"></script> <script src="/javaplanner/ext/dhtmlxscheduler_pdf.js" type="text/javascript" charset="utf-8"></script> <link rel="stylesheet" href="/javaplanner/dhtmlxscheduler.css" type="text/css" charset="utf-8"> <div id='scheduler_here' class='dhx_cal_container' style='width:900px;height:100%;'> <div class='dhx_cal_navline'> <div class='dhx_cal_export pdf' id='export_pdf' title='Export to PDF' onclick='scheduler.toPDF("http://dhtmlxscheduler.appspot.com/export/pdf", "color")'> </div> <div class='dhx_cal_prev_button'> </div> <div class='dhx_cal_next_button'> </div> <div class='dhx_cal_today_button'></div> <div class='dhx_cal_date'></div> <div class='dhx_cal_tab' name='month_tab' style='left:76px;'></div> <div class='dhx_cal_tab' name='week_tab' style='left:140px;'></div> <div class='dhx_cal_tab' name='day_tab' style='left:204px;'></div> </div> <div class='dhx_cal_header'></div> <div class='dhx_cal_data'></div> </div> <script> scheduler.config.serverLists = {}; scheduler.locale.labels.section_time = 'When'; scheduler.locale.labels.section_appointmentName = 'Description'; scheduler.locale.labels.section_patientId = 'Patient ID'; scheduler.locale.labels.section_appointFor = 'Appoint For'; scheduler.locale.labels.section_staffId = 'Staff ID'; scheduler.locale.labels.section_assistantId = 'Assistant ID'; scheduler.locale.labels.section_departmentWardId = 'Dept. Ward ID'; scheduler.locale.labels.section_appointmentStatusId = 'Appt. Status ID'; scheduler.locale.labels.section_referenceConsultancyId = 'Ref. Consult. ID'; scheduler.locale.labels.section_nextAppointmentId = 'Next. Appt. ID'; scheduler.locale.labels.section_consultancyId = 'Consultancy ID'; scheduler.locale.labels.section_firmCode = 'Firm Code'; scheduler.config.lightbox.sections = [ {"name":"time", "type":"time", "map_to":"auto"}, {"height":40, "name":"appointmentName", "type":"textarea", "map_to":"appointmentName"}, {"name":"patientId", "type":"select", "map_to":"patientId", "options":scheduler.serverList("patientsLookup")}, {"name":"appointFor", "type":"select", "map_to":"appointFor", "options":scheduler.serverList("appointForLookup")}, {"name":"staffId", "type":"select", "map_to":"staffId", "options":scheduler.serverList("staffLookup")}, {"name":"assistantId", "type":"select", "map_to":"assistantId", "options":scheduler.serverList("assistantsLookup")}, {"name":"departmentWardId", "type":"select", "map_to":"departmentWardId", "options":scheduler.serverList("wardLookup")}, {"name":"appointmentStatusId", "type":"select", "map_to":"appointmentStatusId", "options":scheduler.serverList("apptStatusLookup")}, {"name":"referenceConsultancyId", "type":"select", "map_to":"referenceConsultancyId", "options":scheduler.serverList("refConsLookup")}, {"name":"nextAppointmentId", "type":"select", "map_to":"nextAppointmentId", "options":scheduler.serverList("nextApptLookup")}, {"name":"consultancyId", "type":"select", "map_to":"consultancyId", "options":scheduler.serverList("consultantsLookup")}, {"height":40, "name":"firmCode", "type":"textarea", "map_to":"firmCode"} ]; scheduler.config.details_on_dblclick = true; scheduler.config.dblclick_create = true; scheduler.config.scroll_hour = 8; scheduler.config.skin = 'terrace'; scheduler.config.details_on_create = true; scheduler.templates.lightbox_header = function(start,end,ev){ var temp = "";temp += "<strong>Appointment</strong>"; return temp; }; scheduler.init('scheduler_here',new Date(2014, 8, 10, 15, 55, 33)); scheduler.config.prevent_cache = true; scheduler.load("appointment", "json"); var dp = new dataProcessor("appointment"); dp.init(scheduler); dp.setTransactionMode("POST", false);</script>

怎么了?如何将 html/css/javascript 字符串放入我的 Thymeleaf 模板中以使其呈现?我可以忽略任何 Thymeleaf 标签吗?还有其他方法/方法吗? 欢迎任何输入,感谢您的帮助!

最佳答案

您不能使用 th:text 来显示预呈现的 HTML 代码。相反,您需要未转义的文本属性 th:utext

有关详细信息,请参阅文档中的此部分:http://www.thymeleaf.org/doc/usingthymeleaf.html#unescaped-text

关于javascript - 使用 Thymeleaf 渲染 Spring ModelAndView 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25771555/

相关文章:

javascript - NodeJS Passport - 如何打开新窗口进行身份验证?

html - 如何为 iframe 设置自定义视口(viewport)?

html - 突出显示表列组

html - 第二个内容 div 填充剩余高度,其中第一个内容 div 具有可变高度

javascript - 如何在每次输入更改时运行动画?

CSS 问题,在 Internet Explorer 中显示意外的背景重复

javascript - Javascript 回调函数/promise 的混淆

javascript - 如何在 JavaScript 中将服务器端标签显示更改为“阻止”?

javascript - 使用 mouseout DOM 删除 textNode

javascript - 如何在网站上生成图像?