e3mall项目:前台搜索商品
准备工作:新建项目 e3-search-web 子工程。并创建好包结构,将静态页面导入
一、相关配置文件
(1)springmvc配置
<?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/context"
xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
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.2.xsd
http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.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">
<!--读取resource配置文件-->
<context:property-placeholder location="classpath:other/resource.properties"/>
<!--开启注解支持-->
<context:component-scan base-package="cn.e3mall.search.controller" />
<!--配置SpringMVC注解支持-->
<mvc:annotation-driven />
<!--配置SpringMVC视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
<!--配置全局异常处理器-->
<bean class="cn.e3mall.search.exception.GlobalExceptionResolveer"/>
<!-- 引用dubbo服务 -->
<dubbo:application name="e3-search-web"/>
<dubbo:registry protocol="zookeeper" address="192.168.25.130:2181"/>
<dubbo:reference interface="cn.e3mall.search.service.SearchService" id="searchService" />
</beans>
(2)resouce.properties
## 搜索结果页面每页显示的记录数
search.rows=20
(3)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"
version="2.5">
<display-name>e3-search-web</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
<!--配置SpringMVC前段控制器-->
<servlet>
<servlet-name>e3-search-web</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/springmvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>e3-search-web</servlet-name>
<!--伪静态化-->
<url-pattern>*.html</url-pattern>
</servlet-mapping>
<!--POST提交乱码解决-->
<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>
</web-app>
(4)pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>e3-parent</artifactId>
<groupId>cn.e3mall</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>e3-search-web</artifactId>
<packaging>war</packaging>
<name>e3-search-web</name>
<dependencies>
<dependency>
<groupId>cn.e3mall</groupId>
<artifactId>e3-search-interface</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>cn.e3mall</groupId>
<artifactId>e3-manager-dao</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<!-- Spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jms</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
</dependency>
<!-- JSP相关 -->
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jsp-api</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<!-- 配置Tomcat插件 -->
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<port>8085</port>
<path>/</path>
</configuration>
</plugin>
</plugins>
</build>
</project>
二、相关代码
(1)e3-search-web中的SearchController
package cn.e3mall.search.controller;
import cn.e3mall.common.entity.SearchResultByPage;
import cn.e3mall.search.service.SearchService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
/**
* 商品搜索 web层
* Author: xushuai
* Date: 2018/5/24
* Time: 16:23
* Description:
*/
@Controller
public class SearchController {
@Autowired
private SearchService searchService;
@Value("${search.rows}")
private Integer rows;
@RequestMapping("/search")
public String search(String keyword, @RequestParam(defaultValue = "1") Integer page, Model model) throws Exception {
//get请求乱码处理
keyword = new String(keyword.getBytes("iso-8859-1"),"utf-8");
//查询商品列表
SearchResultByPage result = searchService.search(keyword, page, rows);
//将需要的数据保存,发送到页面
model.addAttribute("query",keyword);//查询条件回显
model.addAttribute("totalPages",result.getTotalPages());//总页数
model.addAttribute("page",page);//当前页数
model.addAttribute("recourdCount",result.getRecourdCount());//总记录数
model.addAttribute("itemList",result.getItemList());//商品集合
return "search";
}
}
(2)e3-search中的Service以及ServiceImpl(新增方法及其实现)
/**
* 搜索商品
* @auther: xushuai
* @date: 2018/5/24 16:30
* @return: SearchResultByPage 商品分页数据
*/
SearchResultByPage search(String keyword, Integer page, Integer rows) throws Exception;
@Override
public SearchResultByPage search(String keyword, Integer page, Integer rows) throws Exception {
//封装查询条件
SolrQuery query = new SolrQuery();
setQueryParam(query,keyword,page,rows);
//执行查询
SearchResultByPage searchResult = searchDao.search(query);
//计算总页数
Long count = searchResult.getRecourdCount();
int totalPages = (int) (count / rows);
if(count % rows > 0){
totalPages ++;
}
searchResult.setTotalPages(totalPages);
return searchResult;
}
/**
* 将查询参数封装到查询条件中(提高复用性、可维护性)
* @auther: xushuai
* @date: 2018/5/24 16:32
*/
private void setQueryParam(SolrQuery query, String keyword, Integer page, Integer rows) {
//设置查询条件
query.setQuery(keyword);
//设置默认查询域
query.set("df","item_keywords");
//设置分页参数
query.setStart((page - 1) * rows);
query.setRows(rows);
//设置高亮显示
query.setHighlight(true);
query.setHighlightSimplePre("<em style='color:red'>");
query.setHighlightSimplePost("</em>");
}