原型分析
系统架构
工程搭建
创建一个web工程导入jar包
1、springmvc的相关jar包
2、solrJ的jar包
3、Example\lib\ext下的jar包
1.web.xml
<?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">
<!-- POST提交过滤器 UTF-8 -->
<filter>
<filter-name>encoding</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- 前端控制器 -->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>*.action</url-pattern>
</servlet-mapping>
</web-app>
2.Springmvc.xml
<?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:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.2.xsd">
<!-- 配置扫描 器 -->
<context:component-scan base-package="com.itheima.controller"/>
<!-- 配置处理器映射器 适配器 -->
<mvc:annotation-driven/>
<!-- 配置视图解释器 jsp -->
<bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
Dao
功能:接收service层传递过来的参数,根据参数查询索引库,返回查询结果。
参数:SolrQuery对象
返回值:一个商品列表List<ProductModel>
,还需要返回查询结果的总数量。
返回:ResultModel
方法定义:ResultModel queryProduct(SolrQuery query) throws Exception;
商品对象模型:
public class ProductModel {
// 商品编号
private String pid;
// 商品名称
private String name;
// 商品分类名称
private String catalog_name;
// 价格
private float price;
// 商品描述
private String description;
// 图片名称
private String picture;
}
返回值对象模型
public class ResultModel {
// 商品列表
private List<ProductModel> productList;
// 商品总数
private Long recordCount;
// 总页数
private int pageCount;
// 当前页
private int curPage;
}
@Repository
public class ProductDaoImpl implements ProductDao {
@Autowired
private SolrServer solrServer;
@Override
public ResultModel queryProduct(SolrQuery query) throws Exception {
ResultModel resultModel = new ResultModel();
//根据query对象查询商品列表
QueryResponse queryResponse = solrServer.query(query);
SolrDocumentList solrDocumentList = queryResponse.getResults();
//取查询结果的总数量
resultModel.setRecordCount(solrDocumentList.getNumFound());
List<ProductModel> productList = new ArrayList<>();
//遍历查询结果
for (SolrDocument 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("product_picture"));
//添加到商品列表
productList.add(productModel);
}
//商品列表添加到resultmodel中
resultModel.setProductList(productList);
return resultModel;
}
}
Service
功能:接收action传递过来的参数,根据参数拼装一个查询条件,调用dao层方法,查询商品列表。接收返回的商品列表和商品的总数量,根据每页显示的商品数量计算总页数。
参数:
1、查询条件:字符串
2、商品分类的过滤条件:商品的分类名称,字符串
3、商品价格区间:传递一个字符串,满足格式:“0-100、101-200、201-*”
4、排序条件:页面传递过来一个升序或者降序就可以,默认是价格排序。0:升序1:降序
5、分页信息:每页显示的记录条数创建一个常量60条。传递一个当前页码就可以了。
返回值:ResultModel
方法定义:ResultModel queryProduct(String queryString, String caltalog_name, String price, String sort, Integer page) throws Exception;
@Service
public class ProductServiceImpl implements ProductService {
@Autowired
private ProductDao productDao;
@Override
public ResultModel queryProduct(String queryString, String caltalog_name,
String price, String sort, Integer page) throws Exception {
//拼装查询条件
SolrQuery query = new 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;
}
//start
int start = (page-1) * Commons.PAGE_SIZE;
query.setStart(start);
query.setRows(Commons.PAGE_SIZE);
//设置默认搜索域
query.set("df", "product_keywords");
//高亮设置
query.setHighlight(true);
query.addHighlightField("product_name");
query.setHighlightSimplePre("<span style=\"color:red\">");
query.setHighlightSimplePost("</span>");
//查询商品列表
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(page);
return resultModel;
}
}
Controller
功能:接收页面传递过来的参数调用service查询商品列表。将查询结果返回给jsp页面,还需要查询参数的回显。
参数:
1、查询条件:字符串
2、商品分类的过滤条件:商品的分类名称,字符串
3、商品价格区间:传递一个字符串,满足格式:“0-100、101-200、201-*”
4、排序条件:页面传递过来一个升序或者降序就可以,默认是价格排序。0:升序1:降序
5、分页信息:每页显示的记录条数创建一个常量60条。传递一个当前页码就可以了。
6、Model:相当于request。
返回结果:String类型,就是一个jsp的名称。
String queryProduct(String queryString, String caltalog_name, String price, String sort, Integer page, Model model) throws Exception;
@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, sort, page);
//列表传递给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);
return "product_list";
}
}
Jsp
参考资料。