我想向您寻求帮助,我怎样才能在Spring网站开发中拥有一个自动完整的搜索框,但我似乎无法做到正确?这些是我正在使用的代码。请帮忙,谢谢!
UserRoleService.java
public interface UserRoleService {
List<UserRole> getUserRole(UserRole userRole) String role;
}
UserRoleServiceImpl.java
public class UserRoleServiceImpl implements UserRoleService {
private UserRoleDao userRoleDao;
@Autowired
UserRoleServiceImpl(UserRoleDao userRoleDao){
this.userRoleDao = userRoleDao;
}
public List<UserRole> getUserRole(UserRole userRole, String role)throws Exception{
List<UserRole> userRoleList = userRoleDao.getUserRole(userRoleDao);
return userRoleList;
}
}
控制器
@RequestMapping(value = "userRole", method = RequestMethod.GET)
public @ResponseBody
List<UserRole> getUserRole(@RequestParam("term") String role)
throws Exception{
List<UserRole> userRoleList = userRoleService.getUserRole(role);
ArrayList roleList = new ArrayList();
int count = 0;
for(UserRole ei : roleList){
UserRole roleInfo = new UserRole();
roleInfo.put("empUserRole", ei.getUserRole());
roleInfo.put("value", ei.getUserRole());
roleInfo.put("label", ei.getUserRole());
roleList.add(roleInfo);
count++;
if(count > AUTOCOMPLETE_SIZE){
break;
}
}
return roleList;
}
UserRole.jsp
<form:form commandName ="userRoleForm" id="userRoleForm">
<table>
<tr>
<td>User Role</td>
<td><form:input path ="userName" id="empUserRole"/></td>
</tr>
</table>
</form:form>
UserRole.js
$("#empUserRole").autocomplete({
var empRole = {
source: "userRole",
select: function(event, ui){
$("empUserRole").val(ui.item.empUserRole);
},
miLength: 4
};
});