【问题标题】:Why is my browser downloading file instead of rendering SpringBoot & Sitemesh output?为什么我的浏览器下载文件而不是渲染 SpringBoot 和 Sitemesh 输出?
【发布时间】:2017-03-21 01:26:17
【问题描述】:

我正在尝试将 SpringBoot 与 Freemarker 和 Sitemesh 一起使用。

当我转到一个 URL 时,请求由应用程序处理,数据加载并生成 HTML 输出,但由于某种原因,浏览器决定它想要下载文件(包含正确的内容)而不是将其呈现为页面。

这在一段时间前还有效,问题是我不确定我所做的哪个更改破坏了它!

Sitemesh 过滤器:

@WebFilter
public class SitemeshFilter extends ConfigurableSiteMeshFilter {

private static final Logger LOG = Logger.getLogger(SitemeshFilter.class);

    @Override
    protected void applyCustomConfiguration(SiteMeshFilterBuilder builder) {
        LOG.debug("SiteMeshFilter creation");
        builder.addDecoratorPath("/*", "/templates/main.ftl")
            .addExcludedPath("/h2console/*");
    }
}

应用:

@ServletComponentScan
@SpringBootApplication
@EnableAutoConfiguration(exclude = {ErrorMvcAutoConfiguration.class})
public class ClubManagementApplication {

    private static Logger LOG = Logger.getLogger(ClubManagementApplication.class);

    public static void main(String[] args) {
        SpringApplication.run(ClubManagementApplication.class, args);
    }
}

控制器片段:

@Controller
public class ClubController {

    @Autowired
    ClubService clubService;

    @RequestMapping(value = {"Club/{id}","club/{id}"})
    public ModelAndView viewClub(@PathVariable("id") int clubId) {
        ModelAndView mv = new ModelAndView("club");
        ....
        return mv;
    }
}

编辑: 从控制器中的 HttpServletRequest 对象... 接受:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,/;q=0.8

在响应标头中: 内容类型:application/octet-stream;charset=UTF-8

我猜内容类型是问题....只是要找出为什么会这样设置。

【问题讨论】:

  • 你收到的http header的Content-Type值是多少?
  • 你说的很对 @OrtomalaLokni 内容类型不正确,只是要尝试找出原因
  • 可以在github下提供mcve吗?
  • 这可能很棘手,我会看看我能做什么但不能保证

标签: spring-boot sitemesh


【解决方案1】:

万一其他人偶然发现了这个问题,我将我的模板文件从 ftl 更改为 html 扩展名,然后它突然醒了。

@Override
protected void applyCustomConfiguration(SiteMeshFilterBuilder builder)   {
    LOG.debug("SiteMeshFilter creation");
    //builder.addDecoratorPath("/*", "/templates/main.ftl");
    builder.addDecoratorPath("/*", "/templates/main.html");
}

【讨论】:

    猜你喜欢
    • 2015-08-04
    • 2012-08-05
    • 2015-10-22
    • 2021-07-31
    • 1970-01-01
    • 1970-01-01
    • 2016-12-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多