Skip to content

Cannot set a Content-Type using ScriptTemplateView [SPR-13379] #17961

@spring-projects-issues

Description

@spring-projects-issues

Kazuki Shimizu opened SPR-13379 and commented

If ScriptTemplateView is used as View, response data display as plain text in Web Browser because it cannot set a Content-Type header(such as "text/html").

I tried it using following implementations and configuration.

@Controller
@RequestMapping("script-template")
public class ScriptTemplateController {

    private static final Logger logger = LoggerFactory
            .getLogger(ScriptTemplateController.class);

    @RequestMapping(method = RequestMethod.GET)
    public String home(Locale locale, Model model,HttpServletResponse response) {
        logger.info("Welcome home! The client locale is {}.", locale);

        DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG,
                DateFormat.LONG, locale);

        model.addAttribute("title", "Sample title")
                .addAttribute("body", "Sample body")
                .addAttribute("serverTime", dateFormat.format(new Date()));

        return "script-template/home";
    }

}
<html>
<head>
    <title>`title`</title>
</head>
<body>
<p>`body`</p>

<p>The time on the server is `serverTime`.</p>
</body>
</html>
<mvc:view-resolvers>
    <mvc:script-template prefix="META-INF/views/" suffix=".html" />
</mvc:view-resolvers>

<mvc:script-template-configurer engine-name="nashorn" render-object="Mustache" render-function="render">
    <mvc:script location="classpath:META-INF/resources/vendor/mustache/mustache.js"/>
</mvc:script-template-configurer>

I access to "http://localhost:8080/script-template/". However response data does not render as HTML. it render as plain text.

<html>
<head>
    <title>Sample title</title>
</head>
<body>
<p>Sample body</p>

<p>The time on the server is August 23, 2015 4:58:33 AM JST.</p>
</body>
</html>

I expect to display as follows in Web Browser.

Sample body

The time on the server is August 23, 2015 5:20:12 AM JST.

I tried following other solution. However, it cannot resolved.

<mvc:view-resolvers>
    <mvc:bean-name />
</mvc:view-resolvers>

<bean id="script-template/home" class="org.springframework.web.servlet.view.script.ScriptTemplateView">
    <property name="url" value="META-INF/views/script-template/home.html"/>
    <property name="contentType" value="text/html"/>
</bean>

The contentType property is not used in ScriptTemplateView.

I think it should be fix as can be specify a content-type.
How do think ?


Affects: 4.2 GA

Referenced from: commits 04cff89

Metadata

Metadata

Assignees

Labels

in: webIssues in web modules (web, webmvc, webflux, websocket)type: bugA general bug

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions