相關配置以下html
JFinal Config配置java
@Override public void configPlugin(Plugins me) { // MongodbPlugin插件配置 MongodbPlugin mongodbPlugin = createdMongodbPlugin(); me.add(mongodbPlugin); } public static MongodbPlugin createdMongodbPlugin() { String host = "192.168.1.120"; int port = 27017; String database = "test"; String username = "mongo"; String password = "123456"; MongodbPlugin mongodbPlugin = new MongodbPlugin(host, port, database, username, password); return mongodbPlugin; }
MongodbPlugin插件mongodb
public class MongodbPlugin implements IPlugin { protected final Log logger = Log.getLog(getClass()); /** * 默認主機 */ private static final String DEFAULT_HOST = "127.0.0.1"; /** * 默認端口 */ private static final int DEFAUL_PORT = 27017; /** * MongoClient */ private MongoClient client; /** * 主機 */ private String host; /** * 端口 */ private int port; /** * 數據庫 */ private String database; /** * 用戶名 */ private String username; /** * 密碼 */ private String password; public MongodbPlugin(String database) { this.host = DEFAULT_HOST; this.port = DEFAUL_PORT; this.database = database; } public MongodbPlugin(String host, int port, String database, String username, String password) { this.host = host; this.port = port; this.database = database; this.username = username; this.password = password; } @Override public boolean start() { String url = "mongodb://{username}:{password}@{host}:{port}/{database}"; url = StringUtils.replaceEach(url, new String[]{"{username}","{password}","{host}","{port}","{database}"}, new String[]{username,password,host,String.valueOf(port),database}); client = new MongoClient((new MongoClientURI(url))); MongoKit.init(client, database); isStarted = true; return true; } @Override public boolean stop() { if (client != null) { client.close(); } return true; } }
Controller入口數據庫
public void list() { String name = getPara("name"); //設置排序 Map<String, Object> sort = new HashMap<String, Object>(); sort.put("createTime", "desc"); //設置查詢條件 Map<String, Object> like = new HashMap<String, Object>(); like.put("name", name); //分頁查詢 int pageNumber = getParaToInt(0, 1); Page<Record> page = MongoKit.paginate(Tables.d_tag.toString(), pageNumber, PAGE_SIZE, null, like, sort); //保存結果返回頁面 setAttr("page", page); render("list.html"); }
Exception信息session
2016-04-27 17:18:12 [ERROR]-[Thread: qtp250075633-23]-[com.jfinal.core.ActionHandler.handle()]: /tag/list com.mongodb.MongoSocketWriteException: Exception sending message at com.mongodb.connection.InternalStreamConnection.translateWriteException(InternalStreamConnection.java:462) at com.mongodb.connection.InternalStreamConnection.sendMessage(InternalStreamConnection.java:205) at com.mongodb.connection.UsageTrackingInternalConnection.sendMessage(UsageTrackingInternalConnection.java:95) at com.mongodb.connection.DefaultConnectionPool$PooledConnection.sendMessage(DefaultConnectionPool.java:424) at com.mongodb.connection.CommandProtocol.sendMessage(CommandProtocol.java:209) at com.mongodb.connection.CommandProtocol.execute(CommandProtocol.java:111) at com.mongodb.connection.DefaultServer$DefaultServerProtocolExecutor.execute(DefaultServer.java:159) at com.mongodb.connection.DefaultServerConnection.executeProtocol(DefaultServerConnection.java:286) at com.mongodb.connection.DefaultServerConnection.command(DefaultServerConnection.java:173) at com.mongodb.operation.CommandOperationHelper.executeWrappedCommandProtocol(CommandOperationHelper.java:215) at com.mongodb.operation.CommandOperationHelper.executeWrappedCommandProtocol(CommandOperationHelper.java:206) at com.mongodb.operation.CommandOperationHelper.executeWrappedCommandProtocol(CommandOperationHelper.java:112) at com.mongodb.operation.FindOperation$1.call(FindOperation.java:487) at com.mongodb.operation.FindOperation$1.call(FindOperation.java:482) at com.mongodb.operation.OperationHelper.withConnectionSource(OperationHelper.java:239) at com.mongodb.operation.OperationHelper.withConnection(OperationHelper.java:212) at com.mongodb.operation.FindOperation.execute(FindOperation.java:482) at com.mongodb.operation.FindOperation.execute(FindOperation.java:79) at com.mongodb.Mongo.execute(Mongo.java:772) at com.mongodb.Mongo$2.execute(Mongo.java:759) at com.mongodb.DBCursor.initializeCursor(DBCursor.java:851) at com.mongodb.DBCursor.hasNext(DBCursor.java:152) at com.jfinal.ext.plugin.monogodb.MongoKit.paginate(MongoKit.java:125) at com.mlh.buss.tag.TagController.list(TagController.java:44) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:497) at com.jfinal.aop.Invocation.invoke(Invocation.java:73) at com.mlh.interceptor.GlobalInterceptor.intercept(GlobalInterceptor.java:58) at com.jfinal.aop.Invocation.invoke(Invocation.java:67) at com.mlh.interceptor.ConditionInterceptor.doIntercept(ConditionInterceptor.java:31) at com.jfinal.aop.PrototypeInterceptor.intercept(PrototypeInterceptor.java:26) at com.jfinal.aop.Invocation.invoke(Invocation.java:67) at com.jfinal.core.ActionHandler.handle(ActionHandler.java:74) at com.mlh.handler.GlobalHandler.handle(GlobalHandler.java:25) at com.jfinal.ext.handler.ContextPathHandler.handle(ContextPathHandler.java:47) at com.jfinal.core.JFinalFilter.doFilter(JFinalFilter.java:72) at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1307) at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:453) at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:137) at org.eclipse.jetty.security.SecurityHandler.handle(SecurityHandler.java:560) at org.eclipse.jetty.server.session.SessionHandler.doHandle(SessionHandler.java:231) at org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1072) at org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:382) at org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:193) at org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:1006) at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:135) at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:116) at org.eclipse.jetty.server.Server.handle(Server.java:365) at org.eclipse.jetty.server.AbstractHttpConnection.handleRequest(AbstractHttpConnection.java:485) at org.eclipse.jetty.server.AbstractHttpConnection.headerComplete(AbstractHttpConnection.java:926) at org.eclipse.jetty.server.AbstractHttpConnection$RequestHandler.headerComplete(AbstractHttpConnection.java:988) at org.eclipse.jetty.http.HttpParser.parseNext(HttpParser.java:635) at org.eclipse.jetty.http.HttpParser.parseAvailable(HttpParser.java:235) at org.eclipse.jetty.server.AsyncHttpConnection.handle(AsyncHttpConnection.java:82) at org.eclipse.jetty.io.nio.SelectChannelEndPoint.handle(SelectChannelEndPoint.java:628) at org.eclipse.jetty.io.nio.SelectChannelEndPoint$1.run(SelectChannelEndPoint.java:52) at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:608) at org.eclipse.jetty.util.thread.QueuedThreadPool$3.run(QueuedThreadPool.java:543) at java.lang.Thread.run(Thread.java:745) Caused by: java.net.SocketException: Connection reset by peer: socket write error at java.net.SocketOutputStream.socketWrite0(Native Method) at java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:109) at java.net.SocketOutputStream.write(SocketOutputStream.java:153) at com.mongodb.connection.SocketStream.write(SocketStream.java:75) at com.mongodb.connection.InternalStreamConnection.sendMessage(InternalStreamConnection.java:201) ... 59 more