GVKun编程网logo

Spring-boot thymeleaf从类路径加载HTML文件(springboot获取类路径)

7

以上就是给各位分享Spring-bootthymeleaf从类路径加载HTML文件,其中也会对springboot获取类路径进行解释,同时本文还将给你拓展#SpringBoot&thymeleaf模板

以上就是给各位分享Spring-boot thymeleaf从类路径加载HTML文件,其中也会对springboot获取类路径进行解释,同时本文还将给你拓展# Spring Boot & thymeleaf模板 使用 th:each 遍历对象数组 -生成一批html标签体、Java Spring Boot VS .NetCore (六) UI thymeleaf vs cshtml、java – Spring Boot和Thymeleaf – 删除严格的HTML错误检查、spring boot + thymeleaf 项目中html 文件不能使用#{}取值?等相关知识,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!

本文目录一览:

Spring-boot thymeleaf从类路径加载HTML文件(springboot获取类路径)

Spring-boot thymeleaf从类路径加载HTML文件(springboot获取类路径)

我有一个多模块项目结构,如:

- application (parent module)--- boot (web-app)----- src/main/resources/templates/layout.html---- todo (jar file)----- src/main/resources/templates/home.html

在我的控制器上:

@RequestMapping(value = "/home")public String home() {    return "todo/home";}

我收到如下错误消息:

Error resolving template "todo/home", template might not exist ormight not be accessible by any of the configured Template Resolvers]

是否需要配置来专门为spring配置一些东西以在类路径上搜索模板?

答案1

小编典典

添加以下属性可以解决我的问题:

spring.thymeleaf.check-template-location=truespring.thymeleaf.prefix=classpath:/templates/spring.thymeleaf.suffix=.htmlspring.thymeleaf.mode=LEGACYHTML5spring.thymeleaf.encoding=UTF-8spring.thymeleaf.content-type=text/htmlspring.thymeleaf.cache=false

# Spring Boot & thymeleaf模板 使用 th:each 遍历对象数组 -生成一批html标签体

# Spring Boot & thymeleaf模板 使用 th:each 遍历对象数组 -生成一批html标签体

在controller中取出emps 对象数组

//1.查询所有的员工,返回列表页面
    @GetMapping("/emps")
    public String list(Model model){
        Collection<Employee> employees = employeeDao.getAll();

        //放在model请求域中
        model.addAttribute("emps",employees);

        //thymeleaf 默认就会拼串 //public static final String DEFAULT_PREFIX = "classpath:/templates/xxx.html";
        //public static final String DEFAULT_SUFFIX = ".html";
        return "emp/list";
    }

在list.html中进行遍历

<tbody>
								<tr th:each="emp:${emps}">
									<td th:text="${emp.id}"></td>
									<td th:text="${emp.lastName}"></td>
									<td th:text="${emp.email}"></td>
									<td th:text="${emp.gender}==1?''男'':''女''"></td>
									<td th:text="${emp.department.departmentName}"></td>
									<td th:text="${#dates.format(emp.birth, ''yyyy-MM-dd'')}"></td>
									<td>
										<button>编辑</button>
										<button>删除</button>
									</td>
								</tr>
							</tbody>

结果展示

Java Spring Boot VS .NetCore (六) UI thymeleaf vs cshtml

Java Spring Boot VS .NetCore (六) UI thymeleaf vs cshtml

前面已经介绍了操作做数据,接下来就介绍先展示数据 UI,当然UI可以做到前后端分离,这里以前后端为分离的情况作为例子

Spring Boot vs .NetCore

Spring中的视图及资源文件主要还是依赖于resources这个目录

下面有static(静态文件放置)、templates(模版目录)

而.NetCore里面静态文件资源都是放置在 wwwroot这个目录下,而其他的 通过Controller、 Views文件夹下的目录结构决定,里面还多了一个区域的概念

下面来看下Controller处理

Spring中的Controller处理

@Controller
public class TestController {

    @Resource
    public UserRepository _userRepository;

    @Autowired
    private UserMapper _userMapper;
    //用户列表
    @RequestMapping("/user/list")
    public String userList(Model model,@RequestParam(value = "page", defaultValue = "0") Integer page,
                       @RequestParam(value = "size", defaultValue = "6") Integer size)
    {
        Sort sort = new Sort(Sort.Direction.DESC, "id");
    Pageable pageable = new PageRequest(page, size, sort);

    Page<UserModel>  modelList= _userRepository.findLYM(pageable);
//        List<UserModel> modelList = _userRepository.findAll();
        model.addAttribute("users", modelList);
        return  "user/list";
    }

    @RequestMapping(value = "/user/add",method = RequestMethod.GET)
    public String userAdd()
    {
        return  "user/add";
    }
    //添加用户
    @RequestMapping(value = "/user/add",method = RequestMethod.POST)
    public String userAdd(@Valid UserModel userParam,ModelMap map)
    {
        UserModel user=new UserModel();
        BeanUtils.copyProperties(userParam,user);
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        user.setRegTime(df.format(new Date()));
     // UserModel result=  _userRepository.save(user);
        _userMapper.insert(user);
        return  "user/add";
    }
}
 

通过 注解 @Controller来标明这个类是一个Controller控制器类@RequestMapping 指定相关方法的请求路由地址以及请求方式

return 到对应的视图上就ok了,需要视图路径标明 ModelMap 对象处理页面数据对象

如:

model.addAttribute("users", modelList);

页面上会存在 users数据对象模型,这点类是与.NetCore 中的 ViewData["users"] \ ViewBag.Users 等操作

.NetCore 中的Controller处理

.NetCore 中其实是通过后缀名来完成的,在创建的时候必须以Controller结束,通过控制器与视图之间的关系完成对应的Invoke操作,这里不过多的说明...

public class HomeController : Controller
    {

        SignInManager<IdentityUser> _signInManager;
        public HomeController(SignInManager<IdentityUser> signInManager)
        {
            _signInManager = signInManager;
        }
       [Route("~/url")]
       [HttpPost]
    public IActionResult Index()
        {
             ViewBag.Msg="这是信息";
            return View();
        }
}
 

这里的 Views文件夹下需要 Home文件夹,而Home文件夹下 需要有 Index.cshtml 视图文件 这是对应起来的  Action的名称 即为视图名称,这样才能保证正常的访问

关于协议请求方式

 Spring Boot通过  @RequestMapping(value = "/user/add", method = RequestMethod.POST)  默认是GET

.NetCore 通过在Action上添加 [HttpPost] 标签来实现 当然 也通过 [Route("url")] 标签来指定请求路由地址, 默认也是GET方式请求

 

处理页面就需要了解 thymeleaf 模版的使用语法规则了 这里不做介绍了

西门看下做的一个列表功能效果

 

实现到这里做一个Java Spring Boot 的开发应该就没有问题了~

java – Spring Boot和Thymeleaf – 删除严格的HTML错误检查

java – Spring Boot和Thymeleaf – 删除严格的HTML错误检查

我使用 Spring Boot作为MVC应用程序,我的视图技术是Thymeleaf.我需要做的一件事就是复制现有网站的HTML(不是我做的……)并使用Thymeleaf渲染它.但是,某些网站的源HTML包含未关闭的HTML标记(例如< Meta>,< link>,< input>)或HTML标记,其中的元素未被引号括起,例如:
<div id=1></div>

代替

<div id="1"></div>

当然在浏览器中这有效……但是Thymeleaf不允许这样做,也不会为页面提供服务.有没有办法让这个更宽松的规则?我搜索了Thymeleaf的文档和Spring Boot参考,但没有找到答案.

只是为了澄清 – 我甚至没有为Thyemeleaf配置我自己的bean,只是通过maven作为spring-boot-starters之一添加到类路径中.所以现在这些都是默认设置.

解决方法

正如@mussdroid所说,一切都需要有效的XML.以下是Thymeleaf文档的一部分,解释了此背景: http://www.thymeleaf.org/doc/articles/fromhtmltohtmlviahtml.html

此外,如果这是一个问题,我相信您可以打开遗留模式以允许非XML模板,但我希望尽可能使用有效的XML:
http://www.thymeleaf.org/doc/tutorials/2.1/usingthymeleaf.html#what-kind-of-templates-can-thymeleaf-process

我不知道自己如何更改模式,但我确信DuckDuckGo可以或在此网站上有人.

spring boot + thymeleaf 项目中html 文件不能使用#{}取值?

spring boot + thymeleaf 项目中html 文件不能使用#{}取值?

spring boot + thymeleaf 项目中html 文件不能使用#{}取值?idea报错不能这样取么?有大佬知道为什么?

今天关于Spring-boot thymeleaf从类路径加载HTML文件springboot获取类路径的分享就到这里,希望大家有所收获,若想了解更多关于# Spring Boot & thymeleaf模板 使用 th:each 遍历对象数组 -生成一批html标签体、Java Spring Boot VS .NetCore (六) UI thymeleaf vs cshtml、java – Spring Boot和Thymeleaf – 删除严格的HTML错误检查、spring boot + thymeleaf 项目中html 文件不能使用#{}取值?等相关知识,可以在本站进行查询。

本文标签: