Spring3.0.4_Struts2.2.1_Hibernate3.6.0_Stey by Step

merryboy 2010-10-19 12:27:46
原文引用:
http://struts.apache.org/2.0.14/docs/struts-2-spring-2-jpa-ajax.html

1.下载
spring-framework-3.0.4.RELEASE.zip
http://s3.amazonaws.com/dist.springframework.org/release/SPR/spring-framework-3.0.4.RELEASE.zip

spring-framework-2.0.8-with-dependencies.zip
http://downloads.sourceforge.net/project/springframework/springframework-2/2.0.8/spring-framework-2.0.8-with-dependencies.zip?r=http%3A%2F%2Fsourceforge.net%2Fprojects%2Fspringframework%2Ffiles%2Fspringframework-2%2F2.0.8%2Fspring-framework-2.0.8-with-dependencies.zip%2Fdownload&ts=1287368357&use_mirror=nchc

struts2-2.2.1-lib.zip
http://mirror.bjtu.edu.cn/apache//struts/library/struts2-2.2.1-lib.zip

hibernate-distribution-3.6.0.Final-dist.zip
http://nchc.dl.sourceforge.net/project/hibernate/hibernate3/3.6.0.Final/hibernate-distribution-3.6.0.Final-dist.zip

mysql-connector-java-5.1.13.zip
http://gd.tuwien.ac.at/db/mysql/Downloads/Connector-J/mysql-connector-java-5.1.13.zip

eclipse-jee-helios-SR1-win32.zip
http://www.eclipse.org/downloads/download.php?file=/technology/epp/downloads/release/helios/SR1/eclipse-jee-helios-SR1-win32.zip&url=http://download.actuatechina.com/eclipse/technology/epp/downloads/release/helios/SR1/eclipse-jee-helios-SR1-win32.zip&mirror_id=385

jdk-6u21-windows-i586.exe 下面的地址需要自己选择自己要的jdk
http://www.oracle.com/technetwork/java/archive-139210.html

apache-tomcat-7.0.2.exe
http://archive.apache.org/dist/tomcat/tomcat-7/v7.0.2-beta/bin/apache-tomcat-7.0.2.exe

slf4j-1.6.1.zip
http://www.slf4j.org/dist/slf4j-1.6.1.zip

2.提取包
从spring-framework-3.0.4.RELEASE.zip中spring-framework-3.0.4.RELEASE\dist提取如下包(里面的所有jar包都要)
(有人说不要这个org.springframework.web.struts-3.0.4.RELEASE.jar,但我测试后,可以用这个)
org.springframework.aop-3.0.4.RELEASE.jar
org.springframework.asm-3.0.4.RELEASE.jar
org.springframework.aspects-3.0.4.RELEASE.jar
org.springframework.beans-3.0.4.RELEASE.jar
org.springframework.context-3.0.4.RELEASE.jar
org.springframework.context.support-3.0.4.RELEASE.jar
org.springframework.core-3.0.4.RELEASE.jar
org.springframework.expression-3.0.4.RELEASE.jar
org.springframework.instrument-3.0.4.RELEASE.jar
org.springframework.instrument.tomcat-3.0.4.RELEASE.jar
org.springframework.jdbc-3.0.4.RELEASE.jar
org.springframework.jms-3.0.4.RELEASE.jar
org.springframework.orm-3.0.4.RELEASE.jar
org.springframework.oxm-3.0.4.RELEASE.jar
org.springframework.test-3.0.4.RELEASE.jar
org.springframework.transaction-3.0.4.RELEASE.jar
org.springframework.web-3.0.4.RELEASE.jar
org.springframework.web.portlet-3.0.4.RELEASE.jar
org.springframework.web.servlet-3.0.4.RELEASE.jar
org.springframework.web.struts-3.0.4.RELEASE.jar

从spring-framework-2.0.8-with-dependencies.zip中spring-framework-2.0.8\lib\log4j提取如下包
log4j-1.2.14.jar

从hibernate-distribution-3.6.0.Final-dist.zip中hibernate-distribution-3.6.0.Final提取如下包
hibernate3.jar

从hibernate-distribution-3.6.0.Final-dist.zip中hibernate-distribution-3.6.0.Final\lib\required提取如下包(所有包,除了commons-collections-3.1.jar,struts里的版本要高一些)
antlr-2.7.6.jar
dom4j-1.6.1.jar
javassist-3.12.0.GA.jar
jta-1.1.jar
slf4j-api-1.6.1.jar

从hibernate-distribution-3.6.0.Final-dist.zip中hibernate-distribution-3.6.0.Final\lib\bytecode\cglib提取如下包
cglib-2.2.jar

从hibernate-distribution-3.6.0.Final-dist.zip中hibernate-distribution-3.6.0.Final\lib\optional\ehcache提取如下包
ehcache-1.5.0.jar

从hibernate-distribution-3.6.0.Final-dist.zip中hibernate-distribution-3.6.0.Final\lib\jpa提取如下包
hibernate-jpa-2.0-api-1.0.0.Final.jar

从hibernate-distribution-3.6.0.Final-dist.zip中hibernate-distribution-3.6.0.Final\lib\optional\c3p0提取如下包
c3p0-0.9.1.jar

从struts2-2.2.1-lib.zip中struts-2.2.1\lib提取如下包
aopalliance-1.0.jar
commons-collections-3.2.jar
commons-fileupload-1.2.1.jar
commons-io-1.3.2.jar
commons-logging-1.0.4.jar
freemarker-2.3.16.jar
ognl-3.0.jar
struts2-core-2.2.1.jar
struts2-spring-plugin-2.2.1.jar
xwork-core-2.2.1.jar

从mysql-connector-java-5.1.13.zip中mysql-connector-java-5.1.13提取如下包
mysql-connector-java-5.1.13-bin.jar

从slf4j-1.6.1.zip中mysql-connector-java-5.1.13提取如下包
slf4j-log4j12-1.6.1.jar

把这些包拷贝到一个Spring3.0.4_Struts2.2.1_Hibernate3.6.0-lib文件夹中(这个文件夹自己创建一个,主要用途是临时保存这些包)

3.安装JDK和解压Eclipse文件

4.Eclipse配置Tomcat和JDK
a.启动Eclipse (我用的是Eclipse 3.6 for JEE)
Window-->Preferences-->Server-->Runtime Environments-->Add...-->Apache-->Apache Tomcat v7.0-->Next-->Browse...-->
指向Tomcat 7.0的安装路径-->确定-->Installed JREs...-->Add...-->Standard VM-->Next-->Directory...-->
执行jdk1.6.0_21的文件夹(我的在C:\Program Files\Java\jdk1.6.0_21)-->Finish-->选择jdk1.6.0_21-->OK-->
JRE: jdk1.6.0_21-->Finish-->OK

5.创建第1个例子
a.创建Web项目
点击菜单File-->New-->Other...-->Web-->Dynamic Web Project-->Next-->Project name: Spring3_HelloWorld-->
Target runtime: Apache Tomcat v7.0-->Next-->Next-->Content directory: WebRoot (把WebContent改成WebRoot的原因希望能在MyEclipse配合起来)-->
-->选择Generate web.xml deployment descriptor-->Finish-->

b.配置项目为UTF-8格式打开
右键点击Spring3_HelloWorld-->Properties-->Resource-->Text file encoding 选择Other: UTF-8-->OK

c.配置程序
1)在Spring3_HelloWorld\WebRoot\WEB-INF\lib下,把提取出来的放在Spring3.0.4_Struts2.2.1_Hibernate3.6.0-lib文件夹中的jar包拷贝到这里-->
右键点击Spring3_HelloWorld-->Build Path-->Configure Build Path...-->Libraries-->Add JARs-->选中Spring3_HelloWorld\WebRoot\WEB-INF\lib下所有的Jar包(按住Shift从头选到尾)-->
-->OK-->OK-->
修改Spring3_HelloWorld\WebRoot\WEB-INF下的web.xml
修改成如下:

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="person" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>quickstart</display-name>
<!-- Include this if you are using Hibernate -->
<filter>
<filter-name>Spring OpenEntityManagerInViewFilter</filter-name>
<filter-class>
org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter
</filter-class>
</filter>

<filter-mapping>
<filter-name>Spring OpenEntityManagerInViewFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.FilterDispatcher
</filter-class>
</filter>

<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- 以下3项参数与log4j的配置相关 -->

<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>/WEB-INF/log4j.properties</param-value>
</context-param>

<context-param>
<param-name>log4jRefreshInterval</param-name>
<param-value>60000</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.util.Log4jConfigListener
</listener-class>
</listener>
<!-- end -->
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>

<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>

保存-->

2)右键点击Spring3_HelloWorld\Java Resources: src-->New-->Other...-->General-->File-->Next-->
Enter or select the parent folder: Spring3_HelloWorld/src , File name: struts.xml-->
Finish-->struts.xml修改成如下:

<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<include file="struts-default.xml"/>
</struts>

-->保存-->

3)右键点击Spring3_HelloWorld\Java Resources: src-->New-->Other...-->General-->Folder-->Next-->
Enter or select the parent folder: Spring3_HelloWorld/src , Folder name: META-INF-->
Finish-->
右键点击Spring3_HelloWorld\Java Resources: src\META-INF-->New-->Other...-->General-->File-->Next-->
Enter or select the parent folder: Spring3_HelloWorld/src/META-INF , File name: persistence.xml-->
Finish-->
persistence.xml修改成如下:

<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"
version="1.0">
<persistence-unit name="punit">
</persistence-unit>
</persistence>


-->保存-->

4)右键点击Spring3_HelloWorld\WebRoot\WEB-INF-->New-->File-->File name: log4j.properties-->
Finish-->struts.xml修改成如下:

# Set root logger level to error
log4j.rootLogger=INFO, Console, File

###### Console appender definition #######

# All outputs currently set to be a ConsoleAppender.
log4j.appender.Console=org.apache.log4j.ConsoleAppender
log4j.appender.Console.layout=org.apache.log4j.PatternLayout
log4j.appender.Console.layout.ConversionPattern=%d{ABSOLUTE} %-5p [%c{3}] %m%n
#log4j.appender.Console.layout.ConversionPattern=%d{ABSOLUTE} %-5p [%c] %m%n

###### File appender definition #######
log4j.appender.File=org.apache.log4j.DailyRollingFileAppender
log4j.appender.File.File=spring.log
log4j.appender.File.Append=false
log4j.appender.File.layout=org.apache.log4j.PatternLayout
log4j.appender.File.layout.ConversionPattern=%d{ABSOLUTE} %-5p [%c] %m%n

-->保存-->

来源于网络,回归于网络。
我的邮箱:happy.every.day@126.com QQ:48399956
快乐!
2010年10月19日
...全文
2532 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
xwt799023 2011-05-23
  • 打赏
  • 举报
回复
可否把你的qq留下,交流下可以不
w520520134 2011-05-22
  • 打赏
  • 举报
回复
好新的整合。谢谢
celebi825 2011-01-28
  • 打赏
  • 举报
回复
写的很详细,照着配,终于成功了~
yousite1 2010-10-20
  • 打赏
  • 举报
回复
谢谢分享。
merryboy 2010-10-19
  • 打赏
  • 举报
回复

6)修改Spring3_HelloWorld\WebRoot\WEB-INF下的applicationContext.xml文件
修改成如下:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">

<bean
class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />

<bean id="personService" class="myssh.service.impl.PersonServiceImpl" />

<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/myssh" />
<property name="username" value="root" />
<property name="password" value="**" />
</bean>

<bean id="personAction" scope="prototype"
class="myssh.action.PersonAction">
<constructor-arg ref="personService" />
</bean>

<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="database" value="MYSQL" />
<property name="showSql" value="true" />
</bean>
</property>
</bean>

<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>

<tx:annotation-driven transaction-manager="transactionManager" />

</beans>

**是MySQL的密码。自己修改一下。
Note that the "class" attribute of the bean "personAction" is set to the name of the action class,
and the "personService" bean will be passed as a parameter to the action constructor.
Change the "url", "username" and "password" in the "dataSource" bean to the appropiate values for your database.
For more details on the rest of the beans on this file, see Spring's documentation.
The "scope" attribute is new in Spring 2, and it means that Spring will create a new PersonAction object every time an object of that type is requested.
In Struts 2 a new action object is created to serve each request, that's why we need scope="prototype".

注意personAction bean的class的属性被设置成action的class。
personService将会以一个参数的身份传入action的构造函数。
修改在dataSource bean里面的url ,username password 参数,适应你的数据库。
scope="prototype"是Spring2所新带的属性,表明每次Spring都会创建一个PersonAction。
表明每个请求时,Spring都会创建一个新的注入。

7)修改Spring3_HelloWorld\Java Resources: src\下的struts.xml文件
修改成如下:
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<include file="struts-default.xml"/>
<constant name="struts.objectFactory" value="spring" />
<constant name="struts.devMode" value="true" />

<package name="person" extends="struts-default">
<action name="list" class="personAction" method="execute">
<result>list.jsp</result>
<result name="input">list.jsp</result>
</action>

<action name="remove" class="personAction" method="remove">
<result>list.jsp</result>
<result name="input">list.jsp</result>
</action>

<action name="save" class="personAction" method="save">
<result>list.jsp</result>
<result name="input">list.jsp</result>
</action>
</package>
</struts>

Setting "struts.objectFactory" to "spring" will force Struts to instantiate the actions using Spring,
injecting all the defined dependencies on applicationContext.xml.
The "class" attribute for each action alias is set to "personAction",
which is the bean id that we defined on applicationContext.xml for the PersonAction class.
This is all that is needed to make Struts work with Spring.

struts.objectFactory将强制Struts用Spring去初始化action实例,将依赖applicationContext.xml文件。
class属性表明每个action与personAction是别名的, personAction我们是在applicationContext.xml文件中定义的。
这些将导致Struts将依赖Spring工作。

8)添加界面代码
右键点击Spring3_HelloWorld\WebRoot-->New-->JSP File-->Enter or select the parent folder: Spring3_HelloWorld/WebRoot-->
File name: index.jsp-->Finish-->
代码如下:
<%@ page contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Say Hello</title>
</head>
<body>
<s:form action="list">
<s:submit value="显示用户" />
</s:form>
</body>
</html>



9)添加界面代码
右键点击Spring3_HelloWorld\WebRoot-->New-->JSP File-->Enter or select the parent folder: Spring3_HelloWorld/WebRoot-->
File name: list.jsp-->Finish-->
代码如下:
<%@ page contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<p>Persons</p>
<s:if test="persons.size > 0">
<table>
<s:iterator value="persons">
<tr id="row_<s:property value="id"/>">
<td><s:property value="firstName" /></td>
<td><s:property value="lastName" /></td>
</tr>
</s:iterator>
</table>
</s:if>

10)重新部署
然后点中Tomcat v7.0 Server at localhost [Started, Synchronized]右击-->Stop-->
点中Tomcat v7.0 Server at localhost [Stopped, Synchronized]右击-->Publish-->
再点中Tomcat v7.0 Server at localhost [Stopped, Synchronized]右击-->Start-->
打开IE,地址栏中输入: http://localhost:8080/Spring3_HelloWorld/index.jsp 再点击 显示用户 按钮就可以看见结果。


来源于网络,回归于网络。
我的邮箱:happy.every.day@126.com QQ:48399956
快乐!
2010年10月19日
merryboy 2010-10-19
  • 打赏
  • 举报
回复
5)右键点击Spring3_HelloWorld\WebRoot\WEB-INF-->New-->File-->File name: applicationContext.xml-->
Finish-->applicationContext.xml修改成如下:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">

<bean
class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />

<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="database" value="MYSQL" />
<property name="showSql" value="true" />
</bean>
</property>
</bean>

<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>

<tx:annotation-driven transaction-manager="transactionManager" />

</beans>


-->保存-->

6)右键点击Spring3_HelloWorld\WebRoot-->New-->HTML File-->File name: index.html-->Finish-->修改index.html修改成如下:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Hello World</title>
</head>
<body>
<h3>Hello World!</h3>
</body>
</html>

-->保存-->

选择tool-->Window-->Show View-->Servers-->右键点击Servers视图的空白处-->New-->Server-->Tomcat v7.0 Server-->Next-->
选择Spring3_HelloWorld-->选择Add > 按钮-->Finish-->点中Servers视图中的Tomcat v7.0 Server at localhost [Stopped]-->右击-->
Start-->打开IE,地址栏中输入: http://localhost:8080/Spring3_HelloWorld/-->如果网页显示 Hello World!这表明Web已经配置-->
到这里已经完成了一个Struts+Spring+Hibernate框架的搭建.


6.第一个Struts 2+Spring 3+Hibernate 3应用程序--Hello World
1)创建一个表
数据库名为myssh (Create new Schema)
表名person

CREATE TABLE `myssh`.`person` (
`id` INTEGER UNSIGNED NOT NULL AUTO_INCREMENT,
`firstName` VARCHAR(45) NOT NULL,
`lastName` VARCHAR(45) NOT NULL,
PRIMARY KEY (`id`)
)
ENGINE = InnoDB;

向person表里随便加一些数据。
我这里加的
id 1
firstName Sc
lastName GPS



2)数据库实体: Person
右键点击Spring3_HelloWorld\Java Resources: src-->New-->Class-->Package: myssh.model-->Name: Person-->Finish-->
代码如下:
package myssh.model;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;

@Entity
public class Person {
@Id
@GeneratedValue
private Integer id;
private String lastName;
private String firstName;

public String getFirstName() {
return firstName;
}

public void setFirstName(String firstName) {
this.firstName = firstName;
}

public String getLastName() {
return lastName;
}

public void setLastName(String lastName) {
this.lastName = lastName;
}

public Integer getId() {
return id;
}

public void setId(Integer id) {
this.id = id;
}
}

@Entity will let the provider know that this class can be persisted.
@Id marks the "id" field as the primary key for this class.
@GeneratedValue will cause the id field to be generated by the provider (Hibernate).
Classes and fields are by default mapped to tables and columns with the same name,
see JPA's documentation for more details.

@Entity表明是持久化的。
@Id表明id这个变量是主键。
@GeneratedValue表明id这个值有Hibernate控制增加。
类被映射到名称相同的表上。

3)实体的服务接口: PersonService
右键点击Spring3_HelloWorld\Java Resources: src-->New-->Interface-->Package: myssh.service-->Name: PersonService-->Finish-->
代码如下:
package myssh.service;

import java.util.List;
import myssh.model.Person;

public interface PersonService {
public List<Person> findAll();

public void save(Person person);

public void remove(int id);

public Person find(int id);
}

4)实体的服务接口实现: PersonServiceImpl
右键点击Spring3_HelloWorld\Java Resources: src-->New-->Class-->Package: myssh.service.impl-->Name: PersonServiceImpl-->Finish-->
代码如下:
package myssh.service.impl;

import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import org.springframework.transaction.annotation.Transactional;
import myssh.model.Person;
import myssh.service.PersonService;

@Transactional
public class PersonServiceImpl implements PersonService {
private EntityManager em;

@PersistenceContext
public void setEntityManager(EntityManager em) {
this.em = em;
}

@SuppressWarnings("unchecked")
public List<Person> findAll() {
Query query = getEntityManager().createQuery("select p FROM Person p");
return query.getResultList();
}

public void save(Person person) {
if (person.getId() == null) {
// new
em.persist(person);
} else {
// update
em.merge(person);
}
}

public void remove(int id) {
Person person = find(id);
if (person != null) {
em.remove(person);
}
}

private EntityManager getEntityManager() {
return em;
}

public Person find(int id) {
return em.find(Person.class, id);
}

}
@PersistenceContext will make Spring inject an EntityManager into the service when it is instantiated.
The @PersistenceContext annotation can be placed on the field, or on the setter method.
If the class is annotated as @Transactional, Spring will make sure that its methods run inside a transaction.

@PersistenceContext将会让Spring依赖注入EntityManager服务
@PersistenceContext注解可以放在类的定义前面,也可以放在set方法的前面。
@Transactional注解让Spring确认方法是运行在一个事物中。

5)Struts的Action实现: PersonAction
右键点击Spring3_HelloWorld\Java Resources: src-->New-->Class-->Package: myssh.action-->Name: PersonAction-->Finish-->
代码如下:
package myssh.action;

import java.util.List;
import myssh.model.Person;
import myssh.service.PersonService;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.Preparable;

public class PersonAction implements Preparable {
private PersonService service;
private List<Person> persons;
private Person person;
private Integer id;

public PersonAction(PersonService service) {
this.service = service;
}

public String execute() {
this.persons = service.findAll();
return Action.SUCCESS;
}

public String save() {
this.service.save(person);
this.person = new Person();
return execute();
}

public String remove() {
service.remove(id);
return execute();
}

public List<Person> getPersons() {
return persons;
}

public Integer getId() {
return id;
}

public void setId(Integer id) {
this.id = id;
}

public void prepare() throws Exception {
if (id != null)
person = service.find(id);
}

public Person getPerson() {
return person;
}

public void setPerson(Person person) {
this.person = person;
}
}

Look mom my action is a simple POJO!
The "Preparable" interface instructs Struts to call the "prepare" method
if the "PrepareInterceptor" is applied to the action (by default, it is).
The constructor of the action takes a "PersonService" as a parameter,
which Spring will take care of passing when the action is instatiated.

这个action就是一个POJO,java简单对象。
Preparable这个接口告诉Struts去调用prepare方法
构造函数将在action初始化的时候,被Spring注入PersonService参数。这个参数在applicationContext.xml被定义为prototype,
表明每次action初始化的时候,都有一个新的PersonService对象被注入。

来源于网络,回归于网络。
我的邮箱:happy.every.day@126.com QQ:48399956
快乐!
2010年10月19日
内容概要:本文针对考虑算力负荷时空迁移特性的多微电网与共享储能系统的协同优化调度问题展开研究,并提供了完整的Matlab代码实现。研究构建了融合算力负荷动态迁移特征的数学优化模型,通过引入共享储能机制,实现多微电网间的能量互济与资源协同,有效提升系统对分布式能源波动性和负荷不确定性的适应能力。文中采用先进的优化算法求解该调度模型,重点解决了算力任务在时空维度上的灵活调配与电力供需平衡之间的耦合关系,旨在提高综合能源系统的运行经济性、可靠性与灵活性。所提出的方法为未来能源互联网背景下电-算协同管理提供了理论支持与技术路径。; 适合人群:具备电力系统分析、优化理论基础及Matlab编程能力的科研人员、高校研究生,尤其适用于从事微电网运行、共享储能配置、综合能源系统优化以及电-算融合等领域研究的专业技术人员。; 使用场景及目标:①开展多微电网与共享储能系统的协同调度策略设计与仿真验证;②支撑高比例可再生能源接入下的新型电力系统优化运行研究;③为考虑算力迁移的数据中心与电网协同调度提供建模与算法参考;④作为高水平学术论文撰写或学位课题研究的技术支撑与代码复现平台。; 阅读建议:建议读者结合Matlab代码与相关学术文献深入研读,重点关注目标函数构建、约束条件设定及求解器调用逻辑,可在现有模型基础上拓展多时间尺度优化、不确定性建模(如鲁棒优化、随机规划)或加入实际工程约束进行二次开发与深化研究。

52,780

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 Ajax
社区管理员
  • Ajax
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧