在spring,hibernate中尝试输出数据时不断出错

时间:2018-04-01 11:50:55

标签: java spring hibernate spring-mvc

你好我是hibernate的新手,我正在使用它与Spring框架,我得到一个错误,我无法弄明白。我认为它在我的服务或我的DAO文件

  

HTTP状态500 - 内部服务器错误类型异常报告

     

消息处理程序处理失败;嵌套异常是   java.lang.ExceptionInInitializerError

     

description服务器遇到阻止它的内部错误   满足要求。

     

例外

     

org.springframework.web.util.NestedServletException:处理程序   处理失败;嵌套异常是   java.lang.ExceptionInInitializerError     org.springframework.web.servlet.DispatcherServlet.triggerAfterCompletionWithError   (DispatcherServlet.java:1284)     org.springframework.web.servlet.DispatcherServlet.doDispatch   (DispatcherServlet.java:965)     org.springframework.web.servlet.DispatcherServlet.doService   (DispatcherServlet.java:876)     org.springframework.web.servlet.FrameworkServlet.processRequest   (FrameworkServlet.java:961)     org.springframework.web.servlet.FrameworkServlet.doGet   (FrameworkServlet.java:852)javax.servlet.http.HttpServlet.service   (HttpServlet.java:634)     org.springframework.web.servlet.FrameworkServlet.service   (FrameworkServlet.java:837)javax.servlet.http.HttpServlet.service   (HttpServlet.java:741)     org.apache.tomcat.websocket.server.WsFilter.doFilter   (WsFilter.java:53)母亲原因

     

java.lang.ExceptionInInitializerError util.HibernateUtil。   (HibernateUtil.java:29)DAO.RoomDAO.getAllroom(RoomDAO.java:19)     service.RoomService.getAllroom(RoomService.java:21)     controller.HomepageController.roomlist   (HomepageController.java:31)   我的dispatcher-servlet.xml

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.0.xsd
                http://www.springframework.org/schema/mvc
                http://www.springframework.org/schema/mvc/spring-mvc.xsd">

    <!-- declaring base package -->
    <context:annotation-config />
    <context:component-scan base-package="controller" />
     <context:component-scan base-package="org.jasig.portlet" />
    <!-- adding view resolver to show jsp's on browser -->
    <bean id="viewResolver"
          class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/" />
        <property name="suffix" value=".jsp" />
    </bean>
    <bean id="rDAO" class="DAO.RoomDAO" />
    <bean id="rService" class="service.RoomService" />
    <mvc:resources mapping="/resources/**" location="/resources/" cache-period="31556926"/>
    <mvc:annotation-driven />
</beans>

我的hibernate.cfg.xml

    <hibernate-configuration>   <session-factory>
        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/project</property>
        <property name="hibernate.connection.username">root</property>
        <property name="hibernate.connection.password">1234</property>
         <property name="show_sql">true</property>
        <mapping resource="model/User.hbm.xml"/>
        <mapping resource="model/Calendars.hbm.xml"/>
        <mapping resource="model/Notifications.hbm.xml"/>
        <mapping resource="model/Room.hbm.xml"/>
        <mapping resource="model/NotificationEvents.hbm.xml"/>
        <mapping resource="model/Events.hbm.xml"/>   </session-factory> </hibernate-configuration>

我的RoomDAO文件

> public class RoomDAO {
>     public ArrayList<Room> getAllroom(){
>      Session session = HibernateUtil.getSessionFactory().getCurrentSession();
>      Transaction transaction = session.beginTransaction();
>      Query query = (Query)session.createQuery("from room");
>      ArrayList<Room> list = (ArrayList<Room>) query.list();
>      transaction.commit();
>      return list; }
>     public static void main(String[] args) {
>         System.out.println(new RoomDAO().getAllroom().size());
>     } }

我的主页控制器

@Controller
@RequestMapping(value="/home")
public class HomepageController {
    @Autowired
    RoomService roomservice;

    @RequestMapping(value="/index")
    public String index(ModelMap mm){
        return "jsp/index";
    }

    @RequestMapping(value="/roomlist")
    public String roomlist(ModelMap mm){
        mm.put("listRoom",roomservice.getAllroom());
        return "jsp/roomlist";
    }
}

我的RoomService.java

public class RoomService {
    @Autowired
    RoomDAO RoomDAO;
    public ArrayList<Room> getAllroom(){
        return RoomDAO.getAllroom();
    }
}

我的web.xml

<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/applicationContext.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>2</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
    <welcome-file-list>
        <welcome-file>redirect.jsp</welcome-file>
    </welcome-file-list>
</web-app>

我的HibernateUtil.java

public class HibernateUtil {

    private static final SessionFactory sessionFactory;

    static {
        try {
            // Create the SessionFactory from standard (hibernate.cfg.xml) 
            // config file.
            sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();
        } catch (Throwable ex) {
            // Log the exception. 
            System.err.println("Initial SessionFactory creation failed." + ex);
            throw new ExceptionInInitializerError(ex);
        }
    }

    public static SessionFactory getSessionFactory() {
        return sessionFactory;
    }
}

我想在JSP文件中输出的任何数据

 <tr>
                  <c:forEach var="room" items="${listRoom}">
                    <th scope="row"><c:out value="${room.room_id}"/></th>
                      <td>
                        <button type="button" class="btn btn-primary" data-toggle="modal" data-target="#exampleModal">${room.room_name}
                        </button>
                      </td>
                      <td><c:out value="${room.address}"/></td>
                      <td><c:out value="${room.description}"/></td>
                      <td><c:out value="${room.device}"/></td>
                      <td>
                        <a href="add_room.html" title=""><label class="btn btn-xs btn-success margin-top-1"><span class="fa fa-plus-square" title="add"></span></label></a>
                        <a href="edit_room.html" title=""><label class="btn btn-xs btn-info margin-top-1"><span class="fa fa-pencil-square-o" title="edit"></span></label></a>
                        <label class="btn btn-xs btn-danger margin-top-1"><span class="fa fa-times" title="Cancel"></span></label>
                       </td>
                    </c:forEach>

                    </tr>

1 个答案:

答案 0 :(得分:0)

应用程序上下文初始化失败。这种失败有几个原因。请将代码推送到github并分享链接。

您是否在web.xml中配置了DispatcherServlet?

您是否在web.xml中为dispaycher-servlet xml文件添加了contextConfigLocation?

验证hibernate.cg.xml是否正在加载?