Practice 3-design management module, step 1, new module, and department management

Source: Internet
Author: User

I .. design entity classes/Tables

1. Write entity class department. Java:

package cn.itcast.oa.domain;public class Department {private Long id;private String name;private String description;public Long getId() {return id;}public void setId(Long id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getDescription() {return description;}public void setDescription(String description) {this.description = description;}}

2. Write the Department ing file department. HBM. xml

<?xml version="1.0"?><!DOCTYPE hibernate-mapping PUBLIC        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"        "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

3. Add the object class to hibernate. cfg. xml

<?xml version=‘1.0‘ encoding=‘utf-8‘?><!DOCTYPE hibernate-configuration PUBLIC        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"        "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"><!-- 2. other configuration --><property name="show_sql">true</property><property name="hbm2ddl.auto">update</property><property name="connection.pool_size">1</property><!--3. mapping --><mapping resource="cn/itcast/oa/domain/User.hbm.xml" /><mapping resource="cn/itcast/oa/domain/Role.hbm.xml" /><mapping resource="cn/itcast/oa/domain/Department.hbm.xml" /></session-factory>

4. Test class generation table: springtest. Java

package cn.itcast.oa.test;import org.hibernate.SessionFactory;import org.junit.Test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class SpringTest {private ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");@Testpublic void testSessionFactory() throws Exception{SessionFactory sessionFactory = (SessionFactory)ac.getBean("sessionFactory");System.out.println(sessionFactory);}}

The table is successfully generated !!!

2. Analysis has several functions, corresponding to several requests

1. Build the Action Framework

Note: @ controller @ scope ("prototype ")

Package CN. itcast. oa. view. action; import javax. annotation. resource; import Org. springframework. context. annotation. scope; import Org. springframework. stereotype. controller; import COM. opensymphony. xwork2.actioncontext; import COM. opensymphony. xwork2.actionsupport;
@ Controller @ scope ("prototype") public class departmentaction extends actionsupport {/* list */Public String list () throws exception {return "list ";} /* Delete */Public String Delete () throws exception {return "tolist";}/* add page */Public String addui () throws exception {return "saveui ";} /* Add */Public String add () throws exception {return "tolist";}/* modify page */Public String editui () throws exception {return "saveui ";} /* modify */Public string edit () throws exception {return "tolist ";}}

2. Configure struts. xml:

<! -- Department management --> <action name = "department _ *" class = "departmentaction" method = "{1}"> <result name = "list">/WEB-INF/JSP/ departmentaction/list. JSP </result> <result name = "saveui">/WEB-INF/JSP/departmentaction/saveui. JSP </result> <result name = "tolist" type = "redirectaction"> department_list </result> </Action>

3. Write two simple JSP files. The verification is successful.

4. Complete the action:

Package CN. itcast. oa. view. action; import Java. util. list; import javax. annotation. resource; import Org. springframework. context. annotation. scope; import Org. springframework. stereotype. controller; import CN. itcast. oa. domain. department; import CN. itcast. oa. service. departmentservice; import COM. opensymphony. xwork2.actioncontext; import COM. opensymphony. xwork2.actionsupport; import COM. opensymphony. xwork2.modeldriven; @ controller @ scope ("prototype") public class departmentaction extends actionsupport implements modeldriven <Department >{@ resourceprivate departmentservice; private department model = new Department (); /* list */Public String list () throws exception {list <Department> departmentlist = departmentservice. findall (); actioncontext. getcontext (). put ("departmentlist", departmentlist); Return "list";}/* Delete */Public String Delete () throws exception {departmentservice. delete (model. GETID (); Return "tolist";}/* add page */Public String addui () throws exception {return "saveui ";} /* Add */Public String add () throws exception {departmentservice. save (model); Return "tolist";}/* modify page */Public String editui () throws exception {/* prepare echo data */Department = departmentservice. getbyid (model. GETID (); actioncontext. getcontext (). getvaluestack (). push (Department); Return "saveui";}/* modify */Public string edit () throws exception {// 1. retrieve the original object Department = departmentservice from the database. getbyid (model. GETID (); // 2. set the Department attribute to be modified. setname (model. getname (); department. setdescription (model. getdescription (); // 3. update to database departmentservice. update (Department); Return "tolist";} public department GetModel () {// todo auto-generated method stubreturn model ;}}

 

5. Write the service interface, departmentservice. java. It is the method created by Ctrl + 1 when calling the method in action.

package cn.itcast.oa.service;import java.util.List;import cn.itcast.oa.domain.Department;public interface DepartmentService {List<Department> findAll();void delete(Long id);void save(Department model);Department getById(Long id);void update(Department department);}

6. Create the departmentservice interface implementation class, departmentserviceimpl. Java

Note: @ service, @ transactional. @ resource is used when Dao is used.

package cn.itcast.oa.service.impl;import java.util.List;import javax.annotation.Resource;import org.springframework.stereotype.Service;import org.springframework.transaction.annotation.Transactional;import cn.itcast.oa.dao.DepartmentDao;import cn.itcast.oa.domain.Department;import cn.itcast.oa.service.DepartmentService;@Service@Transactionalpublic class DepartmentServiceImpl implements DepartmentService{@Resourceprivate DepartmentDao departmentDao;public List<Department> findAll() {return departmentDao.findAll();}public void delete(Long id) {departmentDao.delete(id);}public void save(Department model) {departmentDao.save(model);}public Department getById(Long id) {return departmentDao.getById(id);}public void update(Department department) {departmentDao.update(department);}}

Iii. Create Dao interface and implementation class

1. Dao interface, departmentdao. java. Be sure to inherit the basedao Interface

package cn.itcast.oa.dao;import cn.itcast.oa.base.BaseDao;import cn.itcast.oa.domain.Department;public interface DepartmentDao extends BaseDao<Department>{
}

2. Implementation class departmentdaoimpl. Java,

Note 1. inherit from basedaoimpl, 2. Implement departmentdao 3. @ Repository

package cn.itcast.oa.dao.impl;import org.springframework.stereotype.Repository;import cn.itcast.oa.base.BaseDaoImpl;import cn.itcast.oa.dao.DepartmentDao;import cn.itcast.oa.domain.Department;@Repositorypublic class DepartmentDaoImpl extends BaseDaoImpl<Department> implements DepartmentDao{}

  

4. Write the JSP page:

Create the departmentaction folder in the JSP path and create two JSP folders: List. jsp and saveui. jsp.

List. jsp:

<% @ Page Language = "Java" Import = "Java. util. * "pageencoding =" UTF-8 "%> <% @ taglib prefix =" S "uri ="/Struts-tags "%> <% string Path = request. getcontextpath (); %> <HTML> 

Saveui. jsp

<% @ Page Language = "Java" Import = "Java. util. * "pageencoding =" UTF-8 "%> <% @ taglib prefix =" S "uri ="/Struts-tags "%> <% string Path = request. getcontextpath (); %> <HTML> 

  

  

  

 

 

 

 

Practice 3-design management module, step 1, new module, and department management

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.