diff --git a/1_Web_System/WebRoot/index.jsp b/1_Web_System/WebRoot/Page/main.jsp similarity index 67% rename from 1_Web_System/WebRoot/index.jsp rename to 1_Web_System/WebRoot/Page/main.jsp index ee20dc3..986d7da 100644 --- a/1_Web_System/WebRoot/index.jsp +++ b/1_Web_System/WebRoot/Page/main.jsp @@ -1,4 +1,4 @@ -<%@ page language="java" import="java.util.*,dao.HibernateDao" pageEncoding="US-ASCII"%> +<%@ page language="java" import="java.util.*" pageEncoding="US-ASCII"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; @@ -9,7 +9,8 @@ String basePath = request.getScheme()+"://"+request.getServerName()+":"+request. - My JSP 'index.jsp' starting page + My JSP 'Main.jsp' starting page + @@ -18,11 +19,15 @@ String basePath = request.getScheme()+"://"+request.getServerName()+":"+request. + - - - This is my JSP page.
- - ${message } +
+
+ Welcome to my Zone
+
+ Login +

+ Register +
diff --git a/1_Web_System/WebRoot/WEB-INF/dispatcher-servlet.xml b/1_Web_System/WebRoot/WEB-INF/dispatcher-servlet.xml index 001158a..ff646e3 100644 --- a/1_Web_System/WebRoot/WEB-INF/dispatcher-servlet.xml +++ b/1_Web_System/WebRoot/WEB-INF/dispatcher-servlet.xml @@ -7,7 +7,9 @@ http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> - + + + diff --git a/1_Web_System/WebRoot/WEB-INF/web.xml b/1_Web_System/WebRoot/WEB-INF/web.xml index 4aef5bf..8a9268f 100644 --- a/1_Web_System/WebRoot/WEB-INF/web.xml +++ b/1_Web_System/WebRoot/WEB-INF/web.xml @@ -1,6 +1,11 @@ - + + Page/main.jsp + + + + Archetype Created Web Application @@ -21,7 +26,7 @@ contextConfigLocation - /WEB-INF/dispatcher-servlet.xml + /WEB-INF/dispatcher-servlet.xml @@ -29,4 +34,7 @@ org.springframework.web.context.ContextLoaderListener + + + \ No newline at end of file diff --git a/1_Web_System/WebRoot/editUserProfile.jsp b/1_Web_System/WebRoot/editUserProfile.jsp new file mode 100644 index 0000000..e43cc97 --- /dev/null +++ b/1_Web_System/WebRoot/editUserProfile.jsp @@ -0,0 +1,37 @@ +<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> +<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form" %> +<%@ page language="java" import="java.util.*" pageEncoding="US-ASCII"%> +<% +String path = request.getContextPath(); +String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; +%> + + + + + + + Edit + + + + + + + + + + + + + + Username:
+ Password:
+ Age:
+ Email:

+ +
+ + diff --git a/1_Web_System/WebRoot/login(old).jsp b/1_Web_System/WebRoot/login(old).jsp new file mode 100644 index 0000000..e83faba --- /dev/null +++ b/1_Web_System/WebRoot/login(old).jsp @@ -0,0 +1,43 @@ +<%@ page language="java" import="java.util.*" pageEncoding="US-ASCII"%> +<% +String path = request.getContextPath(); +String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; +%> + + + + + + + My JSP 'helloworld.jsp' starting page + + + + + + + + + + + + +
Spring Test
+
+ UserName:
+ PassWord:
+ + +
+
+ +
+

+ ${ErrorMessage } +
+ + + + diff --git a/1_Web_System/WebRoot/login.jsp b/1_Web_System/WebRoot/login.jsp index 74abc52..61d0499 100644 --- a/1_Web_System/WebRoot/login.jsp +++ b/1_Web_System/WebRoot/login.jsp @@ -1,4 +1,6 @@ -<%@ page language="java" import="java.util.*" pageEncoding="US-ASCII"%> +<%@ page language="java" import="java.util.*,dao.HibernateDao" pageEncoding="US-ASCII"%> +<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%> + <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; @@ -9,8 +11,7 @@ String basePath = request.getScheme()+"://"+request.getServerName()+":"+request. - My JSP 'helloworld.jsp' starting page - + My JSP 'index.jsp' starting page @@ -19,18 +20,21 @@ String basePath = request.getScheme()+"://"+request.getServerName()+":"+request. - - +
- -
Spring Test
-
- UserName:
- -
-
- + Please Enter You UserName and PassWord
+ +
+
+ UserName:

+ PassWord:

+ + +
+ ${kj } + ${message } +
diff --git a/1_Web_System/WebRoot/register.jsp b/1_Web_System/WebRoot/register.jsp new file mode 100644 index 0000000..2755d37 --- /dev/null +++ b/1_Web_System/WebRoot/register.jsp @@ -0,0 +1,46 @@ +<%@ page language="java" import="java.util.*" pageEncoding="US-ASCII"%> +<% +String path = request.getContextPath(); +String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; +%> + + + + + + + My JSP 'register.jsp' starting page + + + + + + + + + + + +
+ + Please Fill The Form

+
+ + + + + + + + + + + +
UserName
PassWord
Age


+
+
+
+ + diff --git a/1_Web_System/WebRoot/testPage.jsp b/1_Web_System/WebRoot/testPage.jsp new file mode 100644 index 0000000..8641584 --- /dev/null +++ b/1_Web_System/WebRoot/testPage.jsp @@ -0,0 +1,57 @@ +<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> +<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form" %> +<%@ page language="java" import="java.util.*" pageEncoding="US-ASCII"%> +<% +String path = request.getContextPath(); +String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; +%> + + + + + + + My JSP 'testPage.jsp' starting page + + + + + + + + + + + + int binding
+
+ Int: + +
+ + + + Model Binding & @RequestParam +
+ Name: + Age: + + +
+ + + + ModelAtrribute Binding + + + Name: + Age: + + + + + + + diff --git a/1_Web_System/WebRoot/userProfile.jsp b/1_Web_System/WebRoot/userProfile.jsp new file mode 100644 index 0000000..675afb2 --- /dev/null +++ b/1_Web_System/WebRoot/userProfile.jsp @@ -0,0 +1,40 @@ +<%@ page language="java" import="java.util.*,model.User" pageEncoding="US-ASCII"%> +<% +String path = request.getContextPath(); +String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; +%> + + + + + + + My JSP 'userProfile.jsp' starting page + + + + + + + + + + + + + View UserProfile
+
+ Name: ${user.getName()}
+ Age: ${user.getAge()}
+ Date: ${user.getDate() }
+ Email: ${user.getEmail()}
+ Edit Profile
+ Back to HomePage + +
+ + + + diff --git a/1_Web_System/WebRoot/wel.jsp b/1_Web_System/WebRoot/wel.jsp new file mode 100644 index 0000000..4b1fb13 --- /dev/null +++ b/1_Web_System/WebRoot/wel.jsp @@ -0,0 +1,183 @@ +<%@ page language="java" import="java.util.*,dao.HibernateDao,model.User" pageEncoding="US-ASCII"%> +<% +String path = request.getContextPath(); +String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; +%> + + + + + + + My JSP 'wel.jsp' starting page + + + + + + + + + + + +
+ Welcome
+ + <% + HttpSession hs=request.getSession(); + String username=(String)hs.getAttribute("user"); + + if(username!=null){ + out.println("Welcome you: "+username); + } + + + %>
+ + ${statue }

+ + Username:${username }
+ + Age:${age }
+
+ Back + + + + + <% + int pageSize = 3; + int pageNow = 1; // default 1st Page + int rowCount = 0; + int pageCount = 0; + + //process pageNow + //fetch user expert pagenow + + String s_pageNow = request.getParameter("pageNow"); + + if (s_pageNow != null) { + // pageNow not null + pageNow = Integer.parseInt(s_pageNow); + + } + + HibernateDao hibernateDao = new HibernateDao(); + + pageCount = hibernateDao.SearchPageCount(pageSize); + %> + + + + + + + + + + + <% + + %> + + <% + ArrayList al = (ArrayList) hibernateDao.searchPage(pageNow, + pageSize); + for (int i = 0; i < al.size(); i++) { + User user = (User) al.get(i); + %> + + + + + + + + <% + } + %> + +
idnamepwagedate
<%=user.getId()%><%=user.getName()%><%=user.getPw()%><%=user.getAge()%><%=user.getDate()%>${userList.get(i).getName()}
+ + <% + for (int i = 1; i <= pageCount; i++) { + + out.println("[" + i + "]"); + + } + %> +
+
+
+ + + Spring MVC table:
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Name:Age:Date:Email:Setting:
${userList.get(0).getName()}${userList.get(0).getAge()}${userList.get(0).getDate()}${userList.get(0).getEmail()}Edit
${userList.get(1).getName()}${userList.get(1).getAge()}${userList.get(1).getDate()}${userList.get(1).getEmail()}Edit
${userList.get(2).getName()}${userList.get(2).getAge()}${userList.get(2).getDate()}${userList.get(2).getEmail()}Edit
+ <% + for (int i = 1; i <= pageCount; i++) { + + out.println("[" + i + "]"); + + } + %> + + + + + + + + +
+ + +







+
+ Search User: + +
+


+ + + + + diff --git a/1_Web_System/src/controller/LoginProcessController.java b/1_Web_System/src/controller/LoginProcessController.java new file mode 100644 index 0000000..1891e1f --- /dev/null +++ b/1_Web_System/src/controller/LoginProcessController.java @@ -0,0 +1,218 @@ +package controller; + +import java.util.Date; +import java.util.LinkedList; +import java.util.List; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpSession; + +import model.User; +import model.UserBeanProcess; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.ModelAttribute; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.servlet.ModelAndView; + +/** + * + * @author suzhantao + * + */ +@Controller +public class LoginProcessController { + + @Autowired + UserBeanProcess ubp; + + ModelAndView mv = new ModelAndView(); + + /** + * + * @param username + * @param password + * @return + */ + @RequestMapping(value = "loginValidation", method = RequestMethod.POST) + public String LoginValidation( + @RequestParam(value = "username") String username, + @RequestParam(value = "password") String password) { + String url = null; + if (ubp.ValidateUser(username, password)) { + System.out.println("loginValidation hit"); + return "redirect:retrieveUser"; + + } else { + url = "index"; + } + + return url; + } + + /** + * + * @param request + * @param username + * @param password + * @return + */ + @RequestMapping(value = "login", method = RequestMethod.POST) + public ModelAndView Login(HttpServletRequest request, + @RequestParam(value = "username") String username, + @RequestParam(value = "password") String password) { + if (ubp.ValidateUser(username, password)) { + mv = new ModelAndView("wel"); + + HttpSession hs = request.getSession(); + hs.setAttribute("user", username); + } else { + mv = new ModelAndView("login"); + mv.addObject("ErrorMessage", "invalid user,please try again"); + } + + return mv; + } + + @RequestMapping(value = "addUser", method = RequestMethod.POST) + public ModelAndView AddUser(HttpServletRequest request, + @RequestParam(value = "username") String username, + @RequestParam(value = "password") String password, + @RequestParam(value = "age") int age) { + + if (ubp.AddUser(username, password, age)) { + mv = new ModelAndView("redirect:retrieveUser"); + mv.addObject("username", username); + mv.addObject("password", password); + mv.addObject("age", age); + mv.addObject("statue", "Create User Successful"); + + } else { + mv = new ModelAndView("register"); + mv.addObject("statue", "Create User Failue"); + } + + return mv; + + } + + /** + * call from GetUser + * + * @param username + * @return + */ + @RequestMapping(value = "user/{username}", method = RequestMethod.GET) + public ModelAndView GetUserProfile(@PathVariable String username) { + + mv = new ModelAndView("userProfile"); + + User user = new User(); + + user = ubp.GetUserProfile(username); + + + + mv.addObject("user", user); + + return mv; + + } + + /** + * call GetUserProfile ,path username to user/{username} + * + * @param username + * @return + */ + @RequestMapping(value = "getUser", method = RequestMethod.GET) + public String GetUser(@RequestParam(value = "username") String username) { + + return "redirect:user/" + username; + + } + + /** + * + * @param pageNow + * @param pageSize + * @return + */ + @RequestMapping(value = "retrieveUser", method = RequestMethod.GET) + public ModelAndView RetrieveUser(HttpServletRequest request) { + String s_pageNow = request.getParameter("pageNow"); + System.out.println("s_pageNow:" + s_pageNow); + + int pageNow = 1; + if (s_pageNow != null) { + pageNow = Integer.parseInt(s_pageNow); + } else { + pageNow = 1; + } + + int pageSize = 3; + + System.out.println("retrieveUser"); + + List list = new LinkedList(); + + list = ubp.RetrieveUser(pageNow, pageSize); + + mv = new ModelAndView("wel"); + + System.out.println("row Count:"+list.size()); + + mv.addObject("userList", list); + + return mv; + + } + + /** + * EditUserprofile + * @param username + * @return + */ + @RequestMapping(value = "{username}/edit", method = RequestMethod.GET) + public ModelAndView EditUserProfile( + @PathVariable("username") String username) { + User user = new User(); + + user = ubp.GetUserProfile(username); + + mv = new ModelAndView("editUserProfile"); + mv.addObject("userProfile", user); + return mv; + } + + + //{userId} + /** + * Execute Update User + * @param user + * @return + */ + @RequestMapping(value="{name}",method=RequestMethod.POST) + public ModelAndView UpdateUser(User user){ + + Date date=new Date(); + user.setDate(date); + + System.out.println("email:"+user.getEmail()); + + + ubp.UpdateUser(user); + + + + mv=new ModelAndView("redirect:user/" + user.getName()); + + + return mv; + } + +} diff --git a/1_Web_System/src/dao/HibernateDao.java b/1_Web_System/src/dao/HibernateDao.java index 790fa4a..82465c0 100644 --- a/1_Web_System/src/dao/HibernateDao.java +++ b/1_Web_System/src/dao/HibernateDao.java @@ -12,19 +12,23 @@ import org.hibernate.cfg.Configuration; import org.hibernate.service.ServiceRegistry; import org.hibernate.service.ServiceRegistryBuilder; +import org.springframework.stereotype.Component; import org.springframework.stereotype.Repository; +@Component public class HibernateDao { private static SessionFactory sessionFactory; private static ServiceRegistry serviceRegistry; + private int pageCount; + private int pageSize; + /** * * @param username * @param password - * @return + * @return user */ - @SuppressWarnings("deprecation") public List searchUser(String username) { Configuration configuration = new Configuration(); configuration.configure(); @@ -43,12 +47,131 @@ public List searchUser(String username) { query.setString("name", username); List list = query.list(); - // List list = session.createQuery("from User").list(); - session.getTransaction().commit(); return list; } + /** + * @author suzhantao + * @param user + * : user instance 哈哈 + * @return obj: save的返回值为一个obj,用来判断是否save成功 + */ + public Object save(User user) { + Configuration configuration = new Configuration(); + configuration.configure(); + serviceRegistry = new ServiceRegistryBuilder().applySettings( + configuration.getProperties()).buildServiceRegistry(); + sessionFactory = configuration.buildSessionFactory(serviceRegistry); + + Session session = sessionFactory.getCurrentSession(); + session.beginTransaction(); + + Object obj = session.save(user); + + session.getTransaction().commit(); + + return obj; + + } + + /** + * + * @return + */ + public int SearchPageCount(int pageSize) { + Configuration configuration = new Configuration(); + configuration.configure(); + serviceRegistry = new ServiceRegistryBuilder().applySettings( + configuration.getProperties()).buildServiceRegistry(); + sessionFactory = configuration.buildSessionFactory(serviceRegistry); + + Session session = sessionFactory.getCurrentSession(); + + session.beginTransaction(); + + List list = session.createQuery("from User").list(); + + session.getTransaction().commit(); + + int rowCount = list.size(); + + if (rowCount % pageSize == 0) { + pageCount = rowCount / pageSize; + } else { + pageCount = rowCount / pageSize + 1; + } + + return pageCount; + + } + + /** + * Paging + * + * @param pageNow + * @param pageSize + * @return + */ + public List searchPage(int pageNow, int pageSize) { + Configuration configuration = new Configuration(); + configuration.configure(); + + serviceRegistry = new ServiceRegistryBuilder().applySettings( + configuration.getProperties()).buildServiceRegistry(); + sessionFactory = configuration.buildSessionFactory(serviceRegistry); + + Session session = sessionFactory.getCurrentSession(); + + session.beginTransaction(); + + List list = session.createQuery("from User") + .setFirstResult(pageSize * (pageNow - 1)) + .setMaxResults(pageSize).list(); + + session.getTransaction().commit(); + + return list; + } + + /** + * Update user by userId + * + * @param user + */ + public int updateUser(User user) { + Configuration configuration = new Configuration(); + configuration.configure(); + + serviceRegistry = new ServiceRegistryBuilder().applySettings( + configuration.getProperties()).buildServiceRegistry(); + sessionFactory = configuration.buildSessionFactory(serviceRegistry); + + Session session = sessionFactory.getCurrentSession(); + + session.beginTransaction(); + Query query = session.createQuery("update User set "+ + "name=:name,"+ + "pw=:pw,"+ + "age=:age,"+ + "date=:date,"+ + "email=:email"+ + " where id = :id"); + + System.out.println("name" + user.getName()); + + query.setParameter("name", user.getName()); + query.setParameter("id", user.getId()); + query.setParameter("pw", user.getPw()); + query.setParameter("age", user.getAge()); + query.setParameter("date", user.getDate()); + query.setParameter("email", user.getEmail()); + int modifications = query.executeUpdate(); + + session.getTransaction().commit(); + + return modifications; + } } diff --git a/1_Web_System/src/model/User.hbm.xml b/1_Web_System/src/model/User.hbm.xml index b02da26..85b2898 100644 --- a/1_Web_System/src/model/User.hbm.xml +++ b/1_Web_System/src/model/User.hbm.xml @@ -15,7 +15,7 @@ - + \ No newline at end of file diff --git a/1_Web_System/src/model/User.java b/1_Web_System/src/model/User.java index d2ba569..e4b5cc1 100644 --- a/1_Web_System/src/model/User.java +++ b/1_Web_System/src/model/User.java @@ -8,6 +8,13 @@ public class User { private int age; private String pw; private Date date; + private String email; + public String getEmail() { + return email; + } + public void setEmail(String email) { + this.email = email; + } public int getId() { return id; } diff --git a/1_Web_System/src/model/UserBeanProcess.java b/1_Web_System/src/model/UserBeanProcess.java new file mode 100644 index 0000000..bd484f0 --- /dev/null +++ b/1_Web_System/src/model/UserBeanProcess.java @@ -0,0 +1,126 @@ +package model; + +import java.util.Date; +import java.util.LinkedList; +import java.util.List; + +import org.junit.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; +import org.springframework.stereotype.Service; + +import dao.HibernateDao; + +/** + * Function same as Service Layer + * + * @author suzhantao + * + */ +@Component +public class UserBeanProcess { + + @Autowired + private HibernateDao hibernateDao; + + /** + * + * @param username + * @param password + * @return + */ + public boolean ValidateUser(String username, String password) { + boolean b = false; + + List list = new LinkedList(); + + list = hibernateDao.searchUser(username); + + User user = new User(); + + if (!list.isEmpty()) { + user = list.get(0); + if (user.getPw().equals(password)) { + b = true; + return b; + } + } + + return b; + } + + public boolean AddUser(String username, String password, int age) { + boolean statue = false; + + // determine if the user existing + List list = new LinkedList(); + list = hibernateDao.searchUser(username); + + User user = new User(); + user.setName(username); + + boolean userExisting = list.contains(user); + + Date date = new Date(); + + // if user not existing ,execute saving + if (userExisting == false) { + user.setAge(age); + user.setPw(password); + user.setDate(date); + + Object object = hibernateDao.save(user); + statue = true; + return statue; + } else { + return statue; + } + + } + /** + * GetUserProfile by username + * @param username + * @return user + */ + public User GetUserProfile(String username) { + List list = new LinkedList(); + + User user = new User(); + + list = hibernateDao.searchUser(username); + + user = list.get(0); + + return user; + } + + /** + * Retrieve User,and do pagination + * @param pageNow + * @param pageSize + * @return + */ + public List RetrieveUser(int pageNow,int pageSize){ + List list = new LinkedList(); + + list=hibernateDao.searchPage(pageNow, pageSize); + + return list; + } + + + /** + * Update User + * @param user + * @return int modification + */ + public int UpdateUser(User user){ + + int modification=hibernateDao.updateUser(user); + + + return modification; + } + + +} diff --git a/1_Web_System/src/test/TestClass.java b/1_Web_System/src/test/TestClass.java index b9f6542..b8f4447 100644 --- a/1_Web_System/src/test/TestClass.java +++ b/1_Web_System/src/test/TestClass.java @@ -1,18 +1,57 @@ package test; import java.util.ArrayList; +import java.util.Date; import java.util.List; +import org.junit.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.servlet.ModelAndView; + +import controller.LoginProcessController; +import model.User; import dao.HibernateDao; public class TestClass { public static void main(String[] args) { - // TODO Auto-generated method stub + + User user=new User(); + Date date=new Date(); + + user.setId(229376); + user.setName("aaa"); + user.setPw("123"); + user.setDate(date); HibernateDao h=new HibernateDao(); - List list=new ArrayList(); - list=h.searchUser("1"); + int effect=0; + effect=h.updateUser(user); + + System.out.println(effect); + + + + } + @Test + public void TestGetUser(){ + User user=new User(); + + HibernateDao h=new HibernateDao(); + + + user=(User) h.searchUser("shabi"); + + + + } + + + } + + + + diff --git a/1_Web_System/src/test/TestController.java b/1_Web_System/src/test/TestController.java new file mode 100644 index 0000000..8c82ff1 --- /dev/null +++ b/1_Web_System/src/test/TestController.java @@ -0,0 +1,37 @@ +package test; + +import model.User; +import model.UserBeanProcess; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.ModelAttribute; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; + + +@Controller +public class TestController { + @Autowired + UserBeanProcess ubp; + + + @RequestMapping(value="test1") + public void test1(int count){ + System.out.println(count); + } + + @RequestMapping(value="test2") + public void test2(@RequestParam("aage")int age, User user){ + System.out.println(user.getName()); + System.out.println(age); + + } + + + @RequestMapping(value="test3") + public void test3(@ModelAttribute("u") User user){ + System.out.println(user.getName()); + System.out.println(user.getAge()); + } +}