这里我们需要使用spring的ioc将DAO诸如到service中,所以不要忘记在service实现类中加入DAO作为属性。
PersonServiceImpl.java
Java代码
1.package test.s2sh.service.impl;
2.
3.import java.util.List;
4.
5.import test.s2sh.bean.Person;
6.import test.s2sh.dao.PersonDAO;
7.import test.s2sh.service.PersonService;
8.
9.public class PersonServiceImpl implements PersonService {
10. private PersonDAO personDAO;
11. public void delete(Person p) {
12. this.personDAO.removePerson(p);
13. }
14.
15. public List<Person> findAll() {
16. return this.personDAO.findAllPersons();
17. }
18.
19. public Person findById(Integer id) {
20. return this.personDAO.findPersonById(id);
21. }
22.
23. public void save(Person p) {
24. this.personDAO.savePerson(p);
25. }
26.
27. public void update(Person p) {
28. this.personDAO.updatePerson(p);
29. }
30.
31. public PersonDAO getPersonDAO() {
32. return personDAO;
33. }
34.
35. public void setPersonDAO(PersonDAO personDAO) {
36. this.personDAO = personDAO;
37. }
38.
39.}
Java代码
1.package test.<SPAN class=hilite1>s2sh</SPAN>.service.impl;
2.
3.import java.util.List;
4.
5.import test.<SPAN class=hilite1>s2sh</SPAN>.bean.Person;
6.import test.<SPAN class=hilite1>s2sh</SPAN>.dao.PersonDAO;
7.import test.<SPAN class=hilite1>s2sh</SPAN>.service.PersonService;
8.
9.public class PersonServiceImpl implements PersonService {
10. private PersonDAO personDAO;
11. public void delete(Person p) {
12. this.personDAO.removePerson(p);
13. }
14.
15. public List<Person> findAll() {
16. return this.personDAO.findAllPersons();
17. }
18.
19. public Person findById(Integer id) {
20. return this.personDAO.findPersonById(id);
21. }
22.
23. public void save(Person p) {
24. this.personDAO.savePerson(p);
25. }
26.
27. public void update(Person p) {
28. this.personDAO.updatePerson(p);
29. }
30.
31. public PersonDAO getPersonDAO() {
32. return personDAO;
33. }
34.
35. public void setPersonDAO(PersonDAO personDAO) {
36. this.personDAO = personDAO;
37. }
38.
39.}
package test.s2sh.service.impl;
import java.util.List;
import test.s2sh.bean.Person;
import test.s2sh.dao.PersonDAO;
import test.s2sh.service.PersonService;
public class PersonServiceImpl implements PersonService {
private PersonDAO personDAO;
public void delete(Person p) {
this.personDAO.removePerson(p);
}
public List<Person> findAll() {
return this.personDAO.findAllPersons();
}
public Person findById(Integer id) {
return this.personDAO.findPersonById(id);
}
public void save(Person p) {
this.personDAO.savePerson(p);
}
public void update(Person p) {
this.personDAO.updatePerson(p);
}
public PersonDAO getPersonDAO() {
return personDAO;
}
public void setPersonDAO(PersonDAO personDAO) {
this.personDAO = personDAO;
}
}
(6)接下来我们开始对action层进行设置,实际项目中的action可能会有很多,所以我们这里需要对action进行分包,因为我们这里针对Person进行操作,所以我们创建test.s2sh.action.person.
这里先做保存操作
SavePersonAction.java
Java代码
1.package test.s2sh.action.person;
2.
3.import test.s2sh.bean.Person;
4.import test.s2sh.service.PersonService;
5.
6.import com.opensymphony.xwork2.ActionSupport;
7.
8.public class SavePersonAction extends ActionSupport {
9. private Person p;
10. private PersonService service;
11. public Person getP() {
12. return p;
13. }
14. public void setP(Person p) {
15. this.p = p;
16. }
17. public PersonService getService() {
18. return service;
19. }
20. public void setService(PersonService service) {
21. this.service = service;
22. }
23.
24. public String execute() throws Exception {
25. this.service.save(p);
26. return this.SUCCESS;
27. }
28.
29. public void validate() {
30. if(p.getName()==null||"".equals(p.getName())){
31. this.addFieldError("p.name", "name is not null");
32. }
33. }
34.
35.}
Java代码
1.package test.<SPAN class=hilite1>s2sh</SPAN>.action.person;
2.
3.import test.<SPAN class=hilite1>s2sh</SPAN>.bean.Person;
4.import test.<SPAN class=hilite1>s2sh</SPAN>.service.PersonService;
5. 6.import com.opensymphony.xwork2.ActionSupport;
7.
8.public class SavePersonAction extends ActionSupport {
9. private Person p;
10. private PersonService service;
11. public Person getP() {
12. return p;
13. }
14. public void setP(Person p) {
15. this.p = p;
16. }
17. public PersonService getService() {
18. return service;
19. }
20. public void setService(PersonService service) {
21. this.service = service;
22. }
23.
24. public String execute() throws Exception {
25. this.service.save(p);
26. return this.SUCCESS;
27. }
28.
29. public void validate() {
30. if(p.getName()==null||"".equals(p.getName())){
31. this.addFieldError("p.name", "name is not null");
32. }
33. }
34.
35.}
package test.s2sh.action.person;
import test.s2sh.bean.Person;
import test.s2sh.service.PersonService;
import com.opensymphony.xwork2.ActionSupport;
public class SavePersonAction extends ActionSupport {
private Person p;
private PersonService service;
public Person getP() {
return p;
}
public void setP(Person p) {
this.p = p;
}
public PersonService getService() {
return service;
}
public void setService(PersonService service) {
this.service = service;
}
public String execute() throws Exception {
this.service.save(p);
return this.SUCCESS;
}
public void validate() {
if(p.getName()==null||"".equals(p.getName())){
this.addFieldError("p.name", "name is not null");
}
}
}
下面创建一个保存人员的提交页面
save.jsp
Java代码
1.<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
2.<%@ taglib prefix="s" uri="/struts-tags" %>
3.
4.<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
5.<html>
6. <head>
7.
8. <title>Save Person</title>
9.
10. </head>
11.
12. <body>
13. <s:form action="savePerson">
14. <s:textfield name="p.name" label="name"></s:textfield>
15. <s:textfield name="p.age" label="age"></s:textfield>
16. <s:submit></s:submit>
17. </s:form>
18. </body>
19.</html>
Java代码
1.<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
2.<%@ taglib prefix="s" uri="/struts-tags" %>
3.
4.<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
5.<html>
6. <head>
7.
8. <title>Save Person</title>
9.
10. </head>
11.
12. <body>
13. <s:form action="savePerson">
14. <s:textfield name="p.name" label="name"></s:textfield>
15. <s:textfield name="p.age" label="age"></s:textfield>
16. <s:submit></s:submit>
17. </s:form>
18. </body>
19.</html>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Save Person</title>
</head>
<body>
<s:form action="savePerson">
<s:textfield name="p.name" label="name"></s:textfield>
<s:textfield name="p.age" label="age"></s:textfield>
<s:submit></s:submit>
</s:form>
</body>
</html>
操作成功页面
save-success.jsp
Java代码
1.<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
2.
3.<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
4.<html>
5. <head>
6. <title>Save OK</title>
7. </head>
8.
9. <body>
10. 保存成功 <br>
11. </body>
12.</html>
Java代码
1.<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
2.
3.<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
4.<html>
5. <head>
6. <title>Save OK</title>
7. </head>
8.
9. <body>
10. 保存成功 <br>
11. </body>
12.</html>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Save OK</title>
</head>
<body>
保存成功 <br>
</body>
</html>
接下来我们需要配置struts.xml,创建action
Xml代码
1.<action name="savePerson" class="savePersonAction">
2. <result name="success" type="redirect">/save-success.jsp</result>
3. <result name="input">/save.jsp</result>
4.</action>
Xml代码
1.<action name="savePerson" class="savePersonAction">
2. <result name="success" type="redirect">/save-success.jsp</result>
3. <result name="input">/save.jsp</result>
4.</action>
<action name="savePerson" class="savePersonAction">
<result name="success" type="redirect">/save-success.jsp</result>
<result name="input">/save.jsp</result>
</action>
1.<bean id="personDAO" class="test.s2sh.dao.impl.PersonDAOImpl" scope="singleton">
2. <!-- 由于该类是继承于HibernateDaoSupport,所以需要将sessionFactory注入 -->
3. <property name="sessionFactory" >
4. <ref bean="sessionFactory"/>
5. </property>
6.</bean>
7.<bean id="personService" class="test.s2sh.service.impl.PersonServiceImpl">
8. <property name="personDAO" ref="personDAO"></property>
9.</bean>
10.<bean id="savePersonAction" class="test.s2sh.action.person.SavePersonAction" scope="prototype">
11. <property name="service" ref="personService"></property>
12.</bean>
Xml代码
1.<bean id="personDAO" class="test.<SPAN class=hilite1>s2sh</SPAN>.dao.impl.PersonDAOImpl" scope="singleton">
2. <!-- 由于该类是继承于HibernateDaoSupport,所以需要将sessionFactory注入 -->
3. <property name="sessionFactory" >
4. <ref bean="sessionFactory"/>
5. </property>
6.</bean>
7.<bean id="personService" class="test.<SPAN class=hilite1>s2sh</SPAN>.service.impl.PersonServiceImpl">
8. <property name="personDAO" ref="personDAO"></property>
9.</bean>
10.<bean id="savePersonAction" class="test.<SPAN class=hilite1>s2sh</SPAN>.action.person.SavePersonAction" scope="prototype">
11. <property name="service" ref="personService"></property>
12.</bean>
<bean id="personDAO" class="test.s2sh.dao.impl.PersonDAOImpl" scope="singleton">
<!-- 由于该类是继承于HibernateDaoSupport,所以需要将sessionFactory注入 -->
<property name="sessionFactory" >
<ref bean="sessionFactory"/>
</property>
</bean>
<bean id="personService" class="test.s2sh.service.impl.PersonServiceImpl">
<property name="personDAO" ref="personDAO"></property>
</bean>
<bean id="savePersonAction" class="test.s2sh.action.person.SavePersonAction" scope="prototype">
<property name="service" ref="personService"></property>
</bean>
由于现在的action的创建已经由spring所接管,所以这里class里面使用的是别名,该别名与applicationContext.xml中的id对应.
接下来我们需要配置applicationContext的ioc。
Xml代码
这里说明一下spring中的单例与设计模式中的单例不同,设计模式的单例是在jvm中只产生一个实例,而spring中的单例
是针对每个ioc容器来言,如果存在两个ioc容器,那么每个ioc容器都会生成唯一的类实例
将sessionFactory注入Dao中,这里的Dao来说是无状态的,可以设置为单例singleton。这里的action是使用spring创建的。
struts2与struts1的action有所不同,因为struts2中的action中可能存在属性,这里是Person,所以struts2的action可能是有状态的。
如果是有状态的我们就需要将scope设置成原型模式,即每次请求都新建一个action。
不然可能会出现如果表单提交的第一次出错,之后就再也提交不过去了,值就是应为使用默认的singleton,
当第一次的时候就将FieldError中加入了错误,每次都是这个action,始终带着这个错误,所以就提交不过去了。