我是春季休眠的初学者。需要在带有EL的jsp页面上简单地显示数据库中的用户,没有错误,应用程序启动,类映射到数据库但没有在jsp页面上显示,只有表“用户名”来自th。也许输入或anotations是错误的看不到,不知道。 MySQL数据库中的表是已有两个用户的用户。谢谢
用户jsp页面
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@include file="/WEB-INF/view/header.jsp" %>
<table class="table table-bordered table-hover table-striped">
<thead>
<tr>
<th>User Name</th>
</tr>
</thead>
<tbody>
<c:forEach items="${users}" var="user">
<tr>
<td>
<c:out value="${user.name}"/>
</td>
</tr>
</c:forEach>
</tbody>
</table>
<%@include file="/WEB-INF/view/footer.jsp" %>
控制器
@Controller
public class UserController {
@Autowired
private UserService userService;
@RequestMapping("/users")
public String users(Model model){
List<User> users = userService.getUserList();
model.addAttribute("users", users);
return "users";
}
}
userDAO的
@Transactional
@Repository
public class UserDao {
@Autowired
SessionFactory sessionFactory;
public List<User> getUserList() throws NullPointerException {
List<User> userList = null;
try {
Session session = sessionFactory.getCurrentSession();
String hql = "FROM com.hellospringdemo.model.User";
Query query = session.createQuery(hql);
userList = query.list();
session.flush();
} catch (Exception e) {
e.printStackTrace();
}
return userList;
}
}
Tried with this also same show's nothing...
Session session = sessionFactory.getCurrentSession();
List<User> result = session.createCriteria(User.class).list();
return result;
来自界面
的UserService
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserDao userDao;
public List<User> getUserList() {
return userDao.getUserList();
}
}
UserService接口
public interface UserService {
List<User> getUserList();
}
分派器
<context:component-scan base-package="com.hellospringdemo"/>
<mvc:annotation-driven/>
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/view/"/>
<property name="suffix" value=".jsp"/>
</bean>
AppContext.xml
<context:component-scan base-package="com.hellospringdemo"/>
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost/blog"/>
<property name="username" value="root"/>
<property name="password" value=""/>
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
</props>
</property>
<property name="packagesToScan">
<list>
<value>com.hellospringdemo</value>
</list>
</property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<tx:annotation-driven/>
<bean id="userDao" class="com.hellospringdemo.model.UserDao"/>
</beans>