---
title: "s2sh整合<二>"
author: "returnofking"
date: "2010-03-18T13:46:37Z"
tags: []
categories: ["企业架构"]
slug: "s2sh"
draft: false
lastmod: "2010-03-18T13:46:37Z"
id: 618967
url: "https://ReturnOfKing.iteye.com/blog/618967"
---

这里我们需要使用spring的ioc将DAO诸如到service中，所以不要忘记在service实现类中加入DAO作为属性。  
PersonServiceImpl.java

Java代码 [![复制代码](/images/icon_copy.gif)](# "复制代码")

01. **package** test.s2sh.service.impl;
02. **import** java.util.List;
03. **import** test.s2sh.bean.Person;
04. **import** test.s2sh.dao.PersonDAO;
05. **import** test.s2sh.service.PersonService;
06. **public** **class** PersonServiceImpl **implements** PersonService {
07. **private** PersonDAO personDAO;
08. **public** **void** delete(Person p) {
09. **this**.personDAO.removePerson(p);
10. }
11. **public** List&lt;Person&gt; findAll() {
12. **return** **this**.personDAO.findAllPersons();
13. }
14. **public** Person findById(Integer id) {
15. **return** **this**.personDAO.findPersonById(id);
16. }
17. **public** **void** save(Person p) {
18. **this**.personDAO.savePerson(p);
19. }
20. **public** **void** update(Person p) {
21. **this**.personDAO.updatePerson(p);
22. }
23. **public** PersonDAO getPersonDAO() {
24. **return** personDAO;
25. }
26. **public** **void** setPersonDAO(PersonDAO personDAO) {
27. **this**.personDAO = personDAO;
28. }
29. }

```
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代码 [![复制代码](/images/icon_copy.gif)](# "复制代码")

01. **package** test.s2sh.action.person;
02. **import** test.s2sh.bean.Person;
03. **import** test.s2sh.service.PersonService;
04. **import** com.opensymphony.xwork2.ActionSupport;
05. **public** **class** SavePersonAction **extends** ActionSupport {
06. **private** Person p;
07. **private** PersonService service;
08. **public** Person getP() {
09. **return** p;
10. }
11. **public** **void** setP(Person p) {
12. **this**.p = p;
13. }
14. **public** PersonService getService() {
15. **return** service;
16. }
17. **public** **void** setService(PersonService service) {
18. **this**.service = service;
19. }
20. **public** String execute() **throws** Exception {
21. **this**.service.save(p);
22. **return** **this**.SUCCESS;
23. }
24. **public** **void** validate() {
25. **if**(p.getName()==**null**||"".equals(p.getName())){
26. **this**.addFieldError("p.name", "name is not null");
27. }
28. }
29. }

```
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代码 [![复制代码](/images/icon_copy.gif)](# "复制代码")

01. &lt;%@ page language="java" **import**="java.util.\*" pageEncoding="utf-8"%&gt;
02. &lt;%@ taglib prefix="s" uri="/struts-tags" %&gt;
03. &lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"&gt;
04. &lt;html&gt;
05. &lt;head&gt;
06. &lt;title&gt;Save Person&lt;/title&gt;
07. &lt;/head&gt;
08. &lt;body&gt;
09. &lt;s:form action="savePerson"&gt;
10. &lt;s:textfield name="p.name" label="name"&gt;&lt;/s:textfield&gt;
11. &lt;s:textfield name="p.age" label="age"&gt;&lt;/s:textfield&gt;
12. &lt;s:submit&gt;&lt;/s:submit&gt;
13. &lt;/s:form&gt;
14. &lt;/body&gt;
15. &lt;/html&gt;

```
<%@ 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代码 [![复制代码](/images/icon_copy.gif)](# "复制代码")

01. &lt;%@ page language="java" **import**="java.util.\*" pageEncoding="utf-8"%&gt;
02. &lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"&gt;
03. &lt;html&gt;
04. &lt;head&gt;
05. &lt;title&gt;Save OK&lt;/title&gt;
06. &lt;/head&gt;
07. &lt;body&gt;
08. 保存成功 &lt;br&gt;
09. &lt;/body&gt;
10. &lt;/html&gt;

```
<%@ 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代码 [![复制代码](/images/icon_copy.gif)](# "复制代码")

1. **&lt;action** name="savePerson" class="savePersonAction"**&gt;**
2. **&lt;result** name="success" type="redirect"**&gt;**/save-success.jsp**&lt;/result&gt;**
3. **&lt;result** name="input"**&gt;**/save.jsp**&lt;/result&gt;**
4. **&lt;/action&gt;**

```
<action name="savePerson" class="savePersonAction">
	<result name="success" type="redirect">/save-success.jsp</result>
	<result name="input">/save.jsp</result>
</action>
```

由于现在的action的创建已经由spring所接管，所以这里class里面使用的是别名，该别名与applicationContext.xml中的id对应.

接下来我们需要配置applicationContext的ioc。

Xml代码 [![复制代码](/images/icon_copy.gif)](# "复制代码")

01. **&lt;bean** id="personDAO" class="test.s2sh.dao.impl.PersonDAOImpl" scope="singleton"**&gt;**
02. &lt;!-- 由于该类是继承于HibernateDaoSupport，所以需要将sessionFactory注入 --&gt;
03. **&lt;property** name="sessionFactory" **&gt;**
04. **&lt;ref** bean="sessionFactory"**/&gt;**
05. **&lt;/property&gt;**
06. **&lt;/bean&gt;**
07. **&lt;bean** id="personService" class="test.s2sh.service.impl.PersonServiceImpl"**&gt;**
08. **&lt;property** name="personDAO" ref="personDAO"**&gt;&lt;/property&gt;**
09. **&lt;/bean&gt;**
10. **&lt;bean** id="savePersonAction" class="test.s2sh.action.person.SavePersonAction" scope="prototype"**&gt;**
11. **&lt;property** name="service" ref="personService"**&gt;&lt;/property&gt;**
12. **&lt;/bean&gt;**

```
<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>
```

这里说明一下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，始终带着这个错误，所以就提交不过去了。

到目前为止这个整合已经完成了。