zoukankan      html  css  js  c++  java
  • (转)淘淘商城系列——商品搜索功能表现层实现

    http://blog.csdn.net/yerenyuan_pku/article/details/72913431

    首先我们在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/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>
    
        <!-- 引用dubbo服务 -->
        <dubbo:application name="taotao-search-web"/>
        <dubbo:registry protocol="zookeeper" address="192.168.25.128:2181"/>    
        <dubbo:reference interface="com.taotao.search.service.SearchService" id="searchService" />
    
    </beans>

    接着在taotao-search-web工程中的com.taotao.search.controller包下编写一个SearchController类,如下图所示。 

    同样为了方便大家复制,现将SearchController类的代码贴出。

    /**
     * 商品搜索Controller
     * <p>Title: SearchController</p>
     * <p>Description: </p>
     * <p>Company: www.itcast.cn</p> 
     * @version 1.0
     */
    @Controller
    public class SearchController {
    
        @Autowired
        private SearchService searchService;
    
        @Value("${ITEM_ROWS}")
        private Integer ITEM_ROWS;
    
        @RequestMapping("/search")
        public String searchItem(@RequestParam("q") String queryString, 
                @RequestParam(defaultValue="1") Integer page, Model model) throws Exception {
            // 调用服务搜索商品信息
            SearchResult searchResult = searchService.search(queryString, page, ITEM_ROWS);
            // 使用Model向页面传递参数
            model.addAttribute("query", queryString);
            model.addAttribute("totalPages", searchResult.getTotalPage());
            model.addAttribute("itemList", searchResult.getItemList());
            model.addAttribute("page", page);
            // 返回逻辑视图
            return "search";
        }
    
    }
    • 1

    下面我会对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类的searchItem方法当中定义的参数名称”queryString”不一致,名称不一致的情况下需要指定映射关系,于是便有了@RequestParam("q") String queryString。 

    我们从上图的请求当中可以看到也没有每页显示多少条的参数,而这个参数必须是灵活可配置的,因此最好写到taotao-search-web工程的resource.properties配置文件当中,如下图所示。 

    SearchController类的search方法中的Model参数是为了向页面回显数据用的,我们可以看下search.jsp搜索页面都需要回显哪些数据,可以看到有四个值需要回显,这也刚好对应着我们在SearchController类中所回显的四个变量。 

    search方法最后返回逻辑视图”search”经过spring自动添加后缀.jsp,于是便去访问这个search.jsp页面,并把四个变量也都带过来了,这样,页面便可以正常显示数据了。 
    这样,实现商品搜索功能的表现层代码便写完了。

  • 相关阅读:
    数据绑定表达式语法(Eval,Bind区别)
    使用博客园的第一件事 自定义主题
    sql2000 跨服务器复制表数据
    使用UpdatePanel 局部刷新出现中文乱码的解决方法!!
    MMC不能打开文件MSC文件
    sql 日期 、时间相关
    loaded AS2 swf call function in AS3 holder
    Rewrite the master page form action attribute in asp.net 2.0
    100万个不重复的8位的随机数
    flash 中实现斜切变型
  • 原文地址:https://www.cnblogs.com/telwanggs/p/6962464.html
Copyright © 2011-2022 走看看