首先我们需要在taotao-search-web工程中添加对搜索服务的引用,如下图所示。 为了方便大家复制,现将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: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://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd"> <!-- 加载外部属性文件 --> <context:property-placeholder location="classpath:resource/*.properties" /> <context:component-scan base-package="com.taotao.search.controller" /> <mvc:annotation-driven /> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/jsp/" /> <property name="suffix" value=".jsp" /> </bean> <!-- 配置静态资源映射 --> <!-- 指定/WEB-INF/js/和/WEB-INF/css/下的所有的静态资源(包括子目录下的静态资源)都不被拦截 --> <!-- mapping=/js/**:表示访问静态资源的路径的形式,/js/**表示可以访问/js/下的静态资源或者所有的子目录下的静态资源 --> <!-- <mvc:resources location="/WEB-INF/js/" mapping="/js/**"/> <mvc:resources location="/WEB-INF/css/" mapping="/css/**"/> --> <!-- 引用Dubbo服务 --> <dubbo:application name="taotao-search-web"/> <dubbo:registry protocol="zookeeper" address="192.168.81.131:2181"/> <dubbo:reference interface="com.taotao.search.service.SearchService" id="searchService" /> </beans>然后在taotao-search-web工程中的com.taotao.search.controller包下编写一个SearchController类,如下图所示。 同样为了方便大家复制,也将SearchController类的代码贴出,如下所示。
package com.taotao.search.controller; 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; import com.taotao.common.pojo.SearchResult; import com.taotao.search.service.SearchService; @Controller public class SearchController { @Value("${ITEM_ROWS}") private Integer ITEM_ROWS; @Autowired private SearchService service; /** * 根据条件来搜索商品数据 * * @param page:下一页没搜出来,原因在于这个page没有默认值,没有默认值,你在返回给jsp页面,那就是null, * 那么下一页就转不了了,转的时候把null转成一个数字就出问题了。所以应该给其一个默认值。 * 也就是说刚开始查询的时候,你就应该给其一个默认值,例如1 * @param queryString * @return * @throws Exception */ @RequestMapping("/search") public String search(@RequestParam(defaultValue = "1") Integer page, @RequestParam(value = "q") String queryString, Model model) throws Exception { // 1. 引入服务 // 2. 注入 // 3. 调用 SearchResult result = service.search(queryString, page, ITEM_ROWS); // 4. 设置数据并传递到jsp页面中 model.addAttribute("query", queryString); model.addAttribute("totalPages", result.getPageCount()); // 总页数 model.addAttribute("itemList", result.getItemList()); model.addAttribute("page", page); return "search"; } }下面我会对SearchController类当中的代码做一下解释,并且我们还需要在配置文件中配置下每页显示的商品数量。
首先,我来讲讲@RequestMapping("/search")注解中的值为何是/search,这还得要从搜索页面说起,在taotao-portal-web工程中的index.jsp页面当中是没有搜索代码的,搜索代码在commons/header.jsp页面当中,如下图所示。 我们打开commons目录下的header.jsp页面,可以看到当我们在搜索框中输入搜索关键字并按回车或者点击搜索按钮后会触发search方法,这个search方法并不在header.jsp这个页面当中,而是在引用的base-v1.js文件当中,如下图所示。 我们到base-v1.js文件当中找到search方法,在search方法中我们可以看到要访问的页面是search.html,而我们的taotao-search-web工程拦截的就是以.html结尾的请求,因此这是没有任何问题的。既然要请求的是search.html,那么我们当然要拦截的是search了。另外,大家在这里也可以看到请求后面的参数是以q来携带的,这个变量名字与我们SearchController类的search方法当中定义的参数名称(即queryString)不一致,名称不一致的情况下就需要指定映射关系了,于是便有了@RequestParam("q") String queryString。 我们从上图的请求当中还可以看到并没有携带每页显示多少条数据的参数,而这个参数必须又要是灵活可配置的,因此最好将其写到taotao-search-web工程的resource.properties配置文件当中去,如下图所示。 SearchController类的search方法中Model类型的参数是为了向页面回显数据用的,我们可以看下search.jsp搜索页面都需要回显些什么数据,可以看到有四个值需要回显,这也刚好对应着我们在SearchController类中所回显的四个变量。 search方法最后返回的search逻辑视图经过Spring自动添加.jsp后缀之后,于是便去访问search.jsp这个页面了,并把四个变量也都带过来了,这样页面便可以正常显示数据了。
至此,实现商品搜索功能的表现层代码便写完了。