【问题标题】:keep getting error when trying to output data in spring,hibernate尝试在spring,hibernate中输出数据时不断出错
【发布时间】:2018-04-01 11:50:54
【问题描述】:

您好,我目前是 hibernate 新手,我在 Spring 框架中使用它,我遇到了一个我无法弄清楚的错误。我认为它在我的服务或我的 DAO 文件中

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

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

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

异常

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 (主页控制器.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>

【问题讨论】:

    标签: java spring hibernate spring-mvc


    【解决方案1】:

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

    你在 web.xml 中配置 DispatcherServlet 了吗?

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

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

    【讨论】:

    • 是的,我做了 dispatcherorg.springframework.web.servlet.DispatcherServlet2 dispatcher/
    • hibernate.cfg.xml 也在运行,因为我只运行了模板并且它有效。当我尝试从数据库输出数据时出现错误
    猜你喜欢
    • 1970-01-01
    • 2021-07-22
    • 2018-05-09
    • 2015-04-13
    • 1970-01-01
    • 2021-12-30
    • 2016-03-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多