第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

用@Configuration 和@Controller 注釋一個(gè)類(lèi)。在重構(gòu)方面需要幫助

用@Configuration 和@Controller 注釋一個(gè)類(lèi)。在重構(gòu)方面需要幫助

瀟湘沐 2021-06-09 16:06:34
下面是我的課中,我不得不同時(shí)使用@Configuration,并@Controller作為應(yīng)該只有一個(gè)實(shí)例Thymeleaf在整個(gè)應(yīng)用程序一樣,我得到的例外。我的其他類(lèi)被注釋?zhuān)珸RequestScope所以我不能使用單例作用域 bean。所以我混合了配置和控制器來(lái)得到結(jié)果,但我覺(jué)得這是一個(gè)不好的做法。我將不勝感激任何重構(gòu)代碼和消除不良做法的幫助。更新我正在使用spring-boot 1.5.14. 我使用以下方法來(lái)處理模板并將處理后的模板保留為字符串。@Controller@Configuration@EnableWebMvc@ApplicationScopepublic class MyThymeleafConfig {    @GetMapping("/view-template")    @ResponseBody    public void viewTemplates() {        Context context = new Context();        context.setVariable("mydata", "this is it");        String html = templateEngine().process("templates/view-to-process.html", context);        System.out.println(html);    }    /*    configuration for thymeleaf and template processing    */    @Bean    public SpringTemplateEngine templateEngine() {        SpringTemplateEngine templateEngine = new SpringTemplateEngine();        templateEngine.setTemplateResolver(thymeleafTemplateResolver());        return templateEngine;    }    @Bean    public SpringResourceTemplateResolver thymeleafTemplateResolver() {        SpringResourceTemplateResolver templateResolver = new SpringResourceTemplateResolver();        templateResolver.setPrefix("classpath:");        templateResolver.setSuffix(".html");        templateResolver.setCacheable(false);        templateResolver.setTemplateMode(TemplateMode.HTML);        return templateResolver;    }    @Bean    public ThymeleafViewResolver thymeleafViewResolver() {        ThymeleafViewResolver viewResolver = new ThymeleafViewResolver();        viewResolver.setTemplateEngine(templateEngine());        return viewResolver;    }}要提供靜態(tài)資源,請(qǐng)使用以下配置:@Configuration@EnableWebMvcpublic class StaticResourceConfig implements WebMvcConfigurer {    @Override    public void addResourceHandlers(ResourceHandlerRegistry registry) {        registry                .addResourceHandler("/**")                .addResourceLocations("/static/", "classpath:static/");    }}更新我還提到了我不能接受下面提到的答案的原因,因?yàn)槲业钠渌?lèi)有請(qǐng)求范圍。
查看完整描述

3 回答

?
撒科打諢

TA貢獻(xiàn)1934條經(jīng)驗(yàn) 獲得超2個(gè)贊

假設(shè)您使用的是 Spring Boot,因?yàn)槟跇?biāo)簽中有它,所以您不需要任何配置即可使用 Thymeleaf。


只需擁有此依賴(lài)項(xiàng),您就可以:


@GetMapping("/view-template")

public String viewTemplates(Model model) {

    model.addAttribute("mydata", "this is it")

    return "view-to-process";

}

它應(yīng)該工作。


順便說(shuō)一句,是的,在同一個(gè)課堂上擁有@Configuration和@Controller是您永遠(yuǎn)不需要的東西。


查看完整回答
反對(duì) 回復(fù) 2021-06-23
?
阿波羅的戰(zhàn)車(chē)

TA貢獻(xiàn)1862條經(jīng)驗(yàn) 獲得超6個(gè)贊

看一下 Spring Boot 文檔的典型布局

還有這篇文章SOLID 編程原理

并查看 Spring Boot 指南Spring Boot Thymeleaf(您不需要@Bean 配置)

簡(jiǎn)而言之,您應(yīng)該將
1.MyThymeleafConfig配置
2.TemplateControllerviewTemplates()另一個(gè)端點(diǎn)分開(kāi)


查看完整回答
反對(duì) 回復(fù) 2021-06-23
  • 3 回答
  • 0 關(guān)注
  • 228 瀏覽
慕課專(zhuān)欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢(xún)優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)