Java项目实战 - solr实现前端商品搜索

在zheyi-portal工程中实现。

Dao

无。

Service

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
@Service
public class SearchServiceImpl implements SearchService {

@Value("${SEARCH_BASE_URL}")
private String SEARCH_BASE_URL;

@Override
public SearchResult search(String query, int page) {
Map<String, String> param = new HashMap<>();
param.put("q", query);
param.put("page", page + "");
try {
String getString = HttpClientUtil.doGet(SEARCH_BASE_URL, param);
TaotaoResult result = TaotaoResult.formatToPojo(getString, SearchResult.class);
if (result.getStatus() == 200) {
SearchResult searchResult = (SearchResult) result.getData();
return searchResult;
}
} catch (Exception e) {
e.printStackTrace();
}

return null;
}

}

Controller

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
@Controller
public class SearchServiceController {

@Autowired
private SearchService searchService;

@RequestMapping("/search")
public String search(@RequestParam("q")String query, @RequestParam(defaultValue="1")Integer page,
Model model) {

if (query != null) {
try {
query = new String(query.getBytes("iso8859-1"), "utf-8");
} catch (Exception e) {
e.printStackTrace();
}

}
SearchResult searchResult = searchService.search(query, page);

model.addAttribute("query", query);
model.addAttribute("totalPages", searchResult.getTotalPageCount());
model.addAttribute("itemList", searchResult.getItemList());
model.addAttribute("page", page);

return "search";
}

}
唐胡璐 wechat
欢迎您扫一扫上面的微信公众号,订阅我的博客!
分享创造价值,您的支持将鼓励我继续前行!