【问题标题】:Java SpringBoot Postgres remove rowsJava SpringBoot Postgres 删除行
【发布时间】:2021-01-21 13:27:23
【问题描述】:

请帮助我通过单击 Del 按钮删除 Postgres 中的行。 也许要链接数据库中的 id 和 HTML 页面上的 id,但是如何? 也许我需要解析 HTML 页面并传递 String id? 任何想法都会帮助我。

这是我的数据库架构:

<table class="tmc">
<thead>
<tr>
    <th>ID</th><th>TMC</th><th>SN</th><th>Owner</th>
</tr>
</thead>
<tbody>
{{#messages}}
<tr>
    <td>{{id}}</td>
    <td><span>{{text}}</span></td>
    <td><i>{{sn}}</i></td>
    <td><i>{{owner}}</i></td>
    <th><form action="/remove" method="post">
        <input type="submit" value="Del"/>
        <input type="hidden" name="_csrf" value="{{_csrf.token}}" />
    </form>
    </th>
</tr>
{{/messages}}
</tbody>
@Entity
@Table(name = "message")
public class Message {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private Integer id;
private String text;
private String sn;
private String owner;

public Message() {
}

public Message(String text, String sn, String owner) {
    this.text = text;
    this.sn = sn;
    this.owner = owner;
}

行在数据库中形成:

@PostMapping("/main")
public String add (
        @RequestParam String owner,
        @RequestParam String text,
        @RequestParam String sn, Map<String, Object> model) {
    Message message = new Message (text, sn, owner);
    if (text != null && !text.isEmpty() & sn != null && !sn.isEmpty() & owner != null && 
!owner.isEmpty()) {
        if (!text.matches("^[0-9].*$")) {
            messageRepo2.save(message);
            Iterable<Message> messages = messageRepo2.findAll();
            model.put("messages", messages);
        } else
            model.put("error", "ТМЦ не должно начинаться с цифры");
        Iterable<Message> messages = messageRepo2.findAll();
        model.put("messages", messages);
    } else {
        model.put("error", "Заполните все поля!");
        Iterable<Message> messages = messageRepo2.findAll();
        model.put("messages", messages);
    }
    return "main";
}

【问题讨论】:

    标签: java postgresql spring-boot spring-mvc jdbc


    【解决方案1】:

    您的 html 代码有两个问题:

    1. 就是让form 元素在table 之外
    2. 需要使用&lt;input type="hidden" name="xxx" value="{{xxx}}" /&gt;,以便它可以将参数传递给您的控制器方法
    <form action="/remove" method="post">
        <input type="hidden" name="_csrf" value="{{_csrf.token}}" />
        <table class="tmc">
        <thead>
        <tr>
            <th>ID</th><th>TMC</th><th>SN</th><th>Owner</th>
        </tr>
        </thead>
        <tbody>
        {{#messages}}
        <tr>
            <td>{{id}} <input type="hidden" name="id" value="{{id}}" /></td>
            <td><span>{{text}}</span></td>
            <td><i>{{sn}}</i>   <input type="hidden" name="sn" value="{{sn}}" /></td>
            <td><i>{{owner}}</i> <input type="hidden" name="owner" value="{{owner}}" /></td>
            <td><input type="submit" value="Del"/></td>
            </th>
        </tr>
        {{/messages}}
        </tbody>
        </table>
    </form>
    

    【讨论】:

    • 感谢您的反馈。这帮助我找到了正确的解决方案。
    【解决方案2】:
    <thead>
    <tr>
        <th>ID ТМЦ</th><th>Наименование ТМЦ</th><th>Серийный номер ТМЦ</th><th>За кем числится</th>
    </tr>
    </thead>
    <tbody>
    {{#messages}}
    <tr>
        <td>{{id}}</td>
        <td><span>{{text}}</span></td>
        <td><i>{{sn}}</i></td>
        <td><i>{{owner}}</i></td>
        <td>
        <form action="/remove" method="post" name="remove">
            <input type="submit" value="Удалить"/>
            <input type="hidden" name="_csrf" value="{{_csrf.token}}" />
            <input type="hidden" name="sn" value="{{sn}}"/>
        </form>
        </td>
    </tr>
    {{/messages}}
    </tbody>
    
    import com.example.webapp.domain.Message;
    import org.springframework.data.repository.CrudRepository;
    import org.springframework.transaction.annotation.Transactional;
    import java.util.List;
    @Transactional
       public interface MessageDel extends CrudRepository<Message, Long>{
       List<Message> deleteBySn (String sn);
    }
    
    @PostMapping("/remove")
    public String remove (@RequestParam String sn, Map<String, Object> model) {
        Message messagedel = new Message (sn);
        messageDel.deleteBySn(sn);
        model.put("messages", messagedel);
        return "redirect:/main";
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-09-27
      • 1970-01-01
      • 2021-07-17
      • 1970-01-01
      • 2014-02-27
      • 2017-07-30
      • 2018-12-24
      相关资源
      最近更新 更多