gpt4 book ai didi

java - 从两个bean获取数据到jsp

转载 作者:太空宇宙 更新时间:2023-11-04 10:53:22 27 4
gpt4 key购买 nike

我有一个简单的注册。

这是我的 jsp 注册页面的一部分:

    <form class="form-horizontal" method="post" action="RightsGroupRegister">
<div class="form-group">
<label for="Name of group" class="col-sm-2 control-label">Name of group:</label>
<div class="col-sm-10">
<input class="form-control" id="name" name="name" placeholder="Name of group">
</div>
</div>
<div class="form-group">
<label for="Rights" class="col-sm-2 control-label">Rights:</label>
<div class="col-sm-10">
<input class="form-control" id="rights" name="rights" placeholder="Rights">
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button class="btn btn-default">Create</button>
</div>
</div>
</form>

这里是一个 Controller :

@Controller
public class RightsGroupController {
private static final Logger LOGGER = LoggerFactory.getLogger(RightsGroupController.class);

private final RightsGroupService rightsGroupService;

@Inject
public RightsGroupController(RightsGroupService rightsGroupService) {
this.rightsGroupService = rightsGroupService;
}

@RequestMapping(value = "RightsGroupRegister", method = RequestMethod.GET)
public ModelAndView getRightsGroupView() {

LOGGER.debug("Received request for addRight");
return new ModelAndView("RightsGroupRegister", "form", new RightsGroupForm());
}

@RequestMapping(value = "RightsGroupRegister", method = RequestMethod.POST)
public String createRightsGroup(@ModelAttribute("form") RightsGroupForm form ) {
LOGGER.debug("Received request to create {}, with result={}", form);

try {
rightsGroupService.save(new Rightsgroup(form.getName(),form.getRights()));
} catch (RightsGroupAlreadyExistsException e) {
LOGGER.debug("Tried to create rightsgroup with existing id", e);
return "right_create";
}
return "redirect:/";
}}

现在的问题是,我真的不明白如何使用它。如何从另一个对象获取此表单数据?例如权利列表(id,name)?

最佳答案

有几种方法可以做到这一点。我想您的意思是访问您在 post 方法中重定向到的页面上的数据。这是一种方法:

public String createRightsGroup(@ModelAttribute("form") RightsGroupForm 表单,Model 模型)

将模型添加到 post 方法中的参数中。那么,即:

model.addAttribute("name", form.getName());
model.addAttribute("rights", form.getRights());

和..

return "redirect:/right_create";

在 JSP (right_create.jsp) 中,您可以访问以下字段:

<table>
<tr>
<td>${name}</td>
<td>${rights}</td>
</tr>
</table>

如果你有一个集合,如果正确你可以这样做:

<table>
<c:forEach items="${rights}" var="right">
<tr>
<td>${right.id}</td>
<td>${right.name}</td>
</tr>
</c:forEach>
</table>

关于java - 从两个bean获取数据到jsp,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47569516/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com