一、Struts2的配置
见上文:Struts2的应用
二、整合spring
这里主要使用spring的控制反转注入,来管理bean。奖action的对应bean都由spring来注入管理。
1、项目目录
这里依旧采用maven来管理项目
2、maven的pox.xml配置文件,引入对应所需的jar包
1 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 2 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> 3 <modelVersion>4.0.0</modelVersion> 4 <groupId>com.mdf</groupId> 5 <artifactId>Struts2Demo</artifactId> 6 <packaging>war</packaging> 7 <version>0.0.1-SNAPSHOT</version> 8 <name>Struts2Demo Maven Webapp</name> 9 <url>http://maven.apache.org</url> 10 <dependencies> 11 <dependency> 12 <groupId>junit</groupId> 13 <artifactId>junit</artifactId> 14 <version>3.8.1</version> 15 <scope>test</scope> 16 </dependency> 17 <!-- 使用maven 引入 struts2 jar包 --> 18 <dependency> 19 <groupId>org.apache.struts</groupId> 20 <artifactId>struts2-core</artifactId> 21 <version>2.3.1.2</version> 22 </dependency> 23 <!-- 引入server-api包 --> 24 <dependency> 25 <groupId>javax.servlet</groupId> 26 <artifactId>javax.servlet-api</artifactId> 27 <version>3.0.1</version> 28 </dependency> 29 <!-- 引入spring核心jar包 --> 30 <dependency> 31 <groupId>org.springframework</groupId> 32 <artifactId>spring-context</artifactId> 33 <version>4.0.4.RELEASE</version> 34 </dependency> 35 <!-- 引入Struts2和Spring整合插件 --> 36 <dependency> 37 <groupId>org.apache.struts</groupId> 38 <artifactId>struts2-spring-plugin</artifactId> 39 <version>2.3.4.1</version> 40 </dependency> 41 </dependencies> 42 <build> 43 <finalName>Struts2Demo</finalName> 44 </build> 45 </project>
3、web.xml配置文件
1 <web-app> 2 <display-name>Archetype Created Web Application</display-name> 3 <!-- Struts2过滤器 --> 4 <filter> 5 <filter-name>struts2</filter-name> 6 <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class> 7 </filter> 8 <filter-mapping> 9 <filter-name>struts2</filter-name> 10 <url-pattern>/*</url-pattern> 11 </filter-mapping> 12 13 <!-- spring坚挺,配置文件目录 --> 14 <listener> 15 <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 16 </listener> 17 <context-param> 18 <param-name>contextConfigLocation</param-name> 19 <param-value>classpath:spring.xml</param-value> 20 </context-param> 21 </web-app>
4、spring.xml(spring的配置文件)
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns ="http://www.springframework.org/schema/beans" 3 xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation ="http://www.springframework.org/schema/beans 5 http://www.springframework.org/schema/beans/spring-beans.xsd"> 6 7 <!-- 注入service --> 8 <bean name="StudentService" class="com.mdf.service.impl.StudentServiceImpl"></bean> 9 <!-- 注入action的类 --> 10 <bean id="StudentAction" class="com.mdf.action.StudentAction"> 11 <!-- property注入的与action对应类中声明的需要一致 --> 12 <property name="studentservice"> 13 <ref bean="StudentService"></ref> 14 </property> 15 </bean> 16 </beans>
这里提个醒,在注入service和action的时候,需要对应action类中的实例化service名,我在这里吃了不少亏,一直报错。在注释中也标注了,大家需要注意。
这里将所有的bean的注入都在spring中控制了,之后Struts2中action控制的时候,class只需要引入spring中设置的对应action的bean名就行了。
5、struts.xml(Struts2的配置文件)
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE struts PUBLIC 3 "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" 4 "http://struts.apache.org/dtds/struts-2.0.dtd"> 5 6 <struts> 7 <!-- 利用spring来初始化action的bean --> 8 <constant name="struts.objectFactory" value="spring" /> 9 10 <package name="Student" namespace="/student" extends="struts-default"> 11 <action name="test" class="StudentAction"> 12 <result name="success">/WEB-INF/context/success.jsp</result> 13 <result name="error">/WEB-INF/context/error.jsp</result> 14 </action> 15 </package> 16 </struts>
6、对应测试的action、service类
action:
1 package com.mdf.action; 2 3 import com.mdf.bean.Student; 4 import com.mdf.service.StudentService; 5 import com.opensymphony.xwork2.Action; 6 7 public class StudentAction implements Action { 8 9 //这里需要与spring的配置文件中对应 10 private StudentService studentservice; 11 private Student student; 12 13 public StudentService getStudentservice() { 14 return studentservice; 15 } 16 17 public void setStudentservice(StudentService studentservice) { 18 this.studentservice = studentservice; 19 } 20 21 public Student getStudent() { 22 return student; 23 } 24 25 public void setStudent(Student student) { 26 this.student = student; 27 } 28 29 public String execute(){ 30 student=studentservice.getStudent(); 31 if(student.getName()=="madifei"){ 32 return "success"; 33 }else{ 34 return "error"; 35 } 36 } 37 }
service:
1 package com.mdf.service.impl; 2 3 import com.mdf.bean.Student; 4 import com.mdf.service.StudentService; 5 6 public class StudentServiceImpl implements StudentService { 7 8 public Student getStudent() { 9 // TODO Auto-generated method stub 10 Student returnValue=null; 11 12 Student student=new Student(); 13 student.setName("madifei"); 14 student.setAge(28); 15 16 returnValue=student; 17 18 return returnValue; 19 } 20 21 }
三、感悟
这只是基础的spring+Struts2的整合应用吧,对于框架的了解还是甚少,虽然仔细百度了不少有关spring和Struts2的知识,对于原理还是有些一知半解。后期应用上要不断地去理解,不该只是能够挑出程序功能即可。