Thymeleaf练习第一节
关于Thymeleaf的练习第一节
1.首先 创建了ThymeleafStudyController
代码如下:
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import jp.co.sng.activecity.acdms.app.base.AbstractBaseController;
import jp.co.sng.activecity.acdms.app.form.study.StudyForm;
@Controller
@RequestMapping("/ThymeleafStudy/")
@Scope("request")
public class ThymeleafStudyController extends AbstractBaseController {
/**
* 画面
*/
private static final String STUDY_PAGE="screen/study/thymeleafStudy";
/**
* 画面index
*/
@RequestMapping(value="")
public ModelAndView index(@ModelAttribute StudyForm studyForm) {
ModelAndView mv = new ModelAndView();
mv.setViewName(STUDY_PAGE);
// Service返回值设置到form中
// ...
// Service返回值设置到form中
studyForm.setName("testName");
mv.addObject("studyForm", studyForm);
return mv;
}
}
上面的Controller中 需要通过form进行数据的传递
※@ModelAttribute StudyForm
form的定义如下
package form.study;
import java.util.ArrayList;
import java.util.List;
/**
*
* 练习的form
*
*/
public class StudyForm {
// 名称
public String name;
// ID
public Integer ID;
/**
* list
*/
public List<ArrayList> itemList ;
public Integer getID() {
return ID;
}
public void setID(Integer iD) {
ID = iD;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<ArrayList> getItemList() {
return itemList;
}
public void setItemList(List<ArrayList> itemList) {
this.itemList = itemList;
}
}
注意:
2.Controller中的值通过ModeAndView返回到html:screen/study/thymeleafStudy
在 WEB-INF/screen/study/目录下创建 thymeleafStudy.html
代码如下:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:th="http://www.thymeleaf.org" lang="ja"
xmlns:layout="http://www.ultraq.net.nz/web/thymeleaf/layout"
>
<head>
<!-- [[...]] =th:text属性-->
<title>Thymeleaf练习第一节</title>
</head>
<script th:inline="javascript">
</script>
<body layout:fragment="content" th:object="${studyForm}">
<tr>1<span th:text="${studyForm.ID}"></span></tr>
2<span th:text="${studyForm.name}"></span>
3<input ytpe ="text" th:value="${studyForm.name}"></span>
</body>
</html>
执行后,画面上显示出来后台传递过来的内容/值。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
· 25岁的心里话