[mw_shl_code = applescript,true] <?xml version =“1.0”encoding =“UTF-8”?> <beans xmlns =“http://www.springframework.org/schema/beans” xmlns:xsi =“http: //www.w3.org/2001/XMLSchema-instance“xmlns:p =”http://www.springframework.org/schema/p“ xmlns:context =”http://www.springframework.org/schema/上下文“ xmlns:mvc =”http://www.springframework.org/schema/mvc“ xsi:schemaLocation =”http://www.springframework.org/schema/beans http://www.springframework.org/schema /beans/spring-beans-4.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd http:// www。 springframework.org/schema/context http://www.springframework.org/s ... ing-context-4.0.xsd“> <context:component-scan base-package =“com.itheima.jd”/> <! - 配置注解驱动,如果配置此标签可以不用配置处理器映射器和适配器 - > <mvc:annotation-driven /> < ! - 配置视图解析器 - > <bean class =“org.springframework.web.servlet.view.InternalResourceViewResolver”> <property name =“prefix”value =“/ WEB-INF / jsp /”/> <property name = “suffix”value =“.jsp”/> </ bean> <! - SolrServer的配置 - > <bean id =“httpSolrServer”class =“org.apache.solr.client.solrj.impl.HttpSolrServer”> <constructor -arg index =“0”value =“http:// localhost:8080 / solr”/> </ bean> </ beans> [/ mw_shl_code] |
[mw_shl_code = applescript,true] <?xml version =“1.0”encoding =“UTF-8”?> <web-app xmlns:xsi =“http://www.w3.org/2001/XMLSchema-instance”xmlns =“http://java.sun.com/xml/ns/javaee”xsi:schemaLocation =“http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ ns / javaee / web-app_2_5.xsd “id =”WebApp_ID“version =”2.5“> <display-name> solr-jd </ display-name> <welcome-file-list> <welcome-file> index.html <welcome-file> <welcome-file> index.htm </ welcome-file> <welcome-file> index.jsp </ welcome-file> <welcome-file> default.html </ welcome-file> <welcome -file>的default.htm </欢迎文件> <welcome-file> default.jsp </ welcome-file> </ welcome-file-list> <! - 配置前段控制器 - > <servlet> <servlet-name> springmvc </ servlet-name> <servlet-class> org.springframework.web.servlet.DispatcherServlet </ servlet-class> <init-param> <! - 指定springmvc配置文件的路径 如果不指定默认为:/ WEB-INF / $ {servlet-name} -servlet.xml - > <param-name> contextConfigLocation </ param-name> <param-value> classpath:springmvc.xml </ param-值> </ init-param> </ servlet> <servlet-mapping> <servlet-name> springmvc </ servlet-name> <url-pattern> * .action </ url-pattern> </ servlet-mapping> < !- 解决post乱码问题 - > <filter> <filter-name> CharacterEncodingFilter </ filter-name> <filter-class> org.springframework.web.filter.CharacterEncodingFilter </ filter-class> <init-param> <param-name> encoding </ param-name> <参数值> utf-8 </ param-value> </ init-param> </ filter> <filter-mapping> <filter-name> CharacterEncodingFilter </ filter-name> <url-pattern> / * </ url -pattern> </ filter-mapping> </ web-app> [/ mw_shl_code] |
[mw_shl_code = applescript,true] @Repository public class ProductDaoImpl实现ProductDao { @Autowired private SolrServer solrServer; @覆盖 公共ResultModel queryProduct(SolrQuery查询)抛出异常{ ResultModel resultModel =新ResultModel(); //根据查询对象查询商品列表 QueryResponse queryResponse = solrServer.query(query); SolrDocumentList solrDocumentList = queryResponse.getResults(); //取查询结果的总数量 resultModel.setRecordCount(solrDocumentList.getNumFound()); List <ProductModel> productList = new ArrayList <>(); // SolgeDocument solrDocument:solrDocumentList { //取商品信息 ProductModel productModel = new ProductModel(); productModel.setPid((String)solrDocument.get(“id”)); //取高亮显示 String productName =“”; Map <String,Map <String,List <String >>> highlighting = queryResponse.getHighlighting(); List <String> list = highlighting.get(solrDocument.get(“id”))。get(“product_name”); if(null!= list){ productName = list.get(0); } else { productName =(String)solrDocument.get(“product_name”); } productModel.setName(productName); productModel.setPrice((float)solrDocument.get(“product_price”)); productModel.setCatalog_name((String)solrDocument.get(“product_catalog_name”)); productModel.setPicture((String)solrDocument.get(“ } //商品列表添加到resultmodel中 resultModel.setProductList(productList); 返回resultModel; } } [/ mw_shl_code] |
[mw_shl_code = applescript,true] @Service public class ProductServiceImpl实现ProductService { @Autowired private ProductDao productDao; @覆盖 公共ResultModel queryProduct(字符串的queryString,字符串caltalog_name, 字符串价格,字符串排序,整数页)抛出异常{ //拼装查询条件 SolrQuery查询=新SolrQuery(); //查询条件 if(null!= queryString &&!“”。equals(queryString)){ query.setQuery(queryString); } else { query.setQuery(“*:*”); } //商品分类名称过滤 if(null!= caltalog_name &&!“”。equals(caltalog_name)){ query.addFilterQuery(“product_catalog_name:”+ caltalog_name); if(null!= price &&!“”。equals(price)){ String [] strings = price.split(“ - ”); query.addFilterQuery(“product_price:[”+ strings [0] +“TO”+ strings [1] +“]”); } //排序条件 if(“1”.equals(sort)){ query.setSort(“product_price”,ORDER.desc); } else { query.setSort(“product_price”,ORDER.asc); } //分页处理 if(null == page){ page = 1; } //开始 int start =(page-1)* Commons.PAGE_SIZE; query.setStart(启动); query.setRows(Commons.PAGE_SIZE); //设置默认搜索域 query.set(“df”,“product_keywords”); //高亮设置 query.setHighlight(真); query.setHighlightSimplePre(“<span style = \”color:red \“>”); query.setHighlightSimplePost( “</跨度>”); //查询商品列表 ResultModel resultModel = productDao.queryProduct(query); //计算总页数 long recordCount = resultModel.getRecordCount(); int pages =(int)(recordCount / Commons.PAGE_SIZE); if(recordCount%Commons.PAGE_SIZE> 0){ pages ++; } resultModel.setPageCount(pages); resultModel.setCurPage(页); 返回resultModel; } } [/ mw_shl_code] |
[mw_shl_code = applescript,true] @Controller public class ProductAction { @Autowired private ProductService productService; @RequestMapping(“/ list”) public String queryProduct(String queryString,String catalog_name,String price, String sort,Integer page,Model model)throws Exception { //查询商品列表 ResultModel resultModel = productService.queryProduct(queryString,catalog_name,price ,排序,页面); //列表传递给jsp model.addAttribute(“result”,resultModel); //参数回显 model.addAttribute(“queryString”,queryString); model.addAttribute(“caltalog_name”,catalog_name); model.addAttribute(“price”,price); model.addAttribute(“sort”,sort); model.addAttribute(“page”,page); 返回“product_list”; } } [/ mw_shl_code] |
| 欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) | 黑马程序员IT技术论坛 X3.2 |