Diferencia entre revisiones de «Compass con Spring MVC»
(No se muestran 5 ediciones intermedias del mismo usuario) | |||
Línea 1: | Línea 1: | ||
− | [[ | + | [[Compass]], el mapeador de objetos para [[Apache Lucene]], contiene controladores para [[Spring MVC]] dos de ellos son "CompassSearchController" y "CompassIndexController" los cuales permiten realizar búsquedas paginadas y re-indexar mediante compassGps respectivamente. |
− | [[ | ||
− | |||
− | |||
== Ejemplo == | == Ejemplo == | ||
Línea 8: | Línea 5: | ||
=== Entidad anotada con Compass y Hibernate === | === Entidad anotada con Compass y Hibernate === | ||
− | <code> | + | <code java5> |
import java.io.Serializable; | import java.io.Serializable; | ||
import javax.persistence.Column; | import javax.persistence.Column; | ||
Línea 95: | Línea 92: | ||
=== Archivo applicationContext.xml (Este archivo se referencia en el web.xml) === | === Archivo applicationContext.xml (Este archivo se referencia en el web.xml) === | ||
− | <code> | + | <code xml> |
<?xml version="1.0" encoding="UTF-8"?> | <?xml version="1.0" encoding="UTF-8"?> | ||
<beans xmlns="http://www.springframework.org/schema/beans" | <beans xmlns="http://www.springframework.org/schema/beans" | ||
Línea 173: | Línea 170: | ||
=== archivo dispatcher-servlet.xml === | === archivo dispatcher-servlet.xml === | ||
− | <code> | + | <code xml> |
<?xml version="1.0" encoding="UTF-8"?> | <?xml version="1.0" encoding="UTF-8"?> | ||
<beans xmlns="http://www.springframework.org/schema/beans" | <beans xmlns="http://www.springframework.org/schema/beans" | ||
Línea 260: | Línea 257: | ||
=== Archivo view.properties === | === Archivo view.properties === | ||
− | <code> | + | <code ini> |
searchView.(class)=org.springframework.web.servlet.view.JstlView | searchView.(class)=org.springframework.web.servlet.view.JstlView | ||
searchView.url=/WEB-INF/jsp/search.jsp | searchView.url=/WEB-INF/jsp/search.jsp | ||
Línea 276: | Línea 273: | ||
=== Jsp busquedas === | === Jsp busquedas === | ||
− | <code> | + | <code html4strict> |
<%@page contentType="text/html" pageEncoding="UTF-8"%> | <%@page contentType="text/html" pageEncoding="UTF-8"%> | ||
Línea 353: | Línea 350: | ||
=== Jsp idexación === | === Jsp idexación === | ||
− | <code> | + | <code html4strict> |
<%@page contentType="text/html" pageEncoding="UTF-8"%> | <%@page contentType="text/html" pageEncoding="UTF-8"%> | ||
Línea 364: | Línea 361: | ||
<head> | <head> | ||
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> | <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> | ||
− | <title> | + | <title>Indexación</title> |
</head> | </head> | ||
Línea 380: | Línea 377: | ||
</FORM> | </FORM> | ||
<c:if test="${! empty indexResults}"> | <c:if test="${! empty indexResults}"> | ||
− | <P> | + | <P>Tiempo de indexación: <c:out value="${indexResults.indexTime}" />ms. |
</c:if> | </c:if> | ||
<P> | <P> | ||
Línea 386: | Línea 383: | ||
</body> | </body> | ||
</html> | </html> | ||
+ | </code> | ||
+ | |||
+ | ==Ver también== | ||
+ | * [[Compass]] | ||
+ | * [http://www.compass-project.org/docs/2.2.0/reference/html/ documentación] | ||
+ | |||
+ | [[Category:Compass]] | ||
+ | [[Category:Spring Framework]] |
Revisión actual del 15:05 24 ene 2011
Compass, el mapeador de objetos para Apache Lucene, contiene controladores para Spring MVC dos de ellos son "CompassSearchController" y "CompassIndexController" los cuales permiten realizar búsquedas paginadas y re-indexar mediante compassGps respectivamente.
Contenido
[ocultar]Ejemplo
Entidad anotada con Compass y Hibernate
import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import org.compass.annotations.Index;
import org.compass.annotations.Searchable;
import org.compass.annotations.SearchableId;
import org.compass.annotations.SearchableProperty;
@Searchable
@Entity
@Table(name = "PLANETA")
public class Planeta implements Serializable {
@SearchableId
@Id
@Column(name = "id_planeta")
private int codigo;
@SearchableProperty(index = Index.TOKENIZED)
@Column(name = "nombre")
private String nombre;
@SearchableProperty
@Column(name = "diametro")
private long diametro;
@SearchableProperty(index = Index.TOKENIZED)
@Column(name = "tipo")
private String tipo;
@SearchableProperty(index = Index.TOKENIZED)
@Column(name = "observaciones")
private String significado;
public int getCodigo() {
return codigo;
}
public void setCodigo(int codigo) {
this.codigo = codigo;
}
public long getDiametro() {
return diametro;
}
public void setDiametro(long diametro) {
this.diametro = diametro;
}
public String getNombre() {
return nombre;
}
public void setNombre(String nombre) {
this.nombre = nombre;
}
public String getSignificado() {
return significado;
}
public void setSignificado(String significado) {
this.significado = significado;
}
public String getTipo() {
return tipo;
}
public void setTipo(String tipo) {
this.tipo = tipo;
}
@Override
public String toString() {
String desc = "Planeta:" + getNombre();
desc += " - Diametro:" + getDiametro();
desc += " - Tipo:" + getTipo();
desc += " - Significado:" + getSignificado();
return desc;
}
}
Archivo applicationContext.xml (Este archivo se referencia en el web.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: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.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
<bean id="compassAnnotationConfiguration-admin" class="org.compass.annotations.config.CompassAnnotationsConfiguration"/>
<bean id="compass" class="org.compass.spring.LocalCompassBean">
<property name="classMappings">
<list>
<value>com.dosideas.compass.base.Planeta</value>
</list>
</property>
<property name="compassConfiguration" ref="compassAnnotationConfiguration-admin"/>
<property name="compassSettings">
<props>
<prop key="compass.engine.connection">file://D:/demo/</prop>
<prop key="compass.engine.optimizer.schedule.period">10</prop>
</props>
</property>
<property name="transactionManager" ref="transactionManager" />
</bean>
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="org.apache.derby.jdbc.ClientDriver"/>
<property name="url" value="jdbc:derby://localhost:1527/compass"/>
<property name="username" value="compass"/>
<property name="password" value="compass"/>
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource"/>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.DerbyDialect</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
<property name="annotatedClasses">
<list>
<value>com.dosideas.compass.base.Planeta</value>
</list>
</property>
</bean>
<bean id="compassGps" class="org.compass.gps.impl.SingleCompassGps"
init-method="start" destroy-method="stop" lazy-init="false">
<property name="compass" ref="compass" />
</bean>
</beans>
<code>
archivo dispatcher-servlet.xml
<code 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: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.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
<bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"/>
<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basename">
<value>messages</value>
</property>
</bean>
<bean id="viewResolver" class="org.springframework.web.servlet.view.ResourceBundleViewResolver">
<property name="basename">
<value>views</value>
</property>
</bean>
<bean id="exceptionResolver" class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
<property name="exceptionMappings">
<props>
<prop key="org.compass.core.CompassException">dataAccessFailure</prop>
<prop key="org.springframework.dao.DataAccessException">dataAccessFailure</prop>
<prop key="org.springframework.transaction.TransactionException">dataAccessFailure</prop>
</props>
</property>
</bean>
<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="/search.html">searchController</prop>
<prop key="/index.html">indexController</prop>
</props>
</property>
</bean>
<bean id="searchController" class="org.compass.spring.web.mvc.CompassSearchController">
<property name="compass" ref="compass"/>
<property name="searchView" value="searchView"/>
<property name="searchResultsView" value="searchResultsView"/>
<property name="pageSize" value="5"/>
</bean>
<bean id="indexController" class="org.compass.spring.web.mvc.CompassIndexController">
<property name="compassGps" ref="compassGps"/>
<property name="indexView" value="indexView"/>
<property name="indexResultsView" value="indexResultsView"/>
</bean>
</beans>
Archivo view.properties
searchView.(class)=org.springframework.web.servlet.view.JstlView
searchView.url=/WEB-INF/jsp/search.jsp
searchResultsView.(class)=org.springframework.web.servlet.view.JstlView
searchResultsView.url=/WEB-INF/jsp/search.jsp
indexView.(class)=org.springframework.web.servlet.view.JstlView
indexView.url=/WEB-INF/jsp/index.jsp
indexResultsView.(class)=org.springframework.web.servlet.view.JstlView
indexResultsView.url=/WEB-INF/jsp/index.jsp
Jsp busquedas
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core_rt" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jstl/fmt_rt" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Busqueda</title>
</head>
<body>
<FORM method="GET">
<spring:bind path="command.query">
<INPUT type="text" size="20" name="query" value="<c:out value="${status.value}"/>" />
</spring:bind>
<INPUT type = "submit" value="Search"/>
</FORM>
<c:if test="${! empty searchResults}">
Se obtuvieron <c:out value="${searchResults.totalHits}"/> resultados en <c:out value="${searchResults.searchTime}" /> ms
<c:forEach var="hit" items="${searchResults.hits}">
Alias <c:out value="${hit.alias}"/>
<c:choose>
<c:when test="${hit.alias == 'Planeta'}">
codigo: <c:out value="${hit.data.codigo}"/>
nombre: <c:out value="${hit.data.nombre}"/>
diametro: <c:out value="${hit.data.diametro}"/>
tipo: <c:out value="${hit.data.tipo}"/>
significado: <c:out value="${hit.data.significado}"/>
</c:when>
</c:choose>
</c:forEach>
<c:if test="${! empty searchResults.pages}">
<c:forEach var="page" items="${searchResults.pages}" varStatus="pagesStatus">
</c:forEach>
<c:choose>
<c:when test="${page.selected}">
<c:out value="${page.from}" />-<c:out value="${page.to}" />
</c:when>
<c:otherwise>
<FORM method="GET">
<spring:bind path="command.query">
<INPUT type="hidden" name="query" value="<c:out value="${status.value}"/>" />
</spring:bind>
<spring:bind path="command.page">
<INPUT type="hidden" name="page" value="<c:out value="${pagesStatus.index}"/>" />
</spring:bind>
<INPUT type = "submit" value="<c:out value="${page.from}" />-<c:out value="${page.to}" />"/>
</FORM>
</c:otherwise>
</c:choose>
</c:if>
</c:if>
</body>
</html>
</code>
Jsp idexación
<code html4strict>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core_rt" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jstl/fmt_rt" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Indexación</title>
</head>
<body>
<P>
Indice Compass
<P>
Mediante este boton se borra el indice actual y se regenera con los datos de la base
utilizando Compass::Gps.
<FORM method="POST" action="<c:url value="/index.html"/>">
<spring:bind path="command.doIndex">
<INPUT type="hidden" name="doIndex" value="true" />
</spring:bind>
<INPUT type="submit" value="Indexar"/>
</FORM>
<c:if test="${! empty indexResults}">
<P>Tiempo de indexación: <c:out value="${indexResults.indexTime}" />ms.
</c:if>
<P>
</body>
</html>
</code>
Ver también