📄 searchcontroller.java
字号:
package net.java.workeffort.webapp.action;import java.lang.reflect.Method;import java.util.List;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import net.java.workeffort.service.domain.PageResult;import net.java.workeffort.service.domain.PaginationQuery;import net.java.workeffort.webapp.support.WebConstants;import org.apache.commons.lang.Validate;import org.springframework.validation.BindException;import org.springframework.web.servlet.ModelAndView;/** * A generic search controller for queries and pagination. * <p> * <b>Exposed configuration properties: (See BaseFormController also) </b> <br> * <table border="1"> * <tr> * <td><b>Name </b></td> * <td><b>Default </b></td> * <td><b>Description </b></td> * </tr> * <tr> * <td>service</td> * <td>null</td> * <td>The service. Required</td> * </tr> * <tr> * <td>formView</td> * <td>null</td> * <td>The initial query view. Required</td> * </tr> * <tr> * <td>successView</td> * <td>null</td> * <td>The success view which shows the search results. Generally a view which * supports pagination. Required</td> * </tr> * <tr> * <td>commandClass</td> * <td>null</td> * <td>The command class. Required</td> * </tr> * <tr> * <td>queryMethodName</td> * <td>null</td> * <td>The query method name in the service. Required</td> * </tr> * <tr> * <td>pageSize</td> * <td>10</td> * <td>The number of records to be displayed for pagination queries</td> * </tr> * </table> * @author Antony Joseph */public class SearchController extends BaseFormController { private Method queryMethod; private String queryMethodName; protected Integer pageSize; protected void init() { super.init(); Validate.notNull(service, "Service cannot be null"); Validate.notNull(getFormView(), "'formView' cannot be null"); Validate.notNull(getSuccessView(), "'successView' cannot be null"); Validate.notNull(getCommandClass(), "'commandClass' cannot be null"); Validate.notNull(queryMethodName, "'queryMethodName' cannot be null"); if (pageSize == null) pageSize = new Integer(10); // get the page result method. Should be available in the service queryMethod = getServiceMethod(service.getClass(), queryMethodName, new Class[] { getCommandClass() }); setValidateOnBinding(false); } public void setPageSize(Integer pageSize) { this.pageSize = pageSize; } public void setQueryMethodName(String queryMethodName) { this.queryMethodName = queryMethodName; } protected boolean isFormSubmission(HttpServletRequest request) { return QUERY.equals(request.getParameter(DISPATCH_PARAMETER_NAME)); } /** * Invokes the query method on the service. Makes the following attributes * available in the request scope for the jsps to render the result * appropriately: 'resultList' - The query result, 'pageSize' - The page * size, 'rowCount' - The total row count of the initial query. * @param request the request * @param response the response * @param command the query parameters * @param errors the bind exception * @return the model and view */ protected ModelAndView onSubmit(HttpServletRequest request, HttpServletResponse response, Object command, BindException errors) throws Exception { if (logger.isInfoEnabled()) logger.info("onSubmit invoked."); if (command instanceof PaginationQuery) return getPaginationResult(request, response, command, errors); else return getResult(request, response, command, errors); } protected ModelAndView getPaginationResult(HttpServletRequest request, HttpServletResponse response, Object command, BindException errors) throws Exception { PaginationQuery paginationQuery = (PaginationQuery) command; // take care of the info required by the datagrid. paginationQuery.setPageSize(this.pageSize); if (paginationQuery.getRowCount() == null) { paginationQuery.setPageIndex(new Integer(0)); } String pageIndexStr = request .getParameter(WebConstants.DATAGRID_PAGE_INDEX); if (pageIndexStr != null && pageIndexStr.length() > 0) paginationQuery.setPageIndex(new Integer(pageIndexStr)); // set the sortOrder. // Negative values or 0 mean ascending as per the Datagrid taglibs. // The dynamic SQL queries are written in such a way that if the value // for sortOrder is NOT NULL it means descending, otherwise, // the queries will default to ascending. String orderIndex = request .getParameter(WebConstants.DATAGRID_ORDER_INDEX); if (orderIndex != null && !orderIndex.equals("0")) { if (!(orderIndex.startsWith("-"))) { if (logger.isInfoEnabled()) logger.info("setting sortOrder to DESC"); paginationQuery.setSortOrder("DESC"); } } //PageResult result = (PageResult) queryMethod.invoke(service, // new Object[] { command }); PageResult result = (PageResult) query(request, command); // set result attributes so that the Jsps can find it. request.setAttribute("resultList", result.getRows()); request.setAttribute("pageSize", paginationQuery.getPageSize()); request.setAttribute("rowCount", result.getRowCount()); return new ModelAndView(getSuccessView()); } protected ModelAndView getResult(HttpServletRequest request, HttpServletResponse response, Object command, BindException errors) throws Exception { List resultList = (List) query(request, command); return new ModelAndView(getSuccessView(), "resultList", resultList); } /** * Invokes the query method on the service layer * @param request The request * @param command The query object * @return the page result * @throws Exception */ protected Object query(HttpServletRequest request, Object command) throws Exception { return queryMethod.invoke(service, new Object[] { command }); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -