在web应用中使用solr(二)

本文介绍了一个使用SpringMVC作为控制器层框架,并集成了Solr进行商品搜索的应用案例。具体包括web.xml和springmvc.xml的配置详情,以及JSP页面的主要代码片段,展示了如何实现商品查询、过滤及排序等功能。

继续
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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 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>SolrJD15</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>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  <!-- SpringMVC配置 -->
  <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>
  </servlet>
  <servlet-mapping>
    <servlet-name>springmvc</servlet-name>
    <url-pattern>*.action</url-pattern>
  </servlet-mapping>
  <filter>
    <filter-name>Character 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>Character Encoding</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
</web-app>

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-3.1.xsd 
        http://www.springframework.org/schema/mvc 
        http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd 
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-3.1.xsd 
        http://www.springframework.org/schema/aop 
        http://www.springframework.org/schema/aop/spring-aop-3.1.xsd 
        http://www.springframework.org/schema/tx 
        http://www.springframework.org/schema/tx/spring-tx-3.1.xsd ">
    <!-- 配置扫描包 -->
    <context:component-scan base-package="com.itheima.jd"/>
    <!-- 配置注解驱动 -->
    <mvc:annotation-driven/>
    <!-- jsp视图解析器 -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" >
        <!-- 前缀 -->
        <property name="prefix" value="/WEB-INF/jsp/"></property>
        <!-- 后缀 -->
        <property name="suffix" value=".jsp"></property>
    </bean>
    <bean class="org.apache.solr.client.solrj.impl.HttpSolrServer">
        <constructor-arg value="http://localhost:8080/solr/"></constructor-arg>
    </bean>

</beans>

jsp页面(只保留了主要部分,不能直接引用)

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<!DOCTYPE html>
<html lang="en">
<head>
</style>
<script type="text/javascript">
    function query() {
        //执行关键词查询时清空过滤条件
        document.getElementById("catalog_name").value="";
        document.getElementById("price").value="";
        document.getElementById("sort").value="";
        //执行查询
        queryList();
    }
    function queryList() {
        //提交表单
        document.getElementById("actionForm").submit();
    }
    function filter(key, value) {
        document.getElementById(key).value=value;
        //执行查询
        queryList();
    }
    function sort() {
        var s = document.getElementById("sort").value; 
        if (s != "1") {
            s = "1";
        } else {
            s = "0";
        }
        document.getElementById("sort").value = s;
        //执行查询
        queryList();
    }
</script>
</head>
<body class="root61">

<div id="o-header-2013">
    <div class="w" id="header-2013">
        <div id="logo-2013" class="ld"><a href="http://www.jd.com/" hidefocus="true"><b></b><img src="<c:url value='/resource'/>/logo-201305.png" width="270" height="60" alt="京东"></a></div>
        <!--logo end-->
        <div id="search-2013">
            <div class="i-search ld">
                <ul id="shelper" class="hide"></ul>
                <form id="actionForm" action="list.action" method="POST">
                <div class="form">
                    <input type="text" class="text" accesskey="s" name="queryString" id="key" value="${queryString }"
                        autocomplete="off" onkeydown="javascript:if(event.keyCode==13) {query()}">
                    <input type="button" value="搜索" class="button" onclick="query()">
                </div>
                <input type="hidden" name="catalog_name" id="catalog_name" value="${catalog_name }"/> 
                <input type="hidden" name="price" id="price" value="${price }"/> 
                <input type="hidden" name="sort" id="sort" value="${sort }"/> 
                </form>
            </div>
            <div id="hotwords"></div>
        </div>
        <!--search end-->


    </div>
    <!--header end-->

</div>

<div class="w main">
<div class="right-extra">
<div id="select" clstag="thirdtype|keycount|thirdtype|select" class="m">
    <div class="mt">
        <h1>
            T恤 -<strong>&nbsp;商品筛选</strong>
        </h1>
    </div>
    <div class="mc attrs">
        <div data-id="100001" class="brand-attr">
            <div class="attr">
                <div class="a-key">商品类别:</div>
                <div class="a-values">
                    <div class="v-tabs">
                        <div class="tabcon">
                            <div>
                                <a href="javascript:filter('catalog_name', '三星')" >三星</a>
                            </div>
                            <div>
                                <a href="javascript:filter('catalog_name', '苹果')">苹果</a>
                            </div>
                            <div>
                                <a href="javascript:filter('catalog_name', '联想')">联想</a>
                            </div>
                            <div>
                                <a href="javascript:filter('catalog_name', '诺基亚')">诺基亚</a>
                            </div>
                            <div>
                                <a href="javascript:filter('catalog_name', '酷派')">酷派</a>
                            </div>
                            <div>

                            <div>                                             
                                <a href="javascript:filter('catalog_name', '保健按摩')">保健按摩</a>
                            </div>                                            
                            <div>                                             
                                <a href="javascript:filter('catalog_name', '魅力女人')">魅力女人</a>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <div data-id="100002" class="prop-attrs">
            <div class="attr">
                <div class="a-key">价格:</div>
                <div class="a-values">
                    <div class="v-fold">
                        <ul class="f-list">
                            <li><a href="javascript:filter('price','0-9')">0-999</a></li>
                            <li><a href="javascript:filter('price','10-19')">1000-2000</a></li>
                            <li><a href="javascript:filter('price','20-29')">2000-2999</a></li>
                            <li><a href="javascript:filter('price','30-39')">3000-3999</a></li>
                            <li><a href="javascript:filter('price','40-49')">4000-4999</a></li>
                            <li><a href="javascript:filter('price','50-*')">5000以上</a></li>
                        </ul>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<div id="filter">
    <div class="cls"></div>
    <div class="fore1">
        <dl class="order">
            <dt>排序:</dt>
            <dd>
                <a href="javascript:sort()">价格</a><b></b>
            </dd>
        </dl>
        <dl class="activity">
            <dd></dd>
        </dl>
        <div class="pagin pagin-m">
            <span class="text"><i>1</i>/200</span>
            <a href="javascript:;" class="prev">上一页<b></b></a>
            <a href="javascript:;" class="next">下一页<b></b></a>
        </div>
        <div class="total">
            <span><strong>2000</strong>个商品
            </span>
        </div>
        <span class="clr"></span>
    </div>
</div>
<!--商品列表开始-->
<div id="plist" class="m plist-n7 plist-n8 prebuy">
    <ul class="list-h">
        <c:forEach var="item" items="${productModels }">
        <li pid="${item.pid }">
            <div class="lh-wrap">
                <div class="p-img">
                    <a target="_blank" href="#">
                        <img width="220" height="282" class="err-product" src="<c:url value='/images'/>/${item.picture}">
                    </a>
                </div>
                <div class="p-name">
                    <a target="_blank" href="#">${item.name }</a>
                </div>
                <div class="p-price">
                    <strong><fmt:formatNumber value="${item.price}" maxFractionDigits="2"/></strong><span id="p1269191543"></span>
                </div>
            </div>
        </li>
        </c:forEach>
    </ul>
</div>
<!--商品列表结束-->
</body>
</html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值