使用Thymeleaf渲染Spring ModelAndView对象

时间:2014-09-10 17:29:29

标签: javascript html css spring-mvc thymeleaf

我有一个像这样的控制器:

    @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>

出了什么问题?如何在我的Thymeleaf模板中放置html / css / javascript字符串以使其呈现?我可能会忽略任何Thymeleaf标签?任何其他方法/方法? 欢迎任何意见,谢谢你的帮助!

1 个答案:

答案 0 :(得分:0)

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

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