【问题标题】:How to retrieve multiple data in one column field in db and show it each by each into input field如何在数据库的一个列字段中检索多个数据并将其逐个显示到输入字段中
【发布时间】:2019-10-09 18:10:31
【问题描述】:

我想保存一个具有相同路径的 2 个输入字段。 我想显示一个数据,我有两个具有相同路径的输入字段。但不幸的是,当我单击更新时,它会在每个字段中显示以逗号分隔的两个输入值。 EX 值在每个输入字段中。 6,9 但我想看看客户端何时更新 6 9 例如,当客户端想要更新以及在第二个字段中时,第一个字段上的客户端输入必须相同。有人可以帮我吗?

<div class="form-group">
<label class="control-label col-md-5 col-sm-5 col-xs-12"></label>
<div class="col-md-5 col-sm-5 col-xs-12">
  <form:input path="description3" id="and1" type="text"/>
</div>
  </div>
<div class="form-group">
   <label class="control-label col-md-5 col-sm-5 col-xs-12"></label>
  <div class="col-md-5 col-sm-5 col-xs-12">
         <form:input path="description3" id="and2" type="text"/>
  </div>
</div>


I expecting that the result is like if what you input on first field and second field , and when you click update must show what value does first field and second field has.

但它显示第一个字段显示 15,16 第二个字段也显示 15,16 所以我想要的是这样的前任。

first field i inputted
15
second field i inputted
16

错误就像第一个和第二个字段显示 15,16 我想看看 15 16

谢谢!

【问题讨论】:

    标签: java html mysql spring-mvc


    【解决方案1】:

    据我了解,任务是打印出用户输入的值。

    如果是这样,您需要以某种方式访问​​控制器中的这些值。

    这就是“命令对象”派上用场的地方。

    您基本上创建了输入的 java 表示,比如说 input1 和 input2。如果是这样,您的命令对象应如下所示:

    public class CommandObject {
        private String input1;
        private String input2;
    
        //getter, setters
    }
    

    在您的控制器中创建一个响应 GET 方法的方法,设置命令对象并返回一个表单:

    @RequestMapping(value = "/", method = RequestMethod.GET)
    public String formGet(Model model) {
        model.addAttribute("commandObject", new CommandObject()); 
        // this object is available in your view
        return "form";
    }
    

    然后创建一个表单:

    <spring:url value="/" var="formUrl" />
    <form:form action="${formUrl}" method="post"
        modelAttribute="commandObject">
        <br>
        <form:input path="input1" id="input1" /> 
        <%-- after submitting the form the value from this input will be stored in 
        commandObject.input1 --%>
    
        <br>
    
        <form:input path="input2" id="input2" />
        <%-- after submitting the form the value from this input will be stored in 
        commandObject.input2 --%>
        <br>
    
        <button type="submit">Submit</button>
    
    </form:form>
    

    然后创建一个响应 POST 请求的方法:

    @RequestMapping(value = "/", method = RequestMethod.POST)
    public String formPost(@ModelAttribute CommandObject commandObject) {
        System.out.println(commandObject.getInput1() + " " + commandObject.getInput2());
        return "home";
    }
    

    这里基本上是用@ModelAttribute 获取表单数据并打印出需要的值。

    就是这样!

    我强烈建议您查看有关此问题的官方文档:

    https://docs.spring.io/spring/docs/current/spring-framework-reference/web.html#mvc-ann-modelattrib-method-args

    https://docs.spring.io/spring/docs/current/spring-framework-reference/web.html#mvc-ann-modelattrib-methods

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-12-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多