Java 从jsp到Spring MVC控制器和DB获取值和对象
我正在开发一个SpringMVC应用程序来学习spring。My users.jsp是:Java 从jsp到Spring MVC控制器和DB获取值和对象,java,spring,hibernate,jsp,spring-mvc,Java,Spring,Hibernate,Jsp,Spring Mvc,我正在开发一个SpringMVC应用程序来学习spring。My users.jsp是: <body> <h1>Users</h1> <form:form modelAttribute="user" method="GET" > <table> <thead style="background: #fcf"> <tr> <th>ID</th>
<body>
<h1>Users</h1>
<form:form modelAttribute="user" method="GET" >
<table>
<thead style="background: #fcf">
<tr>
<th>ID</th>
<th>Name</th>
<th>User Id</th>
<th>Password</th>
<th>Email</th>
<th>Department</th>
<th>Role</th>
</tr>
</thead>
<c:forEach items="${userList}" var="user">
<tr>
<td>${user.id}</td>
<td>${user.name}</td>
<td>${user.userId}</td>
<td>${user.password}</td>
<td>${user.email}</td>
<td>${user.department}</td>
<td>${user.role}</td>
<td><a href="<c:url value='/user/edit/${user.id}' />">Edit</a></td>
<td><a href="<c:url value='/user/delete/${user.id}' />">Delete</a></td>
<td><a href="<c:url value='/user/update/${user.id}' />">Update</a></td>
</tr>
</c:forEach>
</table>
</form:form>
</body>
editingUser.jsp是:
<body>
<h2>User Update</h2>
<%=new java.util.Date()%>
<p>You are Updating a user with id : ${userAttribute.id}</p>
<c:url var="saveUrl" value="/user/edit?id=${userAttribute.id}" />
<form:form action="${saveUrl}" method="POST" modelAttribute="userAttribute">
<table>
<tr>
<td><form:label path="id">Id:</form:label></td>
<td><form:input path="id" disabled="true"/></td>
</tr>
<tr>
<td><form:label path="name">Name:</form:label></td>
<td><form:input path="name"/></td>
</tr>
<tr>
<td><form:label path="userId">User Id</form:label></td>
<td><form:input path="userId"/></td>
</tr>
<tr>
<td><form:label path="email">Email</form:label></td>
<td><form:input path="email"/></td>
</tr>
<tr>
<td><form:label path="password">Password</form:label></td>
<td><form:input path="password"/></td>
</tr>
</table>
<input type="submit" value="Update" />
</form:form>
</body>
我的ediedUser.jsp是
<body>
<h1>Updated User</h1>
<p>
You have edited/updated a user with id ${id} at
<%=new java.util.Date()%></p>
<h1>Persons</h1>
</body>
此外,我的数据库中的数据也没有更新
用于更新/保存etc的userDaoImpl是
public class UserDaoImpl implements UserDao {
// ***********************save User*****************
@Transactional
public boolean save(User user) {
// Retrieve session from Hibernate
Session session = sessionFactory.getCurrentSession();
// save
session.save(user);
return true;
}
// **************update/edit******************
@Transactional
public User update(User user) {
logger.info("Updating existing person");
// Retrieve session from Hibernate
Session session = this.sessionFactory.getCurrentSession();
// Retrieve existing person via id
User existingUser = (User) session.get(User.class, user.getId());
// Assign updated values to this person
existingUser.setName(user.getName());
existingUser.setName(user.getUserId());
existingUser.setEmail(user.getEmail());
existingUser.setRole(user.getRole());
existingUser.setDepartment(user.getDepartment());
// update User
session.update(existingUser);
// return to jsp
return existingUser;
}
// ***********find user by Id*************
public User findById(int id) {
// Retrieve session from Hibernate
Session session = this.sessionFactory.getCurrentSession();
// get user
User u = (User) session.get(User.class, new Integer(id));
return u;
}
// *************************List of User**************
@SuppressWarnings("unchecked")
@Transactional
public List<User> listPersons() {
Session session = this.sessionFactory.getCurrentSession();
List<User> personsList = session.createQuery("from User").list();
for (User u : personsList) {
logger.info("User List::" + u);
}
return personsList;
}
// *************************Delete a User**************
public User deleteUser(int id) {
logger.info(" Delete a User");
// Retrieve session from Hibernate
Session session = this.sessionFactory.getCurrentSession();
// Retrieve existing person first
User u = (User) session.load(User.class, id);
if(null != u) {
session.delete(u);
logger.info("Person deleted successfully, person details=" + u);
} else {
logger.info("user is not deleted successfully, person details=" + u);
}
return u;
}
public类UserDaoImpl实现UserDao{
//*************************保存用户*****************
@交易的
公共布尔保存(用户){
//从Hibernate检索会话
Session Session=sessionFactory.getCurrentSession();
//拯救
session.save(用户);
返回true;
}
//*************更新/编辑******************
@交易的
公共用户更新(用户){
logger.info(“更新现有人员”);
//从Hibernate检索会话
会话会话=this.sessionFactory.getCurrentSession();
//通过id检索现有人员
User existingUser=(User)session.get(User.class,User.getId());
//将更新的值分配给此人
existingUser.setName(user.getName());
existingUser.setName(user.getUserId());
existingUser.setEmail(user.getEmail());
existingUser.setRole(user.getRole());
existingUser.setDepartment(user.getDepartment());
//更新用户
会话更新(现有用户);
//返回到jsp
返回现有用户;
}
//**********按Id查找用户*************
公共用户findById(int-id){
//从Hibernate检索会话
会话会话=this.sessionFactory.getCurrentSession();
//获取用户
User u=(User)session.get(User.class,新整数(id));
返回u;
}
//*******************************用户列表**************
@抑制警告(“未选中”)
@交易的
公众人士名单{
会话会话=this.sessionFactory.getCurrentSession();
List personsList=session.createQuery(“来自用户”).List();
用于(用户u:PersonList){
logger.info(“用户列表::”+u);
}
返回人员列表;
}
//****************************删除用户**************
公共用户deleteUser(int-id){
logger.info(“删除用户”);
//从Hibernate检索会话
会话会话=this.sessionFactory.getCurrentSession();
//首先检索现有人员
用户u=(用户)会话.load(User.class,id);
如果(null!=u){
删除(u);
logger.info(“成功删除人员,人员详细信息=“+u”);
}否则{
logger.info(“用户未成功删除,人员详细信息=“+u”);
}
返回u;
}
请建议,因为我一直在使用@pathVariable和@requestParam,我猜或者其他东西不正确。谢谢
<body>
<h1>Updated User</h1>
<p>
You have edited/updated a user with id ${id} at
<%=new java.util.Date()%></p>
<h1>Persons</h1>
</body>
WARN : org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/UniProject08/user/edit] in DispatcherServlet with name 'SpringDispatcher'
public class UserDaoImpl implements UserDao {
// ***********************save User*****************
@Transactional
public boolean save(User user) {
// Retrieve session from Hibernate
Session session = sessionFactory.getCurrentSession();
// save
session.save(user);
return true;
}
// **************update/edit******************
@Transactional
public User update(User user) {
logger.info("Updating existing person");
// Retrieve session from Hibernate
Session session = this.sessionFactory.getCurrentSession();
// Retrieve existing person via id
User existingUser = (User) session.get(User.class, user.getId());
// Assign updated values to this person
existingUser.setName(user.getName());
existingUser.setName(user.getUserId());
existingUser.setEmail(user.getEmail());
existingUser.setRole(user.getRole());
existingUser.setDepartment(user.getDepartment());
// update User
session.update(existingUser);
// return to jsp
return existingUser;
}
// ***********find user by Id*************
public User findById(int id) {
// Retrieve session from Hibernate
Session session = this.sessionFactory.getCurrentSession();
// get user
User u = (User) session.get(User.class, new Integer(id));
return u;
}
// *************************List of User**************
@SuppressWarnings("unchecked")
@Transactional
public List<User> listPersons() {
Session session = this.sessionFactory.getCurrentSession();
List<User> personsList = session.createQuery("from User").list();
for (User u : personsList) {
logger.info("User List::" + u);
}
return personsList;
}
// *************************Delete a User**************
public User deleteUser(int id) {
logger.info(" Delete a User");
// Retrieve session from Hibernate
Session session = this.sessionFactory.getCurrentSession();
// Retrieve existing person first
User u = (User) session.load(User.class, id);
if(null != u) {
session.delete(u);
logger.info("Person deleted successfully, person details=" + u);
} else {
logger.info("user is not deleted successfully, person details=" + u);
}
return u;
}