Maven项目下spring3 ,struts2整合入门。
spring 3 的配置文件,名称spring.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:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd"> <context:component-scan base-package="com.mario.server.imp" /> </beans>
入门的内容所以刨除了令新手看不懂的属性 ,context:component-scan 是自动包扫描 base-package是要扫描的包名字。这里使用注入的方式,否则要写bean,但是一旦这个包中bean 很多的时候扫描法会影响性能,bean的写法就不说了,指定id,class什么的你懂的。
下一步需要导入spring 3所需要的包3.2版本(最后的pom.xml中有),如果缺包,在编译时往往提示classNotfound根据那个类名,根据缺的类可以在网上找类对应的jar包导入即可。
之后写类 ,这里我的类是UserServer,代码如下:
import org.apache.log4j.Logger; import org.springframework.stereotype.Service; import com.mario.serverI.UserServerI; @Service("userServer") public class UserServerImp implements UserServerI{ /** * Logger for this class */ private static final Logger logger = Logger.getLogger(UserServerImp.class); @Override public void test() { // TODO Auto-generated method stub logger.info("mssage from UserServer"); } }
这里注意下 @Service这个注解,这个是注入中不可缺少的,里面的名字什么时候用?往下看。。。。
写一个junit测试类,用来测试spring是否配置好。测试代码如下,@test这个注解需要导入junit包:
import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.mario.server.imp.UserServerImp; public class mytest { /** * Logger for this class */ private static final Logger logger = Logger.getLogger(mytest.class); @Test public void test(){ ApplicationContext ac=new ClassPathXmlApplicationContext(new String[]{"classpath:spring.xml"}); UserServerImp us= (UserServerImp) ac.getBean("userServer"); us.test(); } }
applicationContext对象用来获取spring 的context ,之后通过context的getBean方法获取UserService对象,getBean的参数userServer,就是UserServer类中@Server 里面写的那个userServer。 这里它怎么知道userServer就是UserServer中的注解并实例化UserServer类? 这是因为自动扫描包,在spring配置文件中指定了包含该类的包名,然后spring框架会扫描这个包中的类寻找@Server这个注解,如果有个值为userServer的,spring就实例化了。
测试代码中能打印出test方法中的信息,spring 配置成功,在将其配置到web项目上,web.xml配置如下:
<?xml version="1.0" encoding="UTF-8"?> <web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"> <display-name></display-name> <!-- spring 配置文件路径 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring.xml</param-value> </context-param> <!-- spring 监听器 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
Spring3 配置就完事了,接下来加struts2。
第一步还是加包这个略,后面pom.xml里面有,三个包,struts2-core,struts2-convension-plugin,struts2-spring-plugin,加了这三个后maven会自动加其他需要的包。
stuts2的配置文件:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd"> <struts> <!-- struts2的action必须放在一个指定的包空间下定义 --> <package name="basePackage" extends="struts-default"></package> </struts>
package name 包名,不用注入的方式写的话就是在这个标签下写action ,指定class什么的,你懂的,不懂的可以百度下。这里因为注入所以很干净。
接下来写action类,代码如下:
import com.mario.serverI.UserServerI; @ParentPackage("basePackage") @Namespace("/") @Action("userAction") public class UserAction { /** * Logger for this class */ private static final Logger logger = Logger.getLogger(UserAction.class); private UserServerI userServer; public UserServerI getUserServer() { return userServer; } @Autowired public void setUserServer(UserServerI userServer) { this.userServer = userServer; } public void test(){ logger.info("message from struts2"); userServer.test(); } }
注意@ParentPackage这个注解,这里面的base-package 就是配置文件中的package-name 指定的,@NameSpace是名字空间,Maven项目中默认用“/”
@Action就是这个类的访问时用的名字,比如:localhost:8080/项目名/userAction!test.action 中的userAction 。
在这个Action中引用了userServer这个类,但test放法中并没有实例化该类,怎么做到的? 首先定义userServer类的get,set 方法,之后在set方法上加@AutoWirte注解就可以了,很方便吧?
之后再web.xml中配置struts2,加入后的配置文件如下:
<?xml version="1.0" encoding="UTF-8"?> <web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"> <display-name></display-name> <!-- spring 配置文件路径 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring.xml</param-value> </context-param> <!-- struts2 过滤器 --> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>*.action</url-pattern> </filter-mapping> <!-- spring 监听器 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
加入了struts2的过滤器,filter 和filter-mapping。之后部署项目测试。localhost:8080/项目名/userAction!test.action。如果没有问题会显示空白,因为action中没有result。
最后是Maven的pom.xml
<?xml version="1.0"?> <project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <modelVersion>4.0.0</modelVersion> <groupId>myWebProJect</groupId> <artifactId>myWebProJect</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>war</packaging> <name></name> <description></description> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> <version>1.2</version> <scope>provided</scope> </dependency> <dependency> <groupId>javax.servlet.jsp</groupId> <artifactId>jsp-api</artifactId> <version>2.1</version> <scope>provided</scope> </dependency> <dependency> <groupId>org.glassfish</groupId> <artifactId>javax.annotation</artifactId> <version>3.0.1</version> </dependency> <dependency> <groupId>org.glassfish</groupId> <artifactId>javax.ejb</artifactId> <version>3.0.1</version> </dependency> <dependency> <groupId>org.jboss.weld</groupId> <artifactId>weld-osgi-bundle</artifactId> <version>1.0.1-SP3</version> </dependency> <dependency> <groupId>org.glassfish</groupId> <artifactId>javax.servlet</artifactId> <version>3.0.1</version> </dependency> <dependency> <groupId>antlr</groupId> <artifactId>antlr</artifactId> <version>2.7.7</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.6.1</version> </dependency> <dependency> <groupId>dom4j</groupId> <artifactId>dom4j</artifactId> <version>1.6.1</version> </dependency> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-core</artifactId> <version>4.1.4.Final</version> </dependency> <dependency> <groupId>org.hibernate.common</groupId> <artifactId>hibernate-commons-annotations</artifactId> <version>4.0.1.Final</version> </dependency> <dependency> <groupId>org.hibernate.javax.persistence</groupId> <artifactId>hibernate-jpa-2.0-api</artifactId> <version>1.0.1.Final</version> </dependency> <dependency> <groupId>org.javassist</groupId> <artifactId>javassist</artifactId> <version>3.15.0-GA</version> </dependency> <dependency> <groupId>org.jboss.logging</groupId> <artifactId>jboss-logging</artifactId> <version>3.1.0.GA</version> </dependency> <dependency> <groupId>org.jboss.spec.javax.transaction</groupId> <artifactId>jboss-transaction-api_1.1_spec</artifactId> <version>1.0.0.Final</version> </dependency> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-entitymanager</artifactId> <version>4.1.4.Final</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>3.2.3.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>3.2.3.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>3.2.3.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>3.2.3.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-expression</artifactId> <version>3.2.3.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-orm</artifactId> <version>3.2.3.RELEASE</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope/> </dependency> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> </dependency> <dependency> <groupId>org.apache.struts</groupId> <artifactId>struts2-core</artifactId> <version>2.3.15.1</version> </dependency> <dependency> <groupId>org.apache.struts</groupId> <artifactId>struts2-spring-plugin</artifactId> <version>2.3.15.1</version> </dependency> <dependency> <groupId>org.apache.struts</groupId> <artifactId>struts2-convention-plugin</artifactId> <version>2.3.15.1</version> </dependency> </dependencies> <build> <plugins> <plugin> <artifactId>maven-war-plugin</artifactId> </plugin> <plugin> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.6</source> <target>1.6</target> </configuration> </plugin> </plugins> </build> </project>