我有一个参与者模型,该模型仅允许用户创建/编辑/删除包含名称和ID的参与者。然后将其整齐地显示在表ID |名称
此信息是否有可能再次显示在不同页面上的另一个表上?或者此信息仅限于此模型和视图表单。
我的参与者控制者
@Controller
公共类ParticipantsController {
@InitBinder("participant") // Validator for participant model
protected void initBinder(WebDataBinder binder) {
binder.addValidators(new ParticipantsValidator());
}
List<Participants> participantList = new ArrayList<>(); // create a list of participants
@RequestMapping("/participants") // Participants page
public String testing(Model model) {
model.addAttribute("participantList", participantList);
return "forms/ParticipantMaster";
}
@RequestMapping(value = "/participantDetail", method = RequestMethod.GET) // Shows participants
public String participantDetail(@ModelAttribute("participant") Participants participant, @RequestParam(value="participantId", required=false, defaultValue="-1") int participantId) {
if (participantId >= 1) { // Shows participants with an id greater than or equal to 1
Participants p2 = participantList.stream().filter(p -> (p.getId() == participantId)).findAny().get(); // gets participants
participant.setId(p2.getId()); // sets participants id
participant.setName(p2.getName()); // sets participants name
} else {
participant.setId(Participants.lastId); // otherwise create a new participant id
Participants.lastId++; // increment last id
Participants.participantsCounter++;
}
return "forms/ParticipantDetail";
}
@RequestMapping(value = "/addParticipant", method = RequestMethod.POST) // Adding participants page
public String addParticipant(@Valid @ModelAttribute("participant") Participants participant,BindingResult result, Model model) {
if (result.hasErrors()) { // validation
return "forms/ParticipantDetail";
}
else {
participantList.removeIf(p -> (p.getId() == participant.getId()));
participantList.add(participant); // add participants
model.addAttribute("participantList", participantList);
return "forms/ParticipantMaster";
}
}
@RequestMapping(value = "/deleteParticipant", method = RequestMethod.GET) // Deleting participants
public String deleteParticipant(@RequestParam(value="participantId", required=false, defaultValue="-1") int participantId, Model model) {
participantList.removeIf(p -> (p.getId() == participantId)); // removes the participant with id
Participants.participantsCounter--;
model.addAttribute("participantList", participantList);
return "forms/ParticipantMaster";
}
}