UserController.java 代码:
package com.edw.controller; import java.io.IOException; import java.io.PrintWriter; import java.util.List; import javax.annotation.Resource; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.servlet.ModelAndView; import com.edw.entity.User; import com.edw.service.IUserManager; @Controller //类似Struts的Action @RequestMapping("/user") public class UserController { @Resource(name="userManager") // 获取spring配置文件中bean的id为userManager的,并注入 private IUserManager userManager; @RequestMapping("/addUser") // 请求url地址映射,类似Struts的action-mapping public String addUser(HttpServletRequest request){ System.out.println(request.getParameter("name").trim()); if(userManager.addUser(new User(request.getParameter("name").trim()))) { // 重定向 return "redirect:/user/getAllUser"; }else { return "/fail"; } } @RequestMapping("/updateUser") public String updateUser(User user,HttpServletRequest request){ //String result = "{\"result\":\"error\"}"; if (userManager.updateUser(user)) { user = userManager.getOneUser(user); request.setAttribute("user", user); //result = "{\"result\":\"success\"}"; return "/UserEdit"; }else { return "/fail"; } } @RequestMapping("/delUser") public void delUser(User user,HttpServletResponse response){ String result = "{\"result\":\"error\"}"; if(userManager.delUser(user)){ result = "{\"result\":\"success\"}"; } PrintWriter out = null; response.setContentType("application/json"); try { out = response.getWriter(); out.write(result); } catch (IOException e) { e.printStackTrace(); } } @RequestMapping("/toAddUser") public String toAddUser(){ return "/UserAdd"; } @RequestMapping("/toUpdateUser") public String toUpdateUser(User user,HttpServletRequest request){ User user1=userManager.getOneUser(user); request.setAttribute("user1", user1); return "/UserEdit"; } @RequestMapping("/getAllUser") public String getAllUser(HttpServletRequest request){ List userList=userManager.getAllUser(); request.setAttribute("userlist", userList); return "/UserMain"; } }
<pre name="code" class="java"><strong><span style="color:#ff0000;">UserDao.java</span> 代码:</strong>
<pre name="code" class="java">
package com.edw.dao; import java.util.List; import javax.annotation.Resource; import org.hibernate.Query; import org.hibernate.SessionFactory; import org.springframework.orm.hibernate3.support.HibernateDaoSupport; import org.springframework.stereotype.Repository; import com.edw.entity.User; @Repository public class UserDao implements IUserDao { private SessionFactory sessionFactory; @Resource public void setSessionFactory(SessionFactory sessionFactory) { this.sessionFactory = sessionFactory; } @Override public User getUser(long i) { String hql = "from User u where u.id=?"; Query query = sessionFactory.getCurrentSession().createQuery(hql); query.setLong(0, i); return (User) query.uniqueResult(); } @Override public List<User> getAllUser() { String hql = "from User"; Query query = sessionFactory.getCurrentSession().createQuery(hql); return query.list(); } @Override public boolean addUser(User user) { try{ sessionFactory.getCurrentSession().save(user); }catch(Exception e){ e.printStackTrace(); return false; } return true; } @Override public boolean delUser(long id) { String hql = "delete User u where u.id = ?"; Query query = sessionFactory.getCurrentSession().createQuery(hql); query.setLong(0, id); return (query.executeUpdate() > 0); } @Override public boolean updateUser(User user) { String hql = "update User u set u.name = ? where u.id = ?"; Query query = sessionFactory.getCurrentSession().createQuery(hql); query.setString(0, user.getName()); // query.setString(1, user.getAge()); query.setLong(1, user.getId()); return (query.executeUpdate() > 0); } }
User.java 代码:
package com.edw.entity; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.Table; @Entity @Table(name="t_user") public class User { private int id; private String name; public User(){ } public User(String name){ this.name = name; } @GeneratedValue @Id public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
UserManager.java 代码:
package com.edw.service; import java.util.List; import javax.annotation.Resource; import com.edw.dao.UserDao; import com.edw.entity.User; public class UserManager implements IUserManager{ private UserDao userDao; @Resource public void setUserDao(UserDao userDao){ this.userDao = userDao; } @Override public boolean updateUser(User user) { return userDao.updateUser(user); } @Override public User getOneUser(User user) { return userDao.getUser(user.getId()); } @Override public boolean delUser(User user) { return userDao.delUser(user.getId()); } @Override public boolean addUser(User user) { return userDao.addUser(user); } @Override public List getAllUser() { return userDao.getAllUser(); } }
jsp页面部分就不一一给出了,需要的亲可以去 http://download.csdn.net/detail/edwards_june/7876119 下载。。。
总结:
本人也是刚起步学SpringMvc,先前的SSH项目真心庞大,struts中包含了大量无关的jar包,springMVC中基本能胜任,也大大减轻了项目大小,不过也不能认为struts不好,毕竟任何事都有它好的一面,只在与我们在项目中需要什么和谁能提供较好的支持--权衡与折中.本次项目搭建中如有不足之处,希望大家可以提出一起学习,谢谢!!!