无法使用spring / hibernate将两个实体注入一个jsp

时间:2019-01-29 17:54:09

标签: java spring hibernate

我无法将来自两个存储库的数据正确地注入一种jsp形式。在index.jsp中,我只是“ <%response.sendRedirect(” category / list“);%>”当我将重定向与/ category / list一起使用时,它显示了我的类别表中的记录,并且应该还存在用户类别中的记录是空白空间。

感谢您的帮助

CategoryController:

@Controller
@RequestMapping("/category")
public class CategoryController {

   @Autowired
    private CategoryService categoryService;

    @GetMapping("/list")
    public String categoriesList(Model theModel){
        List<Category> allCategoriesFromDatabase = categoryService.getAllCategories();
        theModel.addAttribute("allCategoriesList" , allCategoriesFromDatabase);

        return "test-main-page";
    }
}

UsersController:

    @Controller
    @RequestMapping("/user")
    public class UserController {

        @Autowired
        private UserService userService;

        @GetMapping("/usersList")
        public String getUsersList(Model theModel){
            List<User> allUsersFromDatabase = userService.getAllUsers();
            theModel.addAttribute("allUsersList",allUsersFromDatabase);

            return "test-main-page";
        }

    }

test-main-page.jsp:

<%@ taglib prefix="mvc" uri="http://www.springframework.org/tags/form" %>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>

        <p>
            <table>
                <thead>
                    <tr>
                        <th>ID</th>
                        <th>Category name</th>
                        <th>isExpenditure</th>
                    </tr>
                </thead>
                <tbody
                    <c:forEach var="category" items="${allCategoriesList}" >
                        <tr>
                            <td>${category.id}</td>
                            <td>${category.categoryName}</td>
                            <td>${category.expenditure}</td>
                        </tr>
                    </c:forEach>
                </tbody>
            </table>
        </p>

        <br><br>

        <p>
            <table>
                <thead>
                <tr>
                    <th>ID</th>
                    <th>Login</th>
                    <th>Password</th>
                    <th>Email</th>
                    <th>JoinDate</th>
                </tr>
                </thead>
                <tbody
                <c:forEach var="users" items="${allUsersList}">
                    <tr>
                        <td>${users.id}</td>
                        <td>${users.login}</td>
                        <td>${users.password}</td>
                        <td>${users.email}</td>
                        <td>${users.userJoinDate}</td>
                    </tr>
                </c:forEach>
                </tbody>
            </table>
        </p>



</body>
</html>

dispatcher-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:context="http://www.springframework.org/schema/context"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd">

    <!-- Add support for component scanning -->
    <context:component-scan base-package="com.mybudget" />

    <!-- Add support for conversion, formatting and validation support -->
    <mvc:annotation-driven/>

    <!-- Define Spring MVC view resolver -->
    <bean
            class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/view/" />
        <property name="suffix" value=".jsp" />
    </bean>

    <!-- Step 1: Define Database DataSource / connection pool -->
    <bean id="myDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
    destroy-method="close">
    <property name="driverClass" value="com.mysql.jdbc.Driver" />
    <property name="jdbcUrl" value="jdbc:mysql://***useSSL=false&amp;serverTimezone=UTC" />
    <property name="user" value="***" />
    <property name="password" value="***" />

    <!-- these are connection pool properties for C3P0 -->
    <property name="minPoolSize" value="5" />
    <property name="maxPoolSize" value="20" />
    <property name="maxIdleTime" value="30000" />
</bean>

    <!-- Step 2: Setup Hibernate session factory -->
    <bean id="sessionFactory"
          class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
        <property name="dataSource" ref="myDataSource" />
        <property name="packagesToScan" value="com.mybudget.entity" />
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
                <prop key="hibernate.show_sql">true</prop>
            </props>
        </property>
    </bean>

    <!-- Step 3: Setup Hibernate transaction manager -->
    <bean id="myTransactionManager"
          class="org.springframework.orm.hibernate5.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory"/>
    </bean>

    <!-- Step 4: Enable configuration of transactional behavior based on annotations -->
    <tx:annotation-driven transaction-manager="myTransactionManager" />

    <mvc:resources location="WEB-INF/resources/" mapping="/resources/**"></mvc:resources>
</beans>

1 个答案:

答案 0 :(得分:1)

当请求类别/列表时,将调用CategoryController的categoryList(Model theModel)方法,并且仅将所有类别添加到模型中,然后控制器才能返回到提到的jsp页面。加载jsp时,它仅具有类别列表数据,因为您没有从类别控制器的方法中将用户数据包括在模型中。因此,用户类别为空白。如果要显示两个列表,则需要在“类别”控制器中将用户类别数据包括到模型中。