使用struts2中的Action类连接到couchDb

时间:2014-11-25 05:52:18

标签: struts2 couchdb

我试图使用struts2和沙发Db创建一个简单的登录页面作为database.i编写了一个代码,用于检查用户输入的值与数据库中的值。我尝试通过创建一个简单的java类并检索成功的文档来尝试连接到数据库。但是当我在Action类中放入相同的代码时,它会给出异常。 调试显示它在我调用'Session'对象的行中给出异常。 我的代码片段和堆栈跟踪如下所示。

package net.sarah.struts2;


import java.util.List;

import com.fourspaces.couchdb.Database;
import com.fourspaces.couchdb.Document;
import com.fourspaces.couchdb.Session;
import com.fourspaces.couchdb.ViewResults;


public class LoginAction {

private String username;
private String password;
public final String LOGIN_ID ="loginid";
public final String LOGIN_PWD ="passwd";

public String execute() {

    try{

    Session dbSession = new Session("localhost", 5984);
    String dbname = "hello-world";
    List <String> listofdb = dbSession.getDatabaseNames();
    System.out.println(listofdb);
    Database db = dbSession.getDatabase(dbname);

    ViewResults results = db.getAllDocuments();
//  System.out.println(results.toString());

    List<Document> studentDocuments = results.getResults();

    for(Document couchDocument: studentDocuments){

        String id = couchDocument.getJSONObject().getString("id");
        Document studentRow = db.getDocument(id);
        System.out.println(studentRow.get("_id"));
        if(studentRow.containsKey(LOGIN_ID)){
            if(LOGIN_ID.equals(username) && LOGIN_PWD.equals(password))

                return "success";

            else
                return "error";


        }
    }
    }
    catch(Exception e)
    {

        e.getCause();
    }
    return "result";
}

堆栈追踪 -

INFO:检测到AnnotationActionValidatorManager,初始化它...... 2014年11月25日上午10:55:03 org.apache.catalina.core.StandardWrapperValve调用 严重:带有路径[/ Demo]的上下文中servlet [default]的Servlet.service()引发了带有根本原因的异常[java.lang.reflect.InvocationTargetException] java.lang.ClassNotFoundException:com.fourspaces.couchdb.Session     在org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1714)     在org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1559)     在net.sarah.struts2.LoginAction.execute(LoginAction.java:23)     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)     at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)     at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)     at java.lang.reflect.Method.invoke(Unknown Source)     在com.opensymphony.xwork2.DefaultActionInvocation.invokeAction(DefaultActionInvocation.java:404)     在com.opensymphony.xwork2.DefaultActionInvocation.invokeActionOnly(DefaultActionInvocation.java:267)     在com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:229)     at com.opensymphony.xwork2.interceptor.DefaultWorkflowInterceptor.doIntercept(DefaultWorkflowInterceptor.java:221)     at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:86)     在com.opensymphony.xwork2.DefaultActionInvocation $ 2.doProfiling(DefaultActionInvocation.java:224)     在com.opensymphony.xwork2.DefaultActionInvocation $ 2.doProfiling(DefaultActionInvocation.java:223)     在com.opensymphony.xwork2.util.profiling.UtilTimerStack.profile(UtilTimerStack.java:455)     在com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:221)     在com.opensymphony.xwork2.validator.ValidationInterceptor.doIntercept(ValidationInterceptor.java:150)     at org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor.doIntercept(AnnotationValidationInterceptor.java:48)     at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:86)     在com.opensymphony.xwork2.DefaultActionInvocation $ 2.doProfiling(DefaultActionInvocation.java:224)     在com.opensymphony.xwork2.DefaultActionInvocation $ 2.doProfiling(DefaultActionInvocation.java:223)     在com.opensymphony.xwork2.util.profiling.UtilTimerStack.profile(UtilTimerStack.java:455)     在com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:221)     at com.opensymphony.xwork2.interceptor.ConversionErrorInterceptor.intercept(ConversionErrorInterceptor.java:123)     在com.opensymphony.xwork2.DefaultActionInvocation $ 2.doProfiling(DefaultActionInvocation.java:224)     在com.opensymphony.xwork2.DefaultActionInvocation $ 2.doProfiling(DefaultActionInvocation.java:223)     在com.opensymphony.xwork2.util.profiling.UtilTimerStack.profile(UtilTimerStack.java:455)     在com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:221)     at com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:184)     at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:86)     在com.opensymphony.xwork2.DefaultActionInvocation $ 2.doProfiling(DefaultActionInvocation.java:224)     在com.opensymphony.xwork2.DefaultActionInvocation $ 2.doProfiling(DefaultActionInvocation.java:223)     在com.opensymphony.xwork2.util.profiling.UtilTimerStack.profile(UtilTimerStack.java:455)     在com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:221)     at com.opensymphony.xwork2.interceptor.StaticParametersInterceptor.intercept(StaticParametersInterceptor.java:105)     在com.opensymphony.xwork2.DefaultActionInvocation $ 2.doProfiling(DefaultActionInvocation.java:224)     在com.opensymphony.xwork2.DefaultActionInvocation $ 2.doProfiling(DefaultActionInvocation.java:223)

1 个答案:

答案 0 :(得分:0)

确保您在web-inf/lib中拥有所有必需的库。

此资源可以帮助您Apache CouchDB