您好,我有一个简单的网页,其中有一个按钮和按钮附近的文字。我需要在单击按钮时更改文本,然后从代码中获取新文本。
这是我需要传递响应的控制器类:
@GetMapping("/stream")
public String openStream(Model model) {
String response = service.openStream();
model.addAttribute("stream", response);
return "mainpage";
}
这是我的html页面,控制器的值必须不是问号:
<div id="container">
<button class="button"
onclick="window.location.href = '/stream';">Stream</button>
<p align="center">?????</p>
</div>
预先感谢您的帮助。
编辑:
我尝试了$ {stream}
但将其作为文本获取价值时,请查看屏幕截图:
编辑2: 我需要将字符串从文本区域传递到控制器中的doc变量。请帮忙。
HTML:
<div>
<textarea rows="10" cols="100" name="description"></textarea>
button class="button" onclick="window.location.href ='/send';">Send</button>
</div>
控制器:
@GetMapping("/send")
public String send(String doc) {
service.sendDoc(doc);
return "mainpage";
}
答案 0 :(得分:4)
更改
<p align="center">?????</p>
收件人
<p align="center">${stream}</p> OR <p th:text="${stream}"></p>
它是如何工作的?
您可以通过$ {key}访问变量值。
示例
model.addAttribute("key", value);
通过${key}
在HTML中获得价值
在Thymeleaf中,这些模型属性(或 可以使用以下语法访问Thymeleaf术语:
${attributeName}
,其中本例中的attributeName为stream
。这个 是Spring EL表达式。简而言之,Spring EL(Spring Expression 语言)是一种支持查询和操作 对象图在运行时。
UPDATE
可以在输入,选择或文本区域上使用th:field属性。
用
代替<p align="center">?????</p>
<input type="text" id="stream" name="stream" th:value="${stream}" />
OR
<input type="text" th:field="*{stream}" />`
OR
<input type="text" id="stream" name="stream" th:field="*{stream}" th:value="${stream}" />
也尝试<p th:inline="text">[[${stream}]]</p>
; <p data-th-text="${stream}"
Thymeleaf Document Thymeleaf Document Inputs
更新2
从Thymeleaf到Spring Boot获取价值
<form th:action="@{/send}" method="get">
<textarea th:name="doc" rows="10" cols="100" name="doc"></textarea>
<button type="submit">Send</button>
</form>
@GetMapping("/send")
public String send(@RequestParam(name="doc", required = false) String doc) {
//change required = false as per requirement
System.out.println("Doc: "+doc);
return "textarea-input";
}
注意:对于实体/模型,请使用“ th:field”
答案 1 :(得分:0)
尝试以下方法:<p align="center">${stream}</p>
或<p th:text="${stream}"></p>
此标签可能会引起问题:
<button class="button"
onclick="window.location.href = '/stream';">Stream</button>
请通过删除它进行检查。
答案 2 :(得分:0)
感谢您的帮助。下面显示的行有帮助:
<p th:inline="text">[[${stream}]]</p>