【发布时间】:2021-01-18 02:20:32
【问题描述】:
我试图在 html 页面hello.html 中使用用户输入的 ID 号从数据库中获取员工信息,并在另一个 html 文件 helloResponseDB.html 中显示该信息,但似乎当我提交 ID 号时,它引导我到不同的 html 页面helloResponse.html,它显示一个用户输入的单词。请问您能帮帮我吗?
控制器代码:
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.beans.factory.annotation.Autowired;
@Controller
public class HelloController {
@Autowired
private HelloService helloService;
@GetMapping("/hello")
public String getHello() {
return "hello";
}
@PostMapping("/hello")
public String
postRequest(@RequestParam(name="text1",required=false)String str, Model model) {
model.addAttribute("sample",str);
return "helloResponse";
}
@PostMapping("/hello/db")
public String
postDbRequest(@RequestParam(name="text2")String str, Model model) {
int id = Integer.parseInt(str);
Employee employee = helloService.findOne(id);
model.addAttribute("id",employee.getEmployeeId());
model.addAttribute("name",employee.getEmployeeName());
model.addAttribute("age",employee.getAge());
return "helloResponseDB";
}
}
你好.html:
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<metacharset="UTF8"></meta>
<title>Hello World</title>
</head>
<body>
<h1>Hello World</h1>
<form method="post" action="/hello">
Enter a word:
<input type="text" name="text1 th:value="${text1_value}"/>
<input type="submit" value="Click"/>
</form>
<br/>
<form method="post" actioin="/hello/db">
Enter Employee's ID:
<input type="number" name="text2" th:value="${text2_value}"/>
<input type="submit" value="Click"/>
</form>
<body>
</html>
【问题讨论】:
-
您的表单 html "actioin" 中有错字,您如何发布 requestparam ?
-
我真的不确定这个问题,因为我只是在写一本书之后......
标签: java html spring spring-boot