gpt4 book ai didi

java - 无法使用 spring/hibernate 将两个实体注入(inject)到一个 jsp 中

转载 作者:行者123 更新时间:2023-12-02 10:19:41 25 4
gpt4 key购买 nike

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

感谢您的帮助

类别 Controller :

@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";
}
}

用户 Controller :

    @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>

调度程序-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>

最佳答案

当对类别/列表发出请求时,将调用 CategoryController 的categoriesList(Model theModel) 方法,并且在 Controller 返回到提到的 jsp 页面之前,它仅将所有类别添加到模型中。当 jsp 加载时,它只有类别列表数据,因为您没有在类别 Controller 方法的模型中包含用户数据。因此,用户类别为空。如果您想显示这两个列表,那么您需要将用户类别数据包含到类别 Controller 内的模型中。

关于java - 无法使用 spring/hibernate 将两个实体注入(inject)到一个 jsp 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54426937/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com