四、配置文件举例
1、建立一个Person类,数据model层,请详细看一下里面的注解
package edu.qdlms.logic.model; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; /** * @author Logic.Luo * @email [email protected] */ @Entity public class Person { private String username; private String password; /** * @return the username */ @Id @GeneratedValue public String getUsername() { return username; } /** * @return the password */ public String getPassword() { return password; } /** * @param username the username to set */ public void setUsername(String username) { this.username = username; } /** * @param password the password to set */ public void setPassword(String password) { this.password = password; } }
2、建立Dao
package edu.qdlms.logic.dao; import edu.qdlms.logic.model.Person; /** * @author Logic.Luo * @email [email protected] */ public interface PersonDao { public void add(Person person); }
3、建立DaoImpl
package edu.qdlms.logic.dao.impl; import javax.annotation.Resource; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.springframework.stereotype.Repository; import edu.qdlms.logic.dao.PersonDao; import edu.qdlms.logic.model.Person; /** * @author Logic.Luo * @email [email protected] */ @Repository("PersonDao") public class PersonDaoImpl implements PersonDao { private SessionFactory sessionFactory; @Override public void add(Person person) { // TODO Auto-generated method stub Session session = sessionFactory.openSession(); Transaction ts = session.getTransaction(); ts.begin(); try { session.save(person); ts.commit(); session.close(); } catch (Exception e) { // TODO Auto-generated catch block ts.rollback(); e.printStackTrace(); } } /** * @return the sessionFactory */ public SessionFactory getSessionFactory() { return sessionFactory; } /** * @param sessionFactory the sessionFactory to set */ @Resource(name="sessionFactory") public void setSessionFactory(SessionFactory sessionFactory) { this.sessionFactory = sessionFactory; } }
4、建立service层
package edu.qdlms.logic.service; import edu.qdlms.logic.model.Person; /** * @author Logic.Luo * @email [email protected] */ public interface PersonService { public void add(Person person); }
5、建立ServiceImpl
package edu.qdlms.logic.service.impl; import javax.annotation.Resource; import org.springframework.stereotype.Component; import edu.qdlms.logic.dao.PersonDao; import edu.qdlms.logic.model.Person; import edu.qdlms.logic.service.PersonService;
/** * @author Logic.Luo * @email [email protected] */ @Component("PersonService") public class PersonServiceImpl implements PersonService{ private PersonDao personDao; @Override public void add(Person person) { // TODO Auto-generated method stub personDao.add(person); } /** * @return the personDao */ public PersonDao getPersonDao() { return personDao; } /** * @param personDao the personDao to set */ @Resource(name="PersonDao") public void setPersonDao(PersonDao personDao) { this.personDao = personDao; } }
6、建立Action
package edu.qdlms.logic.action; import javax.annotation.Resource; import org.apache.struts2.convention.annotation.Action; import org.apache.struts2.convention.annotation.Namespace; import org.apache.struts2.convention.annotation.Result; import org.springframework.stereotype.Controller; import com.opensymphony.xwork2.ActionSupport; import edu.qdlms.logic.model.Person; import edu.qdlms.logic.service.PersonService; /** * @author Logic.Luo * @email [email protected] */ @Controller @Namespace("/") @Action(value = "person", results = { @Result(name = "add", location = "/welcome.html") }) public class PersonAction extends ActionSupport{ private static final long serialVersionUID = 960668260484674943L; private PersonService personService; private String username; private String password; /* (non-Javadoc) * @see com.opensymphony.xwork2.ActionSupport#execute() */ public String add() { Person p = new Person(); p.setUsername(username); p.setPassword(password); personService.add(p); return "add"; } /** * @return the username */ public String getUsername() { return username; } /** * @return the password */ public String getPassword() { return password; } /** * @param username the username to set */ public void setUsername(String username) { this.username = username; } /** * @param password the password to set */ public void setPassword(String password) { this.password = password; } /** * @return the personService */ public PersonService getPersonService() { return personService; } /** * @param personService the personService to set */ @Resource(name="PersonService") public void setPersonService(PersonService personService) { this.personService = personService; } }
7、index.html文件的配置
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>欢迎界面</title> </head> <body> 这是首页 <form action="person!add" method="post"> 用户名:<input name="username" type="text"/><br> 密码:<input name="password" type="password" /><br/> <input name="submit" type="submit" value="登陆" /> </form> </body> </html>
8、欢迎界面welcome.html
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>欢迎界面</title> </head> <body> 这是欢迎界面 </body> </html>
五、下面是项目的下载地址
http://download.csdn.net/detail/luojiming1990/5432601