Java SpringBoot中删除Postgres数据库中的行。

huangapple go评论74阅读模式
英文:

Java SpringBoot Postgres remove rows

问题

以下是翻译好的内容:

帮助我通过点击Del按钮来从Postgres中移除行。
也许需要在数据库和HTML页面中链接ID,但如何做呢?
也许我需要解析HTML页面并传递字符串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>
</table>
@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";
}
英文:

Help me please to remove the row in Postgres by clicking the Del button.
Maybe to link id in DB and id on the HTML page, but how?
Maybe I need to parse the HTML page and pass String id?
Any ideas will help me.

here is my database schema:

Java SpringBoot中删除Postgres数据库中的行。

&lt;table class=&quot;tmc&quot;&gt;
&lt;thead&gt;
&lt;tr&gt;
    &lt;th&gt;ID&lt;/th&gt;&lt;th&gt;TMC&lt;/th&gt;&lt;th&gt;SN&lt;/th&gt;&lt;th&gt;Owner&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
{{#messages}}
&lt;tr&gt;
    &lt;td&gt;{{id}}&lt;/td&gt;
    &lt;td&gt;&lt;span&gt;{{text}}&lt;/span&gt;&lt;/td&gt;
    &lt;td&gt;&lt;i&gt;{{sn}}&lt;/i&gt;&lt;/td&gt;
    &lt;td&gt;&lt;i&gt;{{owner}}&lt;/i&gt;&lt;/td&gt;
    &lt;th&gt;&lt;form action=&quot;/remove&quot; method=&quot;post&quot;&gt;
        &lt;input type=&quot;submit&quot; value=&quot;Del&quot;/&gt;
        &lt;input type=&quot;hidden&quot; name=&quot;_csrf&quot; value=&quot;{{_csrf.token}}&quot; /&gt;
    &lt;/form&gt;
    &lt;/th&gt;
&lt;/tr&gt;
{{/messages}}
&lt;/tbody&gt;

</table>

@Entity
@Table(name = &quot;message&quot;)
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;
}

Rows are forming in database by:

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

答案1

得分: 0

以下是翻译好的部分:

有关您的 HTML 代码存在两个问题:

  1. 需要将 form 元素放在 table 元素外部。
  2. 需要使用 &lt;input type=&quot;hidden&quot; name=&quot;xxx&quot; value=&quot;{{xxx}}&quot; /&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>
			</tr>
			{{/messages}}
		</tbody>
	</table>
</form>
英文:

There are two issues with your html code:

  1. it would be to let the form element outside the table
  2. need to use &lt;input type=&quot;hidden&quot; name=&quot;xxx&quot; value=&quot;{{xxx}}&quot; /&gt; in order to let it can pass parameter to your controller method
&lt;form action=&quot;/remove&quot; method=&quot;post&quot;&gt;
	&lt;input type=&quot;hidden&quot; name=&quot;_csrf&quot; value=&quot;{{_csrf.token}}&quot; /&gt;
	&lt;table class=&quot;tmc&quot;&gt;
	&lt;thead&gt;
	&lt;tr&gt;
		&lt;th&gt;ID&lt;/th&gt;&lt;th&gt;TMC&lt;/th&gt;&lt;th&gt;SN&lt;/th&gt;&lt;th&gt;Owner&lt;/th&gt;
	&lt;/tr&gt;
	&lt;/thead&gt;
	&lt;tbody&gt;
	{{#messages}}
	&lt;tr&gt;
		&lt;td&gt;{{id}} &lt;input type=&quot;hidden&quot; name=&quot;id&quot; value=&quot;{{id}}&quot; /&gt;&lt;/td&gt;
		&lt;td&gt;&lt;span&gt;{{text}}&lt;/span&gt;&lt;/td&gt;
		&lt;td&gt;&lt;i&gt;{{sn}}&lt;/i&gt; 	&lt;input type=&quot;hidden&quot; name=&quot;sn&quot; value=&quot;{{sn}}&quot; /&gt;&lt;/td&gt;
		&lt;td&gt;&lt;i&gt;{{owner}}&lt;/i&gt; &lt;input type=&quot;hidden&quot; name=&quot;owner&quot; value=&quot;{{owner}}&quot; /&gt;&lt;/td&gt;
		&lt;td&gt;&lt;input type=&quot;submit&quot; value=&quot;Del&quot;/&gt;&lt;/td&gt;
		&lt;/th&gt;
	&lt;/tr&gt;
	{{/messages}}
	&lt;/tbody&gt;
	&lt;/table&gt;
&lt;/form&gt;

答案2

得分: 0

<thead>
    <tr>
        <th>ID Equipment</th><th>Equipment Name</th><th>Serial Number</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>
        <td>
            <form action="/remove" method="post" name="remove">
                <input type="submit" value="Delete"/>
                <input type="hidden" name="_csrf" value="{{_csrf.token}}" />
                <input type="hidden" name="sn" value="{{sn}}"/>
            </form>
        </td>
    </tr>
    {{/messages}}
</tbody>
</table>
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";
}
英文:
&lt;thead&gt;
&lt;tr&gt;
    &lt;th&gt;ID ТМЦ&lt;/th&gt;&lt;th&gt;Наименование ТМЦ&lt;/th&gt;&lt;th&gt;Серийный номер ТМЦ&lt;/th&gt;&lt;th&gt;За кем числится&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
{{#messages}}
&lt;tr&gt;
    &lt;td&gt;{{id}}&lt;/td&gt;
    &lt;td&gt;&lt;span&gt;{{text}}&lt;/span&gt;&lt;/td&gt;
    &lt;td&gt;&lt;i&gt;{{sn}}&lt;/i&gt;&lt;/td&gt;
    &lt;td&gt;&lt;i&gt;{{owner}}&lt;/i&gt;&lt;/td&gt;
    &lt;td&gt;
    &lt;form action=&quot;/remove&quot; method=&quot;post&quot; name=&quot;remove&quot;&gt;
        &lt;input type=&quot;submit&quot; value=&quot;Удалить&quot;/&gt;
        &lt;input type=&quot;hidden&quot; name=&quot;_csrf&quot; value=&quot;{{_csrf.token}}&quot; /&gt;
        &lt;input type=&quot;hidden&quot; name=&quot;sn&quot; value=&quot;{{sn}}&quot;/&gt;
    &lt;/form&gt;
    &lt;/td&gt;
&lt;/tr&gt;
{{/messages}}
&lt;/tbody&gt;

</table>

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&lt;Message, Long&gt;{
   List&lt;Message&gt; deleteBySn (String sn);
}

@PostMapping(&quot;/remove&quot;)
public String remove (@RequestParam String sn, Map&lt;String, Object&gt; model) {
    Message messagedel = new Message (sn);
    messageDel.deleteBySn(sn);
    model.put(&quot;messages&quot;, messagedel);
    return &quot;redirect:/main&quot;;
}

huangapple
  • 本文由 发表于 2020年10月7日 00:05:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/64229654.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定