gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 03:03:57 25 4
gpt4 key购买 nike

大家好。我正在尝试从 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 Controller userController.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>

当我尝试将一些消息放入模型中时,它会显示出来。但对于属性“users”,什么也没有。请帮我。谢谢。

最佳答案

我找到了答案。我刚刚更换

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

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

而且它有效!

关于java - 使用jsp从数据库检索数据(Hibernate + Spring + Maven),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35259616/

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