使用jsp(Hibernate + Spring + Maven)从数据库中检索数据

时间:2016-02-07 21:57:49

标签: java spring hibernate maven jsp

你好,伙计们。我正在尝试从Oracle数据库中检索数据,但屏幕上没有任何内容。

这是我的usersList.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
    <title>Spring MVC Hello World</title>
</head>

<body>
    <h2>All Users in System</h2>

    <table border="1">
        <tr>
            <th>User Id</th>
            <th>Login</th>
        </tr>
        <c:forEach items="${users}" var="user">
            <tr>
                <td>${user.userId}</td>
                <td>${user.login}</td>
            </tr>
        </c:forEach>
    </table>
</body>
</html>

似乎没问题。 这里是Spring控制器usersController.java

package com.controller;
import com.database.Users;
import com.service.UsersManager;
import java.util.List;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

/**
 *
 * @author glebk
 */
@Controller
@RequestMapping("/users")
public class UsersController {

    private static final Logger log = Logger.getLogger(UsersController.class);

    @Autowired
    UsersManager manager;

    @RequestMapping(value = "/getUsers", method = RequestMethod.GET)
    public String getAllEmployees(Model model)
    {
        List<Users> users = manager.getAllUsers();
        model.addAttribute("users", users);
        return "usersList";
    }
}

我的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">
    <servlet> 
        <servlet-name>event</servlet-name> 
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
        <load-on-startup>1</load-on-startup> 
    </servlet> 
    <servlet-mapping> 
        <servlet-name>event</servlet-name> 
        <url-pattern>/</url-pattern> 
    </servlet-mapping>
</web-app>

event-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:tx="http://www.springframework.org/schema/tx"

       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
          http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
          http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd
          http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
">

    <tx:annotation-driven />
    <context:component-scan base-package="com" />

    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">  
        <property name="driverClassName"  value="oracle.jdbc.driver.OracleDriver"></property>  
        <property name="url" value="jdbc:oracle:thin:@localhost:1521:xe"></property>  
        <property name="username" value="admin"></property>  
        <property name="password" value="0000"></property>  
    </bean>

    <bean id="sessionFactory"
          class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="configLocation">
            <value>
                classpath:hibernate.cfg.xml
            </value>
        </property>
        <property name = "dataSource" ref = "dataSource"></property>
    </bean>

    <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />
    <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />

    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/views/" />
        <property name="suffix" value=".jsp" />
    </bean>

    <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory"/>
    </bean>
</beans>

当我尝试将一些消息放入我的模型时,会显示该消息。但是属性“用户”没有任何东西。 请帮我。 谢谢。

2 个答案:

答案 0 :(得分:2)

我找到了答案。我刚换了

<c:forEach items="${users}" var="user">
      <tr>
          <td>
              <c:out value="${user.userId}"/>
          </td>
          <td>
              <c:out value="${user.login}" />
         </td>
     </tr>
</c:forEach>

$docker run hello-world

它有效!

答案 1 :(得分:0)

尝试像这样写component-scan

   <context:component-scan base-package="com.controller" />