英文:
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:

<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;
}
Rows are forming in database by:
@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";
}
答案1
得分: 0
以下是翻译好的部分:
有关您的 HTML 代码存在两个问题:
- 需要将 
form元素放在table元素外部。 - 需要使用 
<input type="hidden" name="xxx" value="{{xxx}}" />以便能够将参数传递给您的控制器方法。 
<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:
- it would be to let the 
formelement outside thetable - need to use 
<input type="hidden" name="xxx" value="{{xxx}}" />in order to let it can pass parameter to your controller method 
<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
得分: 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";
}
英文:
<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>
</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";
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论