【问题标题】:Mapping MVC to template using thymeleaf使用 thymeleaf 将 MVC 映射到模板
【发布时间】:2019-04-26 01:34:21
【问题描述】:

使用 thymeleaf 自学 Spring 5、SpringBoot 和 MVC。这是一个简单的应用程序。在移动到数据访问层之前,我首先使用我的控制器来正确填充视图。

我的问题是视图没有填充我在控制器中创建的数据。

这是我的控制器:

// generates a logger class for you
@Slf4j
@Controller
@RequestMapping("/select")
public class AreaCodeController {

/*
 * This method is called BEFORE the @GetMapping method.
 * Building a list of items to display on the select template
 */

@ModelAttribute()
public void addAreaToModel(Model model) {
    // id, code, country, abbr, provStateLongName, StateCode
    List<Area> listing = Arrays.asList(new Area(1000, 123, "US", "AL", "Alabama", StateCode.AL),
            new Area(1001, 124, "US", "MS", "Mississippi", StateCode.MS),
            new Area(1002, 125, "US", "WA", "Washington", StateCode.WA),
            new Area(1003, 126, "US", "WV", "West Virgina", StateCode.WV),
            new Area(1004, 127, "US", "GA", "Georgia", StateCode.GA),
            new Area(1005, 128, "US", "IL", "Illonis", StateCode.IL),
            new Area(1006, 129, "US", "OR", "Oregon", StateCode.OR),
            new Area(1007, 121, "US", "CA", "California", StateCode.CA),
            new Area(1008, 122, "US", "NV", "Nevada", StateCode.NV),
            new Area(1009, 120, "US", "NM", "New Mexico", StateCode.NM),
            new Area(1010, 130, "US", "LA", "WildWilly", StateCode.LA));

    StateCode[] stateCodes = Area.StateCode.values();
    for (StateCode stateCode : stateCodes) {
        model.addAttribute("areaCodeList", filterByStateCode(listing, stateCode));

    }

}

@GetMapping
public String showSelectForm(Model model) {
    model.addAttribute("select", new BusinessNumber());
    return "select";
}

private List<Area> filterByStateCode(List<Area> listing, StateCode sc) 
{
    return listing.stream().filter(x -> x.getCode().equals(sc)).collect(Collectors.toList());
}

}

这是我的看法:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
	xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Virtual Business Number Listing</title>
<link rel="stylesheet" th:href="@{/styles.css}" />

</head>

<body>
	<h1>List of Available Business Numbers</h1>
	<img th:src="@{/images/phone.png}"  style="width:200px;height:125px"/>

	<form method="POST" th:object="${select}">
	
		<div class="grid">
			<div class="area-group" id="abbrs">
				<h3>Choose Your Business Number:</h3>
				<div th:each="area : ${areaCodeList}">
					<input  type="checkbox" name="areaCodeList"  th:value="{area.id}" /> 
						<span th:text="${area.code}">Area Code</span><br/>
				</div>
			</div>
			<br/>
			<input type="Submit" id="submitButton" th:value="Save">
		</div>
	</form>
</body>
</html>

这是我看到的行为:

任何建议将不胜感激。

谢谢,

罗斯

【问题讨论】:

    标签: spring-mvc thymeleaf


    【解决方案1】:

    浏览完代码后,我解决了自己的错误。尽管很痛苦,但它涉及到几件事:

    • filterByStateCode 方法使用了错误的属性进行过滤。它 总是空着回来。
    • 一旦过滤器方法被修复,为模型属性添加相同的键会覆盖每个条目。
    • “选择”模板未键入创建字段。

    【讨论】:

      【解决方案2】:

      我认为您的表单标签不知道要从控制器执行哪个操作,因此您需要以这种方式添加它:

      1- 在你看来:&lt;form method="POST" th:object="${select}" *th:action="@{/something}"*&gt;

      2-在您的控制器中:在您的方法上方 @GetMapping 应该是 @PostMapping(value="/something") 因为您的表单有一个 post 方法创建一个新对象选择

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-03-30
        • 2021-01-19
        • 1970-01-01
        • 1970-01-01
        • 2015-11-20
        • 2014-02-04
        • 2016-04-29
        • 1970-01-01
        相关资源
        最近更新 更多