配置文件
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.lgp</groupId> 5 <artifactId>maven_struts_spring</artifactId> 6 <packaging>war</packaging> 7 <version>0.0.1-SNAPSHOT</version> 8 <name>maven_ssh1 Maven Webapp</name> 9 <url>http://maven.apache.org</url> 10 <properties> 11 <project.build.sourceEncoding> 12 UTF-8 13 </project.build.sourceEncoding> 14 </properties> 15 16 <dependencies> 17 <dependency> 18 <groupId>junit</groupId> 19 <artifactId>junit</artifactId> 20 <version>3.8.1</version> 21 <scope>test</scope> 22 </dependency> 23 <!-- https://mvnrepository.com/artifact/commons-logging/commons-logging --> 24 <dependency> 25 <groupId>commons-logging</groupId> 26 <artifactId>commons-logging</artifactId> 27 <version>1.2</version> 28 </dependency> 29 30 <!-- 添加struts2依赖 --> 31 <dependency> 32 <groupId>org.apache.struts</groupId> 33 <artifactId>struts2-core</artifactId> 34 <version>2.3.31</version> 35 </dependency> 36 <!-- https://mvnrepository.com/artifact/org.apache.struts/struts2-spring-plugin --> 37 <dependency> 38 <groupId>org.apache.struts</groupId> 39 <artifactId>struts2-spring-plugin</artifactId> 40 <version>2.3.31</version> 41 </dependency> 42 <!-- 添加Spring依赖 --> 43 <dependency> 44 <groupId>org.springframework</groupId> 45 <artifactId>spring-core</artifactId> 46 <version>3.1.1.RELEASE</version> 47 </dependency> 48 49 <dependency> 50 <groupId>org.springframework</groupId> 51 <artifactId>spring-beans</artifactId> 52 <version>3.1.1.RELEASE</version> 53 </dependency> 54 55 <dependency> 56 <groupId>org.springframework</groupId> 57 <artifactId>spring-context</artifactId> 58 <version>3.1.1.RELEASE</version> 59 </dependency> 60 61 <dependency> 62 <groupId>org.springframework</groupId> 63 <artifactId>spring-jdbc</artifactId> 64 <version>3.1.1.RELEASE</version> 65 </dependency> 66 </dependencies> 67 <build> 68 <finalName>maven_ssh1</finalName> 69 <plugins> 70 <plugin> 71 <artifactId>maven-compiler-plugin</artifactId> 72 <version>2.3.1</version> 73 <configuration> 74 <source>1.7</source> 75 <target>1.7</target> 76 <encoding>UTF-8</encoding> 77 </configuration> 78 </plugin> 79 <plugin> 80 <groupId>org.apache.maven.plugins</groupId> 81 <artifactId>maven-surefire-plugin</artifactId> 82 <version>2.12.4</version> 83 <configuration> 84 <testFailureIgnore>true</testFailureIgnore> 85 </configuration> 86 </plugin> 87 </plugins> 88 </build> 89 </project>
pom.xml
网上有很多用junit教你搭的,虽然都能跑,但是如果要实际运行和前台交互,那样是不行的,还要导入我所导入的包。
1 <?xml version="1.0" encoding="UTF-8"?> 2 <web-app xmlns:web="http://xmlns.jcp.org/xml/ns/javaee"> 3 <display-name>Archetype Created Web Application</display-name> 4 <filter> 5 <filter-name>struts2</filter-name> 6 <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter 7 </filter-class> 8 </filter> 9 <filter-mapping> 10 <filter-name>struts2</filter-name> 11 <url-pattern>/*</url-pattern> 12 </filter-mapping> 13 <listener> 14 <listener-class>org.springframework.web.context.ContextLoaderListener 15 </listener-class> 16 </listener> 17 <context-param> 18 <param-name>contextConfigLocation</param-name> 19 <param-value>classpath:applicationContext.xml</param-value> 20 </context-param> 21 </web-app>
web.xml
则多了sprig的配置,留意<context-param>,自带的web.xml会报错说版本不对,只要把开头的那一坨玩意去掉即可。
其他就是在resource文件夹下的配置文件
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" xmlns:aop="http://www.springframework.org/schema/aop" 4 xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" 5 xsi:schemaLocation=" 6 http://www.springframework.org/schema/beans 7 http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 8 http://www.springframework.org/schema/tx 9 http://www.springframework.org/schema/tx/spring-tx-3.0.xsd 10 http://www.springframework.org/schema/aop 11 http://www.springframework.org/schema/aop/spring-aop-3.0.xsd 12 http://www.springframework.org/schema/context 13 http://www.springframework.org/schema/context/spring-context-3.0.xsd"> 14 <bean id="userDao" class="com.dao.impl.UserDao"></bean> 15 <bean id="userService" class="com.service.impl.UserService"> 16 <property name="userDao" ref="userDao"></property> 17 </bean> 18 <bean id="userAction" class="com.action.UserAction"> 19 <property name="userService" ref="userService"></property> 20 </bean> 21 </beans>
applicationcontext.xml 使用的是依赖注入,这个方法在初学spring的时候,对spring有很好的理解,层层递进,关系明了,当然实际应用通常用注解注入,但是如果我那样写的话,就看不透它的运行轨迹了。
java代码
1 package com.service.impl; 2 3 import com.dao.IUserDao; 4 import com.service.IUserService; 5 6 public class UserService implements IUserService { 7 private IUserDao userDao; 8 9 public void setUserDao(IUserDao userDao) { 10 this.userDao = userDao; 11 } 12 13 public void add() { 14 System.out.println("UserService.add()"); 15 userDao.add(); 16 } 17 18 }
1 package com.service; 2 3 public interface IUserService { 4 public void add(); 5 }
1 package com.dao.impl; 2 3 import com.dao.IUserDao; 4 5 public class UserDao implements IUserDao{ 6 7 public void add() { 8 System.out.println("UserDao.add()"); 9 } 10 }
1 package com.dao; 2 3 public interface IUserDao { 4 public void add(); 5 }
package com.action; import com.service.IUserService; public class UserAction { private String name; private String pwd; private IUserService userService; public String add() { System.out.println("name--" + this.getName()); System.out.println("pwd---" + this.getPwd()); userService.add(); return "success"; } public void setUserService(IUserService userService) { this.userService = userService; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPwd() { return pwd; } public void setPwd(String pwd) { this.pwd = pwd; } }
留意是用接口来实现方法的。
时间: 2024-10-16 04:27:16