JSF应用于Google App Engine作为Android应用程序的后端。注入的对象为空

时间:2017-02-28 10:31:56

标签: android google-app-engine jsf servlets

我在Google App Engine上创建了一个JSF应用程序,为Android应用程序提供数据。我将使用JSF应用程序为Android应用程序准备数据并评估数据。我使用Objectify和Google Datastore来保持持久性。

现在我要迈出下一步,我遇到了麻烦:( 我需要以某种方式下载数据并将数据从数据存储上载到Android应用程序。我的第一个想法是在webapplication中使用额外的Servlet(而不是Faces Servlet),让servlet使用业务逻辑(cdi bean)。

调用servlet没有问题,但是当我注入在JSF应用程序中工作正常的cdi bean时,它们总是为空。注入在应用程序的每个其他部分都可以正常工作,但在额外的servlet(我称之为MobileServlet)中,注入的bean总是为空。

有什么建议吗?这是我的第一个JSF应用程序,所以我很可能误解了一些重要的东西;)

已编辑:我的一些文件:

的web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>GlideSpeedTester_cdi</display-name>
<welcome-file-list>
    <welcome-file>faces/login.xhtml</welcome-file>
</welcome-file-list>
<filter>
  <filter-name>ObjectifyFilter</filter-name>
    <filter-class>com.googlecode.objectify.ObjectifyFilter</filter-class>
  </filter>
<filter-mapping>
<filter-name>ObjectifyFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>
<context-param>
    <param-name>primefaces.THEME</param-name>
    <param-value>bluesky</param-value>
</context-param>
<context-param>
    <param-name>javax.faces.STATE_SAVING_METHOD</param-name>
    <param-value>server</param-value>
</context-param>
<context-param>
    <param-name>javax.faces.DEFAULT_SUFFIX</param-name>
    <param-value>.xhtml</param-value>
</context-param>
<context-param>
    <param-name>com.sun.faces.enableThreading</param-name>
    <param-value>false</param-value>
</context-param>
<context-param>
    <param-name>com.sun.faces.expressionFactory</param-name>
    <param-value>org.jboss.el.ExpressionFactoryImpl</param-value>
</context-param>
<session-config>
    <session-timeout>30</session-timeout>
</session-config>
<listener>
    <listener-class>com.sun.faces.config.ConfigureListener</listener-class>
</listener>
<listener>
    <listener-class>org.jboss.weld.environment.servlet.Listener</listener-class>
</listener>
<context-param>
    <param-name>javax.servlet.jsp.jstl.fmt.localizationContext</param-name>
    <param-value>resources.application</param-value>
  </context-param>
<servlet>
<servlet-name>Faces Servlet</servlet-name>
    <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>Faces Servlet</servlet-name>
    <url-pattern>/faces/*</url-pattern>
</servlet-mapping>
<servlet>
    <description></description>
    <display-name>MobileServlet</display-name>
    <servlet-name>MobileServlet</servlet-name>
    <servlet-class>se.widmantiming.glidespeedtester.servlets.MobileServlet</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>MobileServlet</servlet-name>
    <url-pattern>/MobileServlet</url-pattern>
</servlet-mapping>
</web-app>

beans.xml(在WebContent文件夹中,与web.xml相同)

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation=" 
    http://java.sun.com/xml/ns/javaee 
    http://java.sun.com/xml/ns/javaee/beans_1_0.xsd">
</beans> 

有效的注射:

package se.widmantiming.glidespeedtester.beans;

import javax.enterprise.context.RequestScoped;
import javax.inject.Inject;
import javax.inject.Named;

import se.widmantiming.glidespeedtester.dataservices.LoginService;
import se.widmantiming.glidespeedtester.entities.User;

@Named
@RequestScoped
public class LoginBean {

    @Inject
    private SessionBean sessionBean;

    private String mUsername;
    private String mPassword;
    private String mConfirmedPassword;
    private String mFirstName;
    private String mLastName;
    private String mEmail;
    private String mTestTeamPassword;


    @Inject
    private LoginService loginService;

    public String login() {
        User user = new User();
        user.setUsername(mUsername);
        user.setPassword(mPassword);

        return login(user);

    }

    private String login(User user) {
        User u = loginService.login(user);
        if (u == null) {
            return "login";
        }
        sessionBean.setUser(u);
        return "main";
    }

    public String createAccount() {
        if (!mPassword.equals(mConfirmedPassword)) {
            return "create_account";
        }
        User user = new User();
        user.setUsername(mUsername);
        user.setPassword(mPassword);
        user.setFirstName(mFirstName);
        user.setLastName(mLastName);
        user.setEmail(mEmail);
        user.setActive(true);
        User u = loginService.createAccount(user, mTestTeamPassword);
        if (u == null) {
            return "create_account";
        }
        return "login";
    }


    public String logout() {
        sessionBean.setUser(null);
        return "login";
    }

    public boolean isLoggedIn() {
        return (sessionBean.getUser() != null);
    }

    public SessionBean getSessionBean() {
        return sessionBean;
    }

    public void setSessionBean(SessionBean sessionBean) {
        this.sessionBean = sessionBean;
    }

    public String getUsername() {
        return mUsername;
    }

    public void setUsername(String username) {
        mUsername = username;
    }

    public String getPassword() {
        return mPassword;
    }

    public void setPassword(String password) {
        mPassword = password;
    }

    public String getConfirmedPassword() {
        return mConfirmedPassword;
    }

    public void setConfirmedPassword(String confirmedPassword) {
        mConfirmedPassword = confirmedPassword;
    }

    public String getFirstName() {
        return mFirstName;
    }

    public void setFirstName(String firstName) {
        mFirstName = firstName;
    }

    public String getLastName() {
        return mLastName;
    }

    public void setLastName(String lastName) {
        mLastName = lastName;
    }

    public String getEmail() {
        return mEmail;
    }

    public void setEmail(String email) {
        mEmail = email;
    }

    public String getTestTeamPassword() {
        return mTestTeamPassword;
    }

    public void setTestTeamPassword(String testTeamPassword) {
        mTestTeamPassword = testTeamPassword;
    }

    public LoginService getLoginService() {
        return loginService;
    }

    public void setLoginService(LoginService loginService) {
        this.loginService = loginService;
    }

}

不起作用的注射:

package se.widmantiming.glidespeedtester.servlets;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.inject.Inject;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import se.widmantiming.glidespeedtester.beans.MobileBean;
import se.widmantiming.glidespeedtester.entities.GlideWaxType;
import se.widmantiming.glidespeedtester.entities.User;

public class MobileServlet extends HttpServlet {
private static final long serialVersionUID = 1L;

// Injecting just to see if it is null
@Inject
private loginBean loginBean;

private String message = "Hello";

public void init() throws ServletException {

}

public void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {


    if (loginBean == null) {
        message = "Bean is null";
    }
    response.setContentType("text/html");
    PrintWriter out = response.getWriter();
    out.println("<h1>" + message + "</h1>");
}

public void destroy() {

}

public MobileBean getMobileBean() {
    return mobileBean;
}

public void setMobileBean(MobileBean mobileBean) {
    this.mobileBean = mobileBean;
}

public String getMessage() {
    return message;
}

public void setMessage(String message) {
    this.message = message;
}

public User getUser() {
    return mUser;
}

public void setUser(User user) {
    mUser = user;
}

}

0 个答案:

没有答案