Java Spring框架:如何通过两个post请求控制html页面?Post请求赢得';无法返回正确的html页面
我试图在html页面Java Spring框架:如何通过两个post请求控制html页面?Post请求赢得';无法返回正确的html页面,java,html,spring,spring-boot,Java,Html,Spring,Spring Boot,我试图在html页面hello.html中使用用户输入的ID号从数据库中获取员工信息,并在另一个html文件helloResponseDB.html中显示该信息,但当我提交ID号时,似乎会导致我进入不同的html页面helloResponse.html,它显示用户输入的单词。你能帮我吗 控制器代码: import org.springframework.stereotype.Controller; 导入org.springframework.ui.Model; 导入org.springframe
hello.html
中使用用户输入的ID号从数据库中获取员工信息,并在另一个html文件helloResponseDB.html
中显示该信息,但当我提交ID号时,似乎会导致我进入不同的html页面helloResponse.html
,它显示用户输入的单词。你能帮我吗
控制器代码:
import org.springframework.stereotype.Controller;
导入org.springframework.ui.Model;
导入org.springframework.web.bind.annotation.GetMapping;
导入org.springframework.web.bind.annotation.PostMapping;
导入org.springframework.web.bind.annotation.RequestParam;
导入org.springframework.beans.factory.annotation.Autowired;
@控制器
公共类Hello控制器{
@自动连线
私人HelloService HelloService;
@GetMapping(“/hello”)
公共字符串getHello(){
回复“你好”;
}
@邮戳(“/你好”)
公共字符串
postRequest(@RequestParam(name=“text1”,required=false)字符串str,Model){
model.addAttribute(“sample”,str);
返回“helloResponse”;
}
@后映射(“/hello/db”)
公共字符串
postDbRequest(@RequestParam(name=“text2”)字符串str,模型模型){
int id=Integer.parseInt(str);
Employee=helloService.findOne(id);
model.addAttribute(“id”,employee.getEmployeeId());
model.addAttribute(“name”,employee.getEmployeeName());
model.addAttribute(“age”,employee.getAge());
返回“helloResponseDB”;
}
}
hello.html:
你好,世界
你好,世界
输入一个单词:
首先,在hello.html
中纠正第二个表单中的动作拼写,并使用th:action
作为表单动作,就像使用Thymeleaf一样。此外,您还没有在第一个表单中关闭您的name=“text1”
。
i、 e
您的表单html“actioin”中有一个输入错误,您如何发布requestparam?谢谢您的更正。我真的不确定这个问题,因为我只是在一本书后面写了一篇文章……谢谢您的更正。我只是按照你的建议做了些改变。这有用吗…?是的!成功了!我激动得说不出这件事。
<input type="text" name="text1 th:value="${text1_value}"/>
<input type="text" name="text1" th:value="${text1_value}"/>
<form th:action="@{/hello}" method="post">
Enter a word:
<input type="text" name="text1" th:value="${text1_value}">
<input type="submit" value="Click">
</form>
<br/>
<form th:action="@{/hello/db}" method="post" >
Enter Employee ID:
<input type="number" name="text2" th:value="${text2_value}" >
<input type="submit" value="Click" >
</form>