I have a web application that uses Spring MVC and Hibernate. I follow this document https://getbootstrap.com/docs/4.3/components/navs/#tabs to create tabs.
In jsp the code for the tab is
<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>
Here is part of the code in the 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;
}
Here is part of the code in the MemberServiceImpl.java
public List<LoginMember> getOtherAllMember();
public List<LoginMember> getRegularAllMember();
I have the controller(MemberController.java) to call the function for display.
@RequestMapping(value = {"/memberAccountManagementList"}, method = RequestMethod.GET)
public String memberaccountmanagementlist(ModelMap model) {
model.addAttribute("members", memberService.getRegularAllMember());
return "memberAccountManagementList";
}
I run the application, When I click the Regular tab, it can retrieve memebers that belong to Regular. However when click the Other tab, it also shows the members belong to Regular.
I think maybe the controller needs to do something. So I duplicate the code in controller and rename it
@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";
}
When I run the application, it occurs an error, it says
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
I read this post Spring mvc Ambiguous mapping found. Cannot map controller bean method but I don't have the idea how to show relevant member by click the tab.
So my question is how to display relevant data by click the tab? I guess I should write code in the controller but
I should be grateful if someone can give advice on this issue please.
Update:
From this post @RequestParam vs @PathVariable , I tried to use @RequestParam
to get the id myTab
in the jsp file and pass to the controller.
@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";
}
However when I run the code, I have this error message
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).
From the error message, it says myTab
is not present, but the id myTab
exists in the jsp file.
So from the error description, the id myTab
is not pass to the controller, I don't know which part is not correct.
Would someone let me know my mistake. Grateful for your advice please. Thank you.
I updated the code in controller:
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");
}
When I run the application, it still shows the similar error message and description.
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).
In @RequestParam
, I think maybe the #
will effect the code but no matter I put #regular
or regular
or #other
or other
, the error message shows the similar thing. I don't understand why it does not work.