【问题标题】:Extract data from HTML to controller without using model in JAVA - freemarker在 JAVA 中不使用模型将数据从 HTML 提取到控制器 - freemarker
【发布时间】:2022-12-06 12:22:57
【问题描述】:
<html>
    <head>
        <title>Report Preview</title>
    </head>
    <body>
        <div class="container">
            <h2>Patient Data</h2>
            <form action = "reportUpdate" method = "post" >
              <input  type="text" name="fvalue" value="testingData1"/>
              <input  type="text"  name="svalue" value="testingData2"/>
              <input  type="submit" value="Submit"/>
            </form>
        </div>
        <script type = "text/javascript" src = "main.js"></script>
    </body>
</html>

我有一个 SpringBootProject,我正在使用上面的 Freemarker 代码模板文件 (*.ftl)。我试图显示一些带有值(绑定)的输入字段,编辑后我想从 HTML 输入标签(fvalue,svalue)中提取数据到控制器而不使用任何模型。如何获取值?

我的控制器代码:

@PostMapping({ "/reportUpdate"})
    public String reportToUpdate( ) {
        
        String firstName = ""; // I should get fvalue here
        String secondName = ""; // I should get svalue here
        
        //Some other logics which will use above value.
        
        return "Data saved!";
    }

【问题讨论】:

  • 到目前为止,您的问题中没有实际问题,也没有要查看的实际(Java)代码。
  • @Stultuske 我现在已经添加了控制器代码。
  • 使用像 @RequestParam("fvalue") 这样注释的方法参数。

标签: java spring-boot model freemarker


【解决方案1】:

通过使用 HttpServletRequest 请求,HttpServletResponse 响应我们可以从 HTML 表单中获取数据。 使用下面的代码。

public void getDataFromForm(HttpServletRequest request, HttpServletResponse response) throws ServletException{
                
                // Get the values from the request using 'getParameter'
                String name = request.getParameter("name");
                
                }

有关更多信息,您可以看到这个。 https://www.geeksforgeeks.org/servlet-form-data/#:~:text=The%20doGet()%20method%20in,the%20server%20to%20the%20browser

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-08-01
    • 2021-06-24
    • 2019-04-26
    • 2015-10-16
    • 1970-01-01
    • 2017-05-03
    • 2018-06-28
    相关资源
    最近更新 更多