getsearchresults.jsp
来自「实现一个网络购物」· JSP 代码 · 共 97 行
JSP
97 行
<%
boolean debug = false;
// *** calculations for search results ***
TreeSet searchResultSet = new TreeSet();
String searchResults = "";
if(!searchId.equals("")) {
// fill the result set with all the products that contain the searchterm
boolean firstTerm = true;
String productConstraint = "";
String articleConstraint = "";
String paragraphConstraint = "";
String searchString = searchId.toUpperCase() + " ";
String searchTerm = "";
TreeSet searchTermSet = new TreeSet();
while(!searchString.equals("")) {
if(searchString.substring(0,1).equals("\"")) { // string whithin " " ?
searchString = searchString.substring(1);
try{
searchTerm = searchString.substring(0,searchString.indexOf("\""));
searchString = searchString.substring(searchString.indexOf("\"")+1);
} catch (Exception e) { // no closing "
searchTerm = "";
}
} else { // take next word
searchTerm = searchString.substring(0,searchString.indexOf(" "));
searchString = searchString.substring(searchString.indexOf(" ")+1);
}
searchTerm = searchTerm.replace('-',' '); // for 'search-fast' search on the string 'search fast'
if(!searchTerm.equals("")){
if(!firstTerm) {
productConstraint += " AND ";
articleConstraint += " AND ";
paragraphConstraint += " AND ";
}
productConstraint += "( UPPER(products.title) LIKE '%" + searchTerm + "%'"
+ " OR UPPER(products.subtitle) LIKE '%" + searchTerm + "%'"
+ " OR UPPER(products.intro) LIKE '%" + searchTerm + "%'"
+ " OR UPPER(products.body) LIKE '%" + searchTerm + "%' )";
articleConstraint += "( UPPER(articles.title) LIKE '%" + searchTerm + "%'"
+ " OR UPPER(articles.intro) LIKE '%" + searchTerm + "%' )";
paragraphConstraint += "( UPPER(paragraphs.title) LIKE '%" + searchTerm + "%'"
+ " OR UPPER(paragraphs.body) LIKE '%" + searchTerm + "%' )";
searchTermSet.add(searchTerm);
firstTerm = false;
}
}
%><mm:list nodes="<%= searchResults %>" path="products" constraints="<%= productConstraint %>"
><mm:field name="products.number" jspvar="products_number" vartype="String" write="false"
><% searchResultSet.add(products_number);
%></mm:field
></mm:list
><mm:list nodes="<%= searchResults %>" path="products,posrel,articles" constraints="<%= articleConstraint %>"
><mm:field name="products.number" jspvar="products_number" vartype="String" write="false"
><% searchResultSet.add(products_number);
%></mm:field
></mm:list
><mm:list nodes="<%= searchResults %>" path="products,posrel,articles,posrel,paragraphs"
constraints="<%= paragraphConstraint %>"
><mm:field name="products.number" jspvar="products_number" vartype="String" write="false"
><% searchResultSet.add(products_number);
%></mm:field
></mm:list
><%
searchResults = searchResults(searchResultSet);
searchResultSet.clear();
if(debug) { %>text: <%= searchResults %><br><% }
}
if(!poolId.equals("")&&(searchId.equals("")||!searchResults.equals(""))) { // use productgroups to narrow down search results
%><mm:list nodes="<%= searchResults %>" path="products,posrel,productgroups"
constraints="<%= "productgroups.number = '" + poolId + "'" %>"
><mm:field name="products.number" jspvar="products_number" vartype="String" write="false"
><% searchResultSet.add(products_number);
%></mm:field
></mm:list><%
searchResults = searchResults(searchResultSet);
searchResultSet.clear();
if(debug) { %>pool: <%= searchResults %><br><% }
}
if(!keyId.equals("")&&((searchId.equals("")&&poolId.equals(""))||!searchResults.equals(""))) { // use keys to narrow down search results
%><mm:list nodes="<%= searchResults %>" path="products,posrel,keys"
constraints="<%= "keys.number = '" + keyId + "'" %>"
><mm:field name="products.number" jspvar="products_number" vartype="String" write="false"
><% searchResultSet.add(products_number);
%></mm:field
></mm:list><%
searchResults = searchResults(searchResultSet);
searchResultSet.clear();
if(debug) { %>key: <%= searchResults %><br><% }
}
%>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?