Web应用程序加载失败

时间:2013-09-09 05:56:34

标签: jsf-2 ejb datasource glassfish-3 java-ee-6

我正在使用Netbeans 7.2.1和GlassFish 3.1。

我使用JSF,ejb类和JDBC数据源创建了Web应用程序。 xhtml页面引用支持托管bean,它调用ejb类上的本地接口函数,这些类通过数据源运行查询,直接获取连接和执行查询。

项目构建成功,但是当我运行项目时,浏览器显示错误“没有收到数据”,浏览器选项卡标题显示无法加载。我想也许我有一些缺少的配置,因为当我运行相同的项目而没有引用托管bean(因此没有引用ejb和数据库)时,没有这样的消息。

坦率地说,我迷失在这样一个项目所需的配置文件中,以及在那里配置需要什么。我看到了许多解释,每个都说了别的,我不清楚这里有哪些相关。如果你能指出一些明确的相关解释,我将不胜感激。

我是否需要为此项目配置某个数据源? ejb课程?其他什么?

web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" 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/web-app_3_0.xsd">
<context-param>
    <param-name>javax.faces.PROJECT_STAGE</param-name>
    <param-value>Development</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>
<session-config>
    <session-timeout>
        30
    </session-timeout>
</session-config>
<welcome-file-list>
    <welcome-file>faces/index.xhtml</welcome-file>
</welcome-file-list>

beans.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>

index.xhtml:

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html lang="en"
  xmlns="http://www.w3.org/1999/xhtml"
  xmlns:h="http://java.sun.com/jsf/html"
  xmlns:f="http://java.sun.com/jsf/core">
<h:head>
    <title>Movie Tickets Order </title>
</h:head>
<h:body>
  <h:panelGrid columns="2" rendered="#{!UserBean.loggedIn}">

            <h:outputLabel for="username" value="Username:"></h:outputLabel>

            <h:inputText id="username" value="#{UserBean.username}"/>

            <h:outputLabel for="password" value="Password: "></h:outputLabel>

            <h:inputSecret id="password" value="#{UserBean.password}"/>

        </h:panelGrid>

        <h:commandButton value="Login" action="#{UserBean.login}" rendered="#{!UserBean.loggedIn}"/>

        <h:commandButton value="Logout" action="#{UserBean.logout}" rendered="#{UserBean.loggedIn}"/>

        <h:outputLink  value="EditMovie"  rendered="#{UserBean.isAdmin}"> Add/Edit Movie </h:outputLink>

</h:body>

的UserBean

import TicketsEJB.UserejbLocal;
import javax.inject.Named;
import javax.enterprise.context.SessionScoped;
import java.io.Serializable;
import javax.ejb.EJB;

@Named(value = "UserBean")
@SessionScoped
public class UserBean implements Serializable {
 private static final long serialVersionUID = 20130908L;
private String username;
private String password;
private String status;
private boolean exist = false;
private boolean loggedIn = false;
private final String statusAdmin = "admin";
private final String statusUser = "user";

@EJB
UserejbLocal userejb;

public boolean isAdmin() {
    return status.equals(statusAdmin);
}

public void setLoggedIn(boolean loggedIn) {
    this.loggedIn = loggedIn;
}

public boolean isLoggedIn() {
    return loggedIn;
}

public void login() {

    status = userejb.getUser(username, password);
    exist = (status == null) ? false : true;

    if (exist) {
        //render "Hello user"
        if (status.equals(statusAdmin)) {
            loggedIn=true;
            //render admin part: 
        }
    } else {
        //render "Sorry, wrong credentials"
    }
    password = null;
}
....

Userejb class:

@Stateful
@Local(UserejbLocal.class)
public class Userejb implements UserejbLocal {

private Connection connection = null;
private PreparedStatement getUser = null;
private PreparedStatement addUser = null;
private PreparedStatement getUserSalt = null;
private boolean exist;
private String status;
@Resource( name = "jdbc/Movies")
DataSource dataSource;


@PostConstruct
@Override
public void prepareStatements() {
    try {
        if (dataSource == null) { 
            throw new SQLException("Unable to obtain DataSource");
        }
        connection = dataSource.getConnection();  
        if (connection == null) { 
            throw new SQLException("Unable to connect to DataSource");
        }
        try {
            getUser = connection.prepareStatement( 
                    "SELECT STATUS  "
                    + "FROM Users"
                    + "WHERE NAMEU= ? and HASHP=?");
            addUser = connection.prepareStatement(
                    "insert into Users values ('?','?','?','?')");
            getUserSalt = connection.prepareStatement( 
                    "SELECT SALTP  "
                    + "FROM Users"
                    + "WHERE NAMEU= ? ");
        } catch (SQLException sqlException) {
            sqlException.printStackTrace();
            System.exit(1);
        }
    } catch (SQLException sqlException) {
        sqlException.printStackTrace();
        System.exit(1);
    }
}


    @Override
public void addUser(String name, String password, String status) {
    String salt = Security.salt();
    try {
        addUser.setString(1, name);
        addUser.setString(2, Security.hash(password + salt));
        addUser.setString(3, salt);
        addUser.setString(4, status);
        addUser.executeUpdate();
    } catch (SQLException ex) {
        Logger.getLogger(Userejb.class.getName()).log(Level.SEVERE, null, ex);
    }
}
....

UserejbLocal接口:

public interface UserejbLocal {
void prepareStatements();
void addUser(String name, String password, String status);
public java.lang.String getUser(java.lang.String name, java.lang.String password);
}

感谢您的帮助!

2 个答案:

答案 0 :(得分:0)

问题是SQLSyntaxErrorException。只需要查看服务器日志(输出选项卡,glassfish服务器选项卡)以查看问题所在。修复SQL语法正确地呈现了页面。

答案 1 :(得分:0)

您的查询中缺少UsersWHERE之间的空格。这导致查询被解析为:

SELECT STATUS  FROM UsersWHERE NAMEU ...............