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>

 

执行后,画面上显示出来后台传递过来的内容/值。

 

 

posted @ 2021-06-21 14:32  77庁長  阅读(77)  评论(0编辑  收藏  举报