search.jsp

来自「Jive是基于JSP/JAVA技术构架的一个大型BBS论坛系统,这是Jive论坛」· JSP 代码 · 共 699 行 · 第 1/2 页

JSP
699
字号
<%/** *	$RCSfile: search.jsp,v $ *	$Revision: 1.12 $ *	$Date: 2002/08/10 14:37:08 $ */%><%@ page import="java.util.*,                 com.jivesoftware.forum.*,                 com.jivesoftware.forum.util.*,                 java.text.DateFormat,                 java.text.SimpleDateFormat"    errorPage="error.jsp"%><%@ include file="global.jsp" %><%  // Put the request URI and query string in the session as an attribute.    // This is done so the error.jsp and auth.jsp pages can figure out what    // page sent it an error and redirect appropriately.    setRedirectURL(request);%><%! // global variables, methods, etc    static final int DEFAULT_RESULT_COUNT = 10;    static final int[] RESULT_COUNTS = { 10, 15, 30 };    static final DateFormat shortFormatter            = DateFormat.getDateInstance(DateFormat.MEDIUM, JiveGlobals.getLocale());    static final DateFormat shorterFormatter = new SimpleDateFormat("MMM yyyy");    private static String printForumParams(List forumIDs) {        StringBuffer buf = new StringBuffer(50);        for (int i=0; i<forumIDs.size(); i++) {            long id = ((Long)forumIDs.get(i)).longValue();            buf.append("forums=").append(id);            if ((i+1) < forumIDs.size()) {                buf.append("&");            }        }        return buf.toString();    }    private List getForumIDParams(HttpServletRequest request) {        List idList = new ArrayList(10);        String[] paramNames = new String[] {"forum", "forums"};        for (int j=0; j<paramNames.length; j++) {            String[] params = request.getParameterValues(paramNames[j]);            if (params != null) {                for (int i=0; i<params.length; i++) {                    try {                        // There might be "cidXXX" params here, they'll just fail                        long id = Long.parseLong(params[i]);                        idList.add(new Long(id));                    } catch (Exception parseFailed) {}                }            }        }        return idList;    }    private List getCategoryIDParams(HttpServletRequest request) {        List idList = new ArrayList(10);        // Loop through all parameters, look for ones starting with "cid":        for (Enumeration enum=request.getParameterNames(); enum.hasMoreElements(); )        {            String name = (String)enum.nextElement();            String value = request.getParameter(name);            if (value != null && value.startsWith("cid")) {                // Parse the id part of the string:                try {                    long id = Long.parseLong(value.substring(3, value.length()));                    idList.add(new Long(id));                }                catch (Exception parseFailed) {}            }            // Also check for the parameter name of "cat":            if ("cat".equals(name)) {                try {                    long id = Long.parseLong(value);                    idList.add(new Long(id));                }                catch (Exception parseFailed) {}            }        }        return idList;    }%><%  // Get parameters    // All forum IDs come in as "forums". If we're searching one forum, the    // parameter will be forums=X where X is the ID. If we search multiple,    // it'll be forums=X&forums=Y and the array below will have X and Y    // as its values.    List forumIDs = getForumIDParams(request);    // Get any category IDs passed in. Category ids can come from multiple    // parameters of "cat" or as "cidXXX" where "cid" denotes a category    // ID and XXX is the ID.    List categoryIDs = getCategoryIDParams(request);    // User is passed in by either user ID or username. We'll try to load the    // user either way.    long userID = ParamUtils.getLongParameter(request,"user",-1L);    String username = ParamUtils.getParameter(request,"user");    // Text of the query    String queryText = ParamUtils.getParameter(request,"q");    // Date to restrict our search. Can be either a long or a String. If it's    // a string, it'll be something like "yesterday" or "lastYear"    long date = ParamUtils.getLongParameter(request,"date",-1L);    String relDate = ParamUtils.getParameter(request,"date");    // Tells us to do a search    boolean search = ParamUtils.getBooleanParameter(request,"search");    // Starting point for search results and the number of search results per    // page.    int start = ParamUtils.getIntParameter(request,"start",0);    int range = ParamUtils.getIntParameter(request,"range",DEFAULT_RESULT_COUNT);    // Forum we're searching. This will remain null if we're searching all    // or a set of forums    Forum forum = null;    long forumID = -1L;    // Category we're searching. This will remain null if we're searching all    // or a set of categories    ForumCategory category = null;    long categoryID = -1L;    // The root category in the Jive system:    ForumCategory rootCategory = forumFactory.getRootForumCategory();    // Create the definitive list of forumIDs we're searching. If no forums    // were specified, the list size will be zero.    boolean isGlobalSearch = false;    boolean isSingleForumSearch = false;    Map forums = new HashMap();    // First, add any forumIDs in the forumIDs array to the Map of forums    for (int i=0; i<forumIDs.size(); i++) {        long id = ((Long)forumIDs.get(i)).longValue();        // if the forumID is -1, we're doing a global search so set the        // isGlobalSearch var to true and break        if (id == -1L) {            isGlobalSearch = true;            break;        }        try {            Forum f = forumFactory.getForum(id);            forums.put(new Long(f.getID()), f);        }        catch (Exception ignored) {}    }    // Add any forums that might be in a category:    for (int i=0; i<categoryIDs.size(); i++) {        long id = ((Long)categoryIDs.get(i)).longValue();        // If the categoryID is -1, we're searching all categories (all forums)        if (id == -1L) {            isGlobalSearch = true;            // Add all the forums to the forum map:            for (Iterator iter=rootCategory.recursiveForums(); iter.hasNext(); ) {                Forum f = (Forum)iter.next();                forums.put(new Long(f.getID()), f);            }            break;        }        else {            // This means we need to add all the forums in this category:            try {                ForumCategory c = forumFactory.getForumCategory(id);                for (Iterator iter=c.recursiveForums(); iter.hasNext(); ) {                    Forum f = (Forum)iter.next();                    forums.put(new Long(f.getID()), f);                }            }            catch (Exception ignored) {}        }    }    if (forums.size() == 0) {        isGlobalSearch = true;    }    // Check to see if we're only searching one forum:    if (!isGlobalSearch && forums.size() == 1) {        isSingleForumSearch = true;        for (Iterator iter=forums.values().iterator(); iter.hasNext();) {            forum = (Forum)iter.next();            forumID = forum.getID();            break;        }    }    // Check to see if we're only searching one category:    if (isGlobalSearch) {        categoryID = rootCategory.getID();    }    else if (categoryIDs.size() == 1) {        try {            categoryID = ((Long)categoryIDs.get(0)).longValue();        } catch (Exception ignored) {}    }    // Create the query object    com.jivesoftware.forum.Query query = null;    // If this is a global search, create the query object from the forumFactory    if (isGlobalSearch) {        query = forumFactory.createQuery();    }    // If we're searching a single forum, get the query from that forum    else if (isSingleForumSearch) {        query = forum.createQuery();    }    // Else, we're searching a set of forums, so create the query object from    // that set of forums.    else {        Forum[] forumSet = (Forum[])(forums.values().toArray(new Forum[]{}));        query = forumFactory.createQuery(forumSet);    }    // Try to load a user (try by userID first, then try username). If the user    // is loaded successfully, we'll add it to the query object so the results    // returned are messages posted by this user.    User user = null;    UserManager userManager = forumFactory.getUserManager();    try {        user = userManager.getUser(userID);    }    catch (Exception e) {        try {            user = userManager.getUser(username.trim());        }        catch (Exception ignored) {}    }    if (user != null) {        userID = user.getID();        username = user.getUsername();        query.filterOnUser(user);    }    // If search is true, and no query text or username was submitted, set    // search equal to false because we can't preform a search    if (search && queryText == null && user == null) {        search = false;    }    if (search) {        // Parse the date range.        if (date != -1L) {            query.setAfterDate(new Date(date));        }        else {            if ("any".equalsIgnoreCase(relDate)) {                relDate = null;            }            // Try to parse the relative date            if (relDate != null) {                Date searchStartDate = null;                Calendar calStart = Calendar.getInstance();                calStart.set(Calendar.HOUR_OF_DAY, 0);                calStart.set(Calendar.MINUTE, 0);                calStart.set(Calendar.SECOND, 0);                if ("yesterday".equalsIgnoreCase(relDate)) {                    calStart.add(Calendar.DAY_OF_YEAR, -1);                    searchStartDate = calStart.getTime();                }                else if ("7daysago".equalsIgnoreCase(relDate)) {                    calStart.add(Calendar.DAY_OF_YEAR, -7);                    searchStartDate = calStart.getTime();                }                else if ("30daysago".equalsIgnoreCase(relDate)) {                    calStart.add(Calendar.DAY_OF_YEAR, -30);                    searchStartDate = calStart.getTime();                }                else if ("90daysago".equalsIgnoreCase(relDate)) {                    calStart.add(Calendar.DAY_OF_YEAR, -90);                    searchStartDate = calStart.getTime();                }                else if ("thisyear".equalsIgnoreCase(relDate)) {                    calStart.set(Calendar.DAY_OF_YEAR, 1);                    searchStartDate = calStart.getTime();                }                else if ("2years".equalsIgnoreCase(relDate)) {                    calStart.set(Calendar.DAY_OF_YEAR, 1);                    calStart.add(Calendar.YEAR, -1);                    searchStartDate = calStart.getTime();                }                if (searchStartDate != null) {                    query.setAfterDate(searchStartDate);                }            }        }    }    String encodedQueryText = "";    if (queryText != null) {        encodedQueryText = StringUtils.encodeHex(queryText.getBytes("UTF-8"));    }%><%  String title = SkinUtils.getLocalizedString("skin.default.search.title",locale); %><%@ include file="header.jsp" %><table cellpadding="0" cellspacing="0" border="0" width="100%"><tr>    <td valign="top" width="98%">    <font face="<%= JiveGlobals.getJiveProperty("skin.default.fontFace") %>" color="<%= JiveGlobals.getJiveProperty("skin.default.linkColor") %>">    <b><%= SkinUtils.getLocalizedString("skin.default.global.search",locale) %></b>    </font>    <br>    <font size="<%= JiveGlobals.getJiveProperty("skin.default.fontSize") %>" face="<%= JiveGlobals.getJiveProperty("skin.default.fontFace") %>" color="<%= JiveGlobals.getJiveProperty("skin.default.linkColor") %>">    <b>    <a href="<%= JiveGlobals.getJiveProperty("skin.default.homeURL") %>"     ><%= SkinUtils.getLocalizedString("skin.default.global.home",locale) %></a>    &raquo;    <a href="index.jsp" class="header" title="<%= SkinUtils.getLocalizedString("skin.default.global.go_back_to_forum_list",locale) %>"    ><%= SkinUtils.getLocalizedString("skin.default.global.forums",locale) %></a><%  if (forum != null) { %>    &raquo;    <a href="forum.jsp?forum=<%= forum.getID() %>" class="header" title="<%= SkinUtils.getLocalizedString("skin.default.global.go_back_to_topic_list",locale) %>"    ><%= forum.getName() %></a><%  } else { %>    &raquo;    <a href="search.jsp?forums=-1" class="header"    ><%= SkinUtils.getLocalizedString("skin.default.search.search_all",locale) %></a><%  } %>    </b>    </font>    </td>    <td width="1%"><img src="images/blank.gif" width="10" height="1" border="0"></td>    <td valign="top" width="1%" align="center">    <%@ include file="loginbox.jsp" %>    </td></tr></table><form action="search.jsp" name="searchForm"><%  if (forum != null) { %><!--input type="hidden" name="forums" value="<%= forum.getID() %>"--><%  } %><input type="hidden" name="search" value="true"><table bgcolor="<%= JiveGlobals.getJiveProperty("skin.default.tableBorderColor") %>" cellpadding="0" cellspacing="0" border="0" width="450" align="center"><tr><td><table bgcolor="<%= JiveGlobals.getJiveProperty("skin.default.tableBorderColor") %>" cellpadding="3" cellspacing="1" border="0" width="100%"><tr bgcolor="#ffffff">    <td><font size="<%= JiveGlobals.getJiveProperty("skin.default.fontSize") %>"         face="<%= JiveGlobals.getJiveProperty("skin.default.fontFace") %>"><b><%= SkinUtils.getLocalizedString("skin.default.search.search_terms",locale) %><%= SkinUtils.getLocalizedString("skin.default.global.colon",locale) %></b></font></td>    <td><input type="text" name="q" style="width:100%;" value="<%= (queryText!=null)?(StringUtils.replace(queryText,"\"","&quot;")):"" %>"></td>

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?