【问题标题】:Spring Framework: How could I control html pages with two post requests? Post request won't return correct html pageSpring Framework:我如何控制带有两个 post 请求的 html 页面?发布请求不会返回正确的 html 页面
【发布时间】: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


【解决方案1】:

首先,您在hello.html 中更正第二形式的动作拼写,也使用th:action 进行表单动作,就像您使用 Thymeleaf 一样。此外,您还没有在第一个表单中关闭您的name="text1"。 即

<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>

【讨论】:

  • 感谢指正。我只是按照您的建议进行了更改。
  • 是的!有效!太兴奋了,不敢提。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-07-18
  • 1970-01-01
  • 1970-01-01
  • 2017-01-13
  • 2021-10-03
  • 2020-08-27
  • 2013-01-28
相关资源
最近更新 更多