departmentlist">

Presentation is loading. Please wait.

Presentation is loading. Please wait.

EXtreme Programming XP Тема 11. XP Для работы по администрированию таблицы Department создаем классы: DepartmentListController; EnterDepartmentController;

Similar presentations


Presentation on theme: "EXtreme Programming XP Тема 11. XP Для работы по администрированию таблицы Department создаем классы: DepartmentListController; EnterDepartmentController;"— Presentation transcript:

1 eXtreme Programming XP Тема 11

2 XP Для работы по администрированию таблицы Department создаем классы: DepartmentListController; EnterDepartmentController; DeleteDepartmentController; EnterDepartmentValidator; DelName – командный объект для формы выбора удаляемого элемента; DeleteDepartmentController.

3 XP Добавляем: departmentlist

4 XP Валидаторы: Менеджер:

5 XP true /enterdepartment redirect:departmentlist.htm com.visualpatterns.timex.model.Department

6 XP true /deletedepartment redirect:departmentlist.htm com.visualpatterns.timex.controller.DelName

7 XP Назначение перехватчика на файлы: departmentListController enterDepartmentConroller deleteDepartmentConroller

8 XP package com.visualpatterns.timex.controller; public class SignLogin { String login, password; public String getLogin() {return login;} public void setLogin(String login){this.login=login;} public String getPassword(){return password;} public void setPassword(String pass){ password=pass;} }

9 XP public List getDepartments() { List departmentList = null; SessionFactory SessionFactory=new Configuration().configure().buildSessionFactory(); Session session =SessionFactory.getCurrentSession(); session.beginTransaction(); try { departmentList = session.createQuery( "from Department ORDER BY name").list(); session.getTransaction().commit(); } catch (HibernateException e){ session.getTransaction().rollback(); throw e;} return departmentList; }

10 XP public Department getDepartment(String departmentCode) { Department department = null; SessionFactory SessionFactory=new Configuration().configure().buildSessionFactory(); Session session =SessionFactory.getCurrentSession(); session.beginTransaction(); try{ department = (Department) session.createQuery( "from Department" + " where departmentCode = ?").setString(0, departmentCode).uniqueResult(); session.getTransaction().commit(); SessionFactory.close();} catch (HibernateException e) { session.getTransaction().rollback(); throw e;} return department; }

11 XP public void saveDepartment(Department department) { SessionFactory SessionFactory=new Configuration().configure().buildSessionFactory(); Session session =SessionFactory.getCurrentSession(); session.beginTransaction(); try{ session.saveOrUpdate(department); session.getTransaction().commit(); SessionFactory.close(); } catch (HibernateException e) { session.getTransaction().rollback(); throw e;} }

12 XP public void deleteDepartment(String departmentCode) { SessionFactory SessionFactory=new Configuration().configure().buildSessionFactory(); Session session =SessionFactory.getCurrentSession(); session.beginTransaction(); try{ session.delete(session.load(Department.class, departmentCode)); session.flush(); session.getTransaction().commit(); SessionFactory.close();} catch (HibernateException e) { session.getTransaction().rollback(); throw e;} }

13 XP package com.visualpatterns.timex.controller; import java.util.List; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.mvc.Controller; import com.visualpatterns.timex.model.Department; import com.visualpatterns.timex.model.DepartmentManager; import com.visualpatterns.timex.controller.ApplicationSecurityManager; public class DepartmentListController implements Controller { private DepartmentManager departmentManager; public static final String MAP_KEY = "departments"; private ApplicationSecurityManager applicationSecurityManager; private String successView;

14 XP public ModelAndView handleRequest( HttpServletRequest request, HttpServletResponse response) throws Exception { List departments = departmentManager.getDepartments(); return new ModelAndView(getSuccessView(), MAP_KEY, departments); } public ApplicationSecurityManager getApplicationSecurityManager() { return applicationSecurityManager; } public void setApplicationSecurityManager(ApplicationSecurityManager app) { applicationSecurityManager=app; }

15 XP public DepartmentManager getDepartmentManager() { return departmentManager;} public void setDepartmentManager(DepartmentManager departmentManager) { this.departmentManager = departmentManager;} public String getSuccessView() { return successView;} public void setSuccessView(String successView) { this.successView = successView; }

16 XP package com.visualpatterns.timex.controller; import org.springframework.validation.Errors; import com.visualpatterns.timex.model.Department; public class EnterDepartmentValidator implements org.springframework.validation.Validator { public boolean supports(Class clazz) { return clazz.equals(Department.class);} public void validate(Object command, Errors errors) { Department log = (Department) command; if (log == null) return; String code = log.getDepartmentCode(); if (code.length()>2 || code.length()<2) errors.reject("error.enterdepartment.codeiswrong"); }

17 XP package com.visualpatterns.timex.controller; import java.util.HashMap; import java.util.Map; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.validation.BindException; import org.springframework.web.bind.ServletRequestDataBinder; import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.mvc.SimpleFormController; import com.visualpatterns.timex.model.DepartmentManager; import com.visualpatterns.timex.model.Department; import com.visualpatterns.timex.model.DepartmentManager; import com.visualpatterns.timex.controller.ApplicationSecurityManager;

18 XP public class EnterDepartmentController extends SimpleFormController { private DepartmentManager departmentManager = null; private ApplicationSecurityManager applicationSecurityManager; public static final String TID = "tid"; protected Object formBackingObject(HttpServletRequest request) { if (request.getParameter(TID) != null && request.getParameter(TID).trim().length() > 0) { String code1=""; try{ code1=new String(request.getParameter(TID). trim().getBytes("ISO "),"Cp1251"); } catch(Exception e){} return departmentManager.getDepartment(code1); } Department department = new Department(); return department; }

19 XP protected ModelAndView onSubmit( HttpServletRequest request, HttpServletResponse response, Object command, BindException errors) { Department department = (Department) command; try{ String code1=new String(department.getDepartmentCode().getBytes("ISO "),"Cp1251"); String name1=new String(department.getName().getBytes("ISO "),"Cp1251"); department.setDepartmentCode(code1);department.setName(name1); } catch(Exception e){} departmentManager.saveDepartment(department); try{ String message=new String(getMessageSourceAccessor().getMessage("message.enterdepartment.sa vesuccess").getBytes("ISO "),"Cp1251"); request.getSession().setAttribute("message",message); } catch(Exception e){} return new ModelAndView(getSuccessView());} // также следует добавить все set и get-методы для свойств класса

20 XP package com.visualpatterns.timex.controller; import org.springframework.validation.Errors; import com.visualpatterns.timex.controller.SignLogin; public class SignInValidator implements org.springframework.validation.Validator { public boolean supports(Class clazz) { return clazz.equals(SignLogin.class); } public void validate(Object command, Errors errors) { SignLogin log = (SignLogin) command; if (log == null) return; String login = log.getLogin(); String password = log.getPassword(); if (!(login.equals("admin") && password.equals("admin"))) errors.reject("error.login.invalid"); }

21 XP package com.visualpatterns.timex.controller; import org.springframework.validation.Errors; import com.visualpatterns.timex.controller.DelName; public class DeleteDepartmentValidator implements org.springframework.validation.Validator { public boolean supports(Class clazz) { return clazz.equals(DelName.class);} public void validate(Object command, Errors errors) { DelName log = (DelName) command; if (log == null) return; String code = log.getCode(); if (code.length()!=2) errors.reject("error.login.invalid"); }

22 XP package com.visualpatterns.timex.controller; import java.util.HashMap; import java.util.Map; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.validation.BindException; import org.springframework.web.bind.ServletRequestDataBinder; import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.mvc.SimpleFormController; import com.visualpatterns.timex.model.DepartmentManager; import com.visualpatterns.timex.model.Department; import com.visualpatterns.timex.controller.DelName; import com.visualpatterns.timex.controller.ApplicationSecurityManager;

23 XP public class DeletDepartmentController extends SimpleFormController { private DepartmentManager departmentManager = null; private ApplicationSecurityManager applicationSecurityManager; protected Object formBackingObject(HttpServletRequest request) { return new DelName(); } protected Map referenceData(HttpServletRequest request) throws Exception { // для создания списка выбора HashMap model = new HashMap(); model.put("departments", departmentManager.getDepartments()); return model; }

24 XP protected ModelAndView onSubmit( HttpServletRequest request, HttpServletResponse response, Object command, BindException errors) { DelName code= (DelName) command; String code1=""; try{ code1=new String(code.getCode().getBytes("ISO "),"Cp1251"); }catch(Exception e){} departmentManager.deleteDepartment(code1); request.getSession().setAttribute("message",getMessageSourceAccessor(). getMessage("message.deletedepartment.deletesuccess")); return new ModelAndView(getSuccessView()); } // а также get и set-методы для всех свойств контроллера

25 XP Список всех отделов Список всех отделов Выйти из системы

26 XP Добавить отдел Удалить отдел Code Name '>

27 XP Добавление/редактирование отдела Информация об отделе Выйти из системы

28 XP Код отдела Название отдела ' value=' ' type="text" size="4" maxlength="6"> ' value=' ' type="text" size="4" maxlength="6“>

29 XP

30 XP Удаление отдела Выберите отдел для удаления Выйти из системы '> '>

31 XP


Download ppt "EXtreme Programming XP Тема 11. XP Для работы по администрированию таблицы Department создаем классы: DepartmentListController; EnterDepartmentController;"

Similar presentations


Ads by Google