我有一个使用Spring MVC和Hibernate的Web应用程序。我按照这份文档https://getbootstrap.com/docs/4.3/components/navs/#tabs创建标签。
在jsp中,标签的代码为
<div id="memberDiv" class="container">
<div id="memberAccountManagement" class="jumbotron">
<h3>Member List</h3>
<hr class="mb-4">
<ul class="nav nav-tabs" id="myTab" role="tablist">
<li class="nav-item"><a class="nav-link active" id="other-tab"
data-toggle="tab" href="#other" role="tab" aria-controls="other"
aria-selected="true">Other</a></li>
<li class="nav-item"><a class="nav-link" id="regular-tab"
data-toggle="tab" href="#regular" role="tab"
aria-controls="regular" aria-selected="false">Regular</a></li>
</ul>
<div class="tab-content" id="myTabContent">
<div class="tab-pane fade show active" id="other" role="tabpanel"
aria-labelledby="other-tab">Other member content here
<br />
<table class="table table-striped table-bordered table-hover">
<thead>
<tr>
<th>ID</th>
<th>NAME</th>
<th>ROLE</th>
</tr>
</thead>
<tbody>
<c:forEach var="member" items="${members}">
<tr>
<td>${member.id}</td>
<td>${member.username}</td>
<td>${member.memberRole.role}</td>
<td>${member.enabled}</td>
</tr>
</c:forEach>
</tbody>
</table>
</div>
<div class="tab-pane fade" id="regular" role="tabpanel"
aria-labelledby="regular-tab">Regular member content here
<br />
<table class="table table-striped table-bordered table-hover">
<thead>
<tr>
<th>ID</th>
<th>NAME</th>
<th>ROLE</th>
<th>ENABLED</th>
<th> </th>
</tr>
</thead>
<tbody>
<c:forEach var="member" items="${members}">
<tr>
<td>${member.id}</td>
<td>${member.username}</td>
<td>${member.memberRole.role}</td>
</tr>
</c:forEach>
</tbody>
</table>
</div>
</div>
</div>
</div>
这是MemberService.java中的代码的一部分
//for other member
@Override
public List<LoginMember> getOtherAllMember() {
List<LoginMember> memberAccountManagementList = new ArrayList<LoginMember>();
memberAccountManagementList = memberDao.list("from LoginMember u where u.memberRole.role='ROLE_Other' order by u.username");
return memberAccountManagementList;
}
//for regular member
@Override
public List<LoginMember> getRegularAllMember() {
List<LoginMember> memberAccountManagementList = new ArrayList<LoginMember>();
memberAccountManagementList = memberDao.list("from LoginMember u where u.memberRole.role='ROLE_Regular' order by u.username");
return memberAccountManagementList;
}
这是MemberServiceImpl.java中的代码的一部分
public List<LoginMember> getOtherAllMember();
public List<LoginMember> getRegularAllMember();
我有控制器(MemberController.java)调用该函数进行显示。
@RequestMapping(value = {"/memberAccountManagementList"}, method = RequestMethod.GET)
public String memberaccountmanagementlist(ModelMap model) {
model.addAttribute("members", memberService.getRegularAllMember());
return "memberAccountManagementList";
}
我运行该应用程序,当我单击“常规”选项卡时,它可以检索属于“常规”的成员。但是,当单击“其他”选项卡时,它还会显示成员属于“常规”。
我认为控制器可能需要做一些事情。所以我在控制器中复制了代码并重命名了
@RequestMapping(value = {"/memberAccountManagementList"}, method = RequestMethod.GET)
public String memberaccountmanagementlist(@RequestParam("myTab") String myTab, ModelMap model) {
System.out.println(myTab); //try to show value
//model.addAttribute("members", memberService.getRegularAllUser());
model.addAttribute("members", memberService.getOtherAllMember());
return "memberAccountManagementList";
}
当我运行该应用程序时,它会显示错误
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping': Invocation of init method failed; nested exception is java.lang.IllegalStateException: Ambiguous mapping. Cannot map 'memberController' method
我读了这篇文章Spring mvc Ambiguous mapping found. Cannot map controller bean method,但不知道如何通过单击选项卡显示相关成员。
所以我的问题是如何通过单击选项卡显示相关数据?我想我应该在控制器中写代码,但是
如果有人可以就此问题提供建议,我将不胜感激。
更新:
在这篇帖子@RequestParam vs @PathVariable中,我试图使用@RequestParam
在jsp文件中获取ID myTab
并传递给控制器。
@RequestMapping(value = {"/memberAccountManagementList"}, method = RequestMethod.GET)
public String memberaccountmanagementlist(@RequestParam("myTab") String myTab, ModelMap model) {
System.out.println(myTab); //try to show value
//model.addAttribute("members", memberService.getRegularAllMember());
model.addAttribute("members", memberService.getOtherAllMember());
return "memberAccountManagementList";
}
但是,当我运行代码时,出现此错误消息
Message: Required String parameter 'myTab' is not present
Description: The server cannot or will not process the request due to something that is perceived to be a client error (e.g., malformed request syntax, invalid request message framing, or deceptive request routing).
从错误消息中可以看出,myTab
不存在,但是ID myTab
存在于jsp文件中。
因此从错误描述中,id myTab
没有传递给控制器,我不知道哪一部分不正确。
有人会让我知道我的错误吗?请感谢您的建议。谢谢。
我更新了控制器中的代码:
public String memberaccountmanagementlist(@RequestParam(value="#regular", required=false) String regular,ModelMap model) {
System.out.println("requestparam value is " + regular);
if (regular== "#regular")
{
model.addAttribute("members", memberService.getRegularAllUser());
}
else if(regular =="#other")
{
model.addAttribute("members", memberService.getOtherAllMember());
}
else
{
System.out.println("need to further investigate");
}
当我运行该应用程序时,它仍然显示类似的错误消息和说明。
Message: Required String parameter '#regular' is not present
Description: The server cannot or will not process the request due to something that is perceived to be a client error (e.g., malformed request syntax, invalid request message framing, or deceptive request routing).
在@RequestParam
中,我认为#
可能会影响代码,但是无论我放置#regular
还是regular
还是#other
还是{{1} },错误消息显示类似的内容。我不明白为什么它不起作用。