我正在学习Spring,并且想从表中删除数据时URL映射存在问题。我不知道应该输入什么URL来再次将我重定向到表。从不相关的表中删除它很容易,因为url映射将类似于("/charList")
,但是在相关表中,我必须重定向到类似return "notebook/chract/{id}";
的映射。但是它将我重定向到ID,该ID已删除,这是错误的。我真的不知道该如何用英语正确解释,因为我不是母语人士。希望你能理解我的问题。下面有一些类:
@Controller
public class TabCharsController {
final private TabRepository tabRepository;
final private TabCharsRepository tabCharsRepository;
final private ElectronicCharsNameRepository electronicCharsNameRepository;
final private ElectronicCharsValueRepository electronicCharsValueRepository;
public TabCharsController(TabRepository tabRepository,
TabCharsRepository tabCharsRepository,
ElectronicCharsNameRepository electronicCharsNameRepository,
ElectronicCharsValueRepository electronicCharsValueRepository) {
this.tabRepository = tabRepository;
this.tabCharsRepository = tabCharsRepository;
this.electronicCharsNameRepository = electronicCharsNameRepository;
this.electronicCharsValueRepository = electronicCharsValueRepository;
}
@GetMapping("tab/{id}/characts")
public String tabCharList(@PathVariable Long id, Model model) throws
Exception{
Tab tab = tabRepository.findById(id).orElseThrow(() -> new
Exception("PostId " + id + " not found"));
List<ElectronicCharactsName> elCharList =
electronicCharsNameRepository.findAll();
List<ElectronicCharactsValue> elCharValueList =
electronicCharsValueRepository.findAll();
List<TabChars> tabChars = tab.getCharacts();
model.addAttribute("model", tab.getModel());
model.addAttribute("tabChars", tabChars);
model.addAttribute("elCharList", elCharList);
model.addAttribute("elCharValueList", elCharValueList);
return "tab/tabCharList";
}
@GetMapping("/tab/characts/{id}/delete")
public String deleteTab(@PathVariable("id") TabChars tabChars){
tabCharsRepository.delete(tabChars);
return "redirect:/tab/{id}/characts"; //THIS REDIRECT IS INCORRECT
}
型号:
@Entity
@Table(name = "tabChars")
public class TabChars {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String name;
private String value;
@ManyToOne
@JoinColumn(name = "tab_id")
private Tab tab;
public TabChars() {
}
public TabChars(String name, String value, Tab tab) {
this.name = name;
this.value = value;
this.tab = tab;
}
public Tab getTab() {
return tab;
}
public void setTab(Tab tab) {
this.tab = tab;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
}
查看:
<#import "../parts/common.ftl" as c>
<@c.page>
<h3>${model}</h3>
<div class="col-3" id="add_form">
<form method="post" action="/tab/${id}/characts">
<div class="form-group">
<select name="name" class="form-control">
<#list elCharList as elect>
<option>${elect.charName}</option>
</#list>
</select>
</div>
<div class="form-group">
<select name="value" class="form-control">
<#list elCharValueList as elect>
<option>${elect.charValue}</option>
</#list>
</select>
</div>
<input type="hidden" name="id" value="${id}" class="form-control">
<input type="hidden" name="_csrf" value="${_csrf.token}">
<button type="submit" class="btn btn-primary form-control">Add</button>
</form>
</div>
<div>
<table class="table">
<thead class="thead-dark">
<tr>
<th>Name</th>
<th>Value</th>
<th>Edit</th>
<th>Delete</th>
</tr>
</thead>
<#list tabChars as char>
<tr>
<td>${char.name}</td>
<td>${char.value}</td>
<td><a href="/tabCharact/${char.id}">Edit</a></td>
<td><a href="/tab/characts/${char.id}/delete">Delete</a></td> //HERE
</tr>
</#list>
</table>
</div>
</@c.page>
那么,我应该在RETURN中的TabCharsController deleteTab方法中键入什么?