struts2+hibernate+spring配置版框架搭建以及简单测试(方便脑补)

为了之后学习的日子里加深对框架的理解和使用,这里将搭建步奏简单写一下,目的主要是方便以后自己回来脑补;

1:File--->New--->Other--->Maven--->Maven Project--->Next(之后界面如下所示:)

--->Next(点击next之后出现如下界面:选择最后一个 maven-archetype-webapp,然后点击next)

--->Next(点击next之后出现如下界面,然后选择好组织号,工程号,版本号即可),最后点击Finish

2:maven项目创建好之后会报错,这里先在pom.xml中导入jar包,导入方式如下所示:这个pom配置好之后点击保存会将jar包下载到本地仓库,这个学习maven的时候都配置过,这个自己下去脑补吧;

  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.bie.maven</groupId>
  5   <artifactId>ssh_xml</artifactId>
  6   <packaging>war</packaging>
  7   <version>0.0.1-SNAPSHOT</version>
  8   <name>ssh_xml Maven Webapp</name>
  9   <url>http://maven.apache.org</url>
 10   
 11   
 12   
 13   <!-- 配置一下jar包 -->
 14    <properties>
 15         <java-version>1.8</java-version>
 16         <junit-version>4.8.2</junit-version>
 17         <org.springframework-version>3.2.4.RELEASE</org.springframework-version>
 18         <jackson-mapper-asl-version>1.9.12</jackson-mapper-asl-version>
 19         <jackson-core-asl-version>1.9.12</jackson-core-asl-version>
 20         <mysql-connector-java-version>5.1.8</mysql-connector-java-version>
 21         
 22         <opensymphony-version>2.4.2</opensymphony-version>
 23         <freemarker-version>2.3.9</freemarker-version>
 24         <c3p0-version>0.9.1.2</c3p0-version>
 25         <commons-collections-version>1.0</commons-collections-version>
 26         <commons-fileupload-version>1.2.2</commons-fileupload-version>
 27         <org.apache.commons-version>3.1</org.apache.commons-version>
 28         <commons-codec-version>1.6</commons-codec-version>
 29         <dom4j-version>1.6.1</dom4j-version>
 30         <javax.servlet-version>1.2</javax.servlet-version>
 31         <aspectjweaver-version>1.6.6</aspectjweaver-version>
 32         <slf4j-log4j12-version>1.6.6</slf4j-log4j12-version>
 33         <log4j-version>1.2.16</log4j-version>
 34         <javax.servlet-jsp-version>2.0</javax.servlet-jsp-version>
 35         <cglib-version>2.2.2</cglib-version>
 36         <slf4j-api-version>1.6.6</slf4j-api-version>
 37         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
 38     </properties>
 39     <dependencies>
 40         <!-- 配置 junit4.8.2 -->
 41         <dependency>
 42             <groupId>junit</groupId>
 43             <artifactId>junit</artifactId>
 44             <version>${junit-version}</version>
 45             <type>jar</type>
 46         </dependency>
 47         <!-- 配置 spring -->
 48         <dependency>
 49             <groupId>org.springframework</groupId>
 50             <artifactId>spring-core</artifactId>
 51             <version>${org.springframework-version}</version>
 52         </dependency>
 53         <dependency>
 54             <groupId>org.springframework</groupId>
 55             <artifactId>spring-web</artifactId>
 56             <version>${org.springframework-version}</version>
 57         </dependency>
 58         <dependency>
 59             <groupId>org.springframework</groupId>
 60             <artifactId>spring-test</artifactId>
 61             <version>${org.springframework-version}</version>
 62         </dependency>
 63         <dependency>
 64             <groupId>org.springframework</groupId>
 65             <artifactId>spring-aspects</artifactId>
 66             <version>${org.springframework-version}</version>
 67         </dependency>
 68         <dependency>
 69             <groupId>org.springframework</groupId>
 70             <artifactId>spring-context-support</artifactId>
 71             <version>${org.springframework-version}</version>
 72         </dependency>
 73         <dependency>
 74             <groupId>org.springframework</groupId>
 75             <artifactId>spring-web</artifactId>
 76             <version>${org.springframework-version}</version>
 77         </dependency>
 78 
 79         <dependency>
 80             <groupId>org.springframework</groupId>
 81             <artifactId>spring-webmvc</artifactId>
 82             <version>${org.springframework-version}</version>
 83         </dependency>
 84         <dependency>
 85             <groupId>org.springframework</groupId>
 86             <artifactId>spring-jdbc</artifactId>
 87             <version>${org.springframework-version}</version>
 88         </dependency>
 89         <dependency>
 90             <groupId>org.springframework</groupId>
 91             <artifactId>spring-tx</artifactId>
 92             <version>${org.springframework-version}</version>
 93         </dependency>
 94         <dependency>
 95             <groupId>org.springframework</groupId>
 96             <artifactId>spring-instrument</artifactId>
 97             <version>${org.springframework-version}</version>
 98         </dependency>
 99         <dependency>
100             <groupId>org.springframework</groupId>
101             <artifactId>spring-instrument-tomcat</artifactId>
102             <version>${org.springframework-version}</version>
103         </dependency>
104         
105         <dependency>  
106             <groupId>org.springframework</groupId>  
107             <artifactId>spring-orm</artifactId>  
108             <version>${org.springframework-version}</version>  
109         </dependency>
110         <!--springmvc 转换为 Json -->
111         <dependency>
112             <groupId>org.codehaus.jackson</groupId>
113             <artifactId>jackson-mapper-asl</artifactId>
114             <version>${jackson-mapper-asl-version}</version>
115         </dependency>
116         <dependency>
117             <groupId>org.codehaus.jackson</groupId>
118             <artifactId>jackson-core-asl</artifactId>
119             <version>${jackson-core-asl-version}</version>
120         </dependency>
121         <!--配置 freemarker -->
122         <dependency>
123             <groupId>freemarker</groupId>
124             <artifactId>freemarker</artifactId>
125             <version>${freemarker-version}</version>
126         </dependency>
127         
128         <!-- jdbc driver -->
129         <dependency>
130             <groupId>mysql</groupId>
131             <artifactId>mysql-connector-java</artifactId>
132             <version>${mysql-connector-java-version}</version>
133         </dependency>
134         <!-- connect pool with c3p0 -->
135         <dependency>
136             <groupId>c3p0</groupId>
137             <artifactId>c3p0</artifactId>
138             <version>${c3p0-version}</version>
139         </dependency>
140 
141         <dependency>
142             <groupId>commons-fileupload</groupId>
143             <artifactId>commons-fileupload</artifactId>
144             <version>${commons-fileupload-version}</version>
145         </dependency>
146 
147         <dependency>
148             <groupId>org.apache.commons</groupId>
149             <artifactId>commons-lang3</artifactId>
150             <version>${org.apache.commons-version}</version>
151         </dependency>
152 
153         <dependency>
154             <groupId>commons-codec</groupId>
155             <artifactId>commons-codec</artifactId>
156             <version>${commons-codec-version}</version>
157         </dependency>
158         <!-- analyze xml use dom4j -->
159         <dependency>
160             <groupId>dom4j</groupId>
161             <artifactId>dom4j</artifactId>
162             <version>${dom4j-version}</version>
163         </dependency>
164         <!-- about servlet -->
165         <dependency>
166             <groupId>javax.servlet</groupId>
167             <artifactId>jstl</artifactId>
168             <version>${javax.servlet-version}</version>
169         </dependency>
170 
171 
172         <dependency>
173             <groupId>taglibs</groupId>
174             <artifactId>standard</artifactId>
175             <version>1.1.2</version>
176         </dependency>
177         <dependency>
178             <groupId>javax.servlet</groupId>
179             <artifactId>javax.servlet-api</artifactId>
180             <version>3.0.1</version>
181             <scope>provided</scope>
182         </dependency>
183         <dependency>
184             <groupId>org.slf4j</groupId>
185             <artifactId>slf4j-log4j12</artifactId>
186             <version>1.6.6</version>
187             <scope>provided</scope>
188         </dependency>
189         <dependency>
190             <groupId>log4j</groupId>
191             <artifactId>log4j</artifactId>
192             <version>1.2.17</version>
193         </dependency>
194 
195         <dependency>
196             <groupId>org.slf4j</groupId>
197             <artifactId>slf4j-api</artifactId>
198             <version>1.6.6</version>
199         </dependency>
200         <dependency>
201             <groupId>javax.activation</groupId>
202             <artifactId>activation</artifactId>
203             <version>1.1.1</version>
204         </dependency>
205 
206         <!-- memcached -->
207         <dependency>
208             <groupId>com.google.code.maven-play-plugin.spy</groupId>
209             <artifactId>memcached</artifactId>
210             <version>2.4.2</version>
211         </dependency>
212         <dependency>
213             <groupId>cglib</groupId>
214             <artifactId>cglib</artifactId>
215             <version>${cglib-version}</version>
216         </dependency>
217         <dependency>
218             <groupId>net.fckeditor</groupId>
219             <artifactId>java-core</artifactId>
220             <version>2.6</version>
221         </dependency>
222         <dependency>
223             <groupId>org.json</groupId>
224             <artifactId>json</artifactId>
225             <version>20131018</version>
226         </dependency>
227       
228         <dependency>
229             <groupId>com.sun.jersey</groupId>
230             <artifactId>jersey-client</artifactId>
231             <version>1.18.1</version>
232         </dependency>
233         <dependency>
234             <groupId>jsptags</groupId>
235             <artifactId>pager-taglib</artifactId>
236             <version>2.0</version>
237         </dependency>
238 
239         <dependency>
240             <groupId>net.sourceforge.jexcelapi</groupId>
241             <artifactId>jxl</artifactId>
242             <version>2.6.10</version>
243         </dependency>
244 
245         <dependency>
246             <groupId>opensymphony</groupId>
247             <artifactId>oscache</artifactId>
248             <version>2.4.1</version>
249         </dependency>
250         <dependency>
251             <groupId>javax.servlet</groupId>
252             <artifactId>servlet-api</artifactId>
253             <version>2.4</version>
254             <scope>provided</scope>
255         </dependency>
256         
257         <dependency>
258             <groupId>org.apache.struts</groupId>
259             <artifactId>struts2-core</artifactId>
260             <version>2.3.8</version>
261             <exclusions>
262                 <exclusion>
263                     <artifactId>javassist</artifactId>
264                         <groupId>javassist</groupId>
265                 </exclusion>
266             </exclusions>
267         </dependency>
268         
269         <!-- spring和struts之间的联系 -->
270         <dependency>  
271             <groupId>org.apache.struts</groupId>  
272             <artifactId>struts2-spring-plugin</artifactId>  
273             <version>2.3.8</version>  
274         </dependency>
275         
276         <!-- struts2的注解 -->
277         <dependency>
278             <groupId>org.apache.struts</groupId>
279             <artifactId>struts2-convention-plugin</artifactId>
280         <version>2.3.8</version>
281         </dependency>
282 
283     
284     <dependency>
285         <groupId>org.hibernate</groupId>
286         <artifactId>hibernate-core</artifactId>
287         <version>4.1.7.Final</version>
288     </dependency>
289     <dependency>
290         <groupId>org.hibernate</groupId>
291         <artifactId>hibernate-ehcache</artifactId>
292         <version>4.1.7.Final</version>
293     </dependency>
294     <dependency>
295         <groupId>displaytag</groupId>
296         <artifactId>displaytag</artifactId>
297         <version>1.2</version>
298     </dependency>
299     
300   </dependencies>
301   
302   
303   
304   
305   <build>
306     <finalName>ssh_xml</finalName>
307   </build>
308   
309   
310 </project>

3:web.xml里面的dtd默认是2.3的,不过2.3的不支持el表达式,所以这里修改为2.5的。配置如下所示:

1 <?xml version="1.0" encoding="UTF-8"?>
2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
3   <display-name>Archetype Created Web Application</display-name>
4   
5 
6   <display-name>Archetype Created Web Application</display-name>
7   
8 </web-app>

4:项目配置好上面的,接下来开始写代码,首先从web.xml的配置开始,比如struts2的核心过滤器,spring核心配置文件,监听器;

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
 3   <display-name>Archetype Created Web Application</display-name>
 4   
 5 
 6   <display-name>Archetype Created Web Application</display-name>
 7   
 8   <!-- 配置spring的监听器 ,加载本地的springp配置文件-->
 9   <context-param>
10       <param-name>contextConfigLocation</param-name>
11       <param-value>classpath:bean-*.xml</param-value>
12   </context-param>
13   
14   <!-- 
15       spring的监听器 spring-web-3.2.4.RELEASE.jar ==》org.springframework
16           ==》context ==》ContextLoaderListener
17   -->
18   <listener>
19       <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
20   </listener>
21   
22   
23   <!-- 
24       struts2的核心过滤器 
25           1:filter-class  ==》 struts2-core-2.3.8.jar    ==》org.apache.struts2    
26               ==》 dispatcher ==》 ng ==》filter ==》StrutsPrepareAndExecuteFilter
27   -->
28   <filter>
29       <filter-name>struts2Filter</filter-name>
30       <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
31   </filter>
32   <filter-mapping>
33       <filter-name>struts2Filter</filter-name>
34       <url-pattern>/*</url-pattern>
35   </filter-mapping>
36   
37 </web-app>

5:配置好之后就需要配置一下maven标准格式:在src/main下面创建一个java的文件夹,在src/下面创建一个test,在test下面创建java文件夹,配置如下所示:

6:简单测试一下,将项目发布到tomcat中,启动tomcat,运行之后tomcat不报错,说明项目正常;

7:将spring的配置文件bean-common.xml配置文件放到src/main/resources下面即可(src/main/resources也是根目录):配置如下所示:

 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     xmlns:p="http://www.springframework.org/schema/p"
 5     xmlns:context="http://www.springframework.org/schema/context"
 6     xmlns:aop="http://www.springframework.org/schema/aop"
 7     xmlns:tx="http://www.springframework.org/schema/tx"
 8     xsi:schemaLocation="http://www.springframework.org/schema/beans
 9          http://www.springframework.org/schema/beans/spring-beans.xsd
10           http://www.springframework.org/schema/context
11          http://www.springframework.org/schema/context/spring-context.xsd
12          http://www.springframework.org/schema/aop
13          http://www.springframework.org/schema/aop/spring-aop.xsd
14          http://www.springframework.org/schema/tx
15           http://www.springframework.org/schema/tx/spring-tx.xsd">
16 
17           
18 </beans>          
 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     xmlns:p="http://www.springframework.org/schema/p"
 5     xmlns:context="http://www.springframework.org/schema/context"
 6     xmlns:aop="http://www.springframework.org/schema/aop"
 7     xmlns:tx="http://www.springframework.org/schema/tx"
 8     xsi:schemaLocation="http://www.springframework.org/schema/beans
 9          http://www.springframework.org/schema/beans/spring-beans.xsd
10           http://www.springframework.org/schema/context
11          http://www.springframework.org/schema/context/spring-context.xsd
12          http://www.springframework.org/schema/aop
13          http://www.springframework.org/schema/aop/spring-aop.xsd
14          http://www.springframework.org/schema/tx
15           http://www.springframework.org/schema/tx/spring-tx.xsd">
16 
17 
18     <!-- 1. 连接池实例 -->
19     <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
20         <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/ssh"></property>
21         <property name="driverClass" value="com.mysql.jdbc.Driver"></property>
22         <property name="user" value="root"></property>
23         <property name="password" value="123456"></property>
24         <property name="initialPoolSize" value="3"></property>
25         <property name="maxPoolSize" value="6"></property>
26     </bean>
27     
28     <!-- 2. Spring管理SessionFactory 【全部配置都写到spring中】 -->
29     <!-- # 注入DataSource、 注入常用配置属性、映射配置属性 -->
30     <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
31         <property name="dataSource" ref="dataSource"></property>
32         <property name="hibernateProperties">
33             <props>
34                  <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
35                  <prop key="hibernate.show_sql">true</prop>
36                  <!-- <prop key="hibernate.hbm2ddl.auto">update</prop> -->
37             </props>
38         </property>
39         <property name="mappingLocations">
40             <list>
41                 <value>classpath:com/bie/po/*.hbm.xml</value>
42             </list>
43         </property>
44     </bean>
45     
46     <!-- 3. 事务相关配置 -->
47     <!-- 3.1 事务管理器类 -->
48     <bean id="txManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
49         <property name="sessionFactory" ref="sessionFactory"></property>
50     </bean>
51 
52     <!-- 3.2 事务增强(如何管理事务)-->
53     <tx:advice id="txAdvice" transaction-manager="txManager">
54         <tx:attributes>
55             <tx:method name="get*" read-only="true"/>
56             <tx:method name="find*" read-only="true"/>
57             <tx:method name="select*" read-only="true"/>
58             <tx:method name="*" read-only="false"/>
59         </tx:attributes>
60     </tx:advice>
61 
62     <!-- 3.3 Aop配置 = 切入点表达式(拦截目标对象,生成代理)  + 事务增强应用-->
63     <aop:config>
64         <aop:pointcut expression="execution(* com.bie.service.impl.*.*(..))" id="pointcut"/>
65         <aop:advisor advice-ref="txAdvice" pointcut-ref="pointcut"/>
66     </aop:config>
67      
68      
69     <!-- ioc容器注入 dao层的注入-->      
70     <bean id="adminDao" class="com.bie.dao.impl.AdminDaoImpl">
71         <property name="sessionFactory" ref="sessionFactory"></property>
72     </bean>
73     
74     <!-- Service 需要注入 Dao -->
75     <bean id="adminService" class="com.bie.service.impl.AdminServiceImpl">
76         <property name="adminDao" ref="adminDao"></property>
77     </bean>
78           
79     <!-- Service 需要注入 Dao -->
80     <bean id="adminAction" class="com.bie.action.AdminAction">
81         <property name="adminService" ref="adminService"></property>
82     </bean> 
83           
84 </beans>          

8:将struts的配置文件struts.xml配置文件放到src/main/resources下面即可(src/main/resources也是根目录):配置如下所示:

1 <?xml version="1.0" encoding="UTF-8" ?>
2 <!DOCTYPE struts PUBLIC
3     "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
4     "http://struts.apache.org/dtds/struts-2.3.dtd">
5 
6 <struts>
7     
8 
9 </struts>    
 1 <?xml version="1.0" encoding="UTF-8" ?>
 2 <!DOCTYPE struts PUBLIC
 3     "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
 4     "http://struts.apache.org/dtds/struts-2.3.dtd">
 5 
 6 <struts>
 7     
 8     <package name="admin_package" extends="struts-default">
 9     
10         <!-- Ation实例交给spring容器创建 -->
11         <action name="select_*" class="adminAction" method="{1}">
12             
13             <!-- 列表展示 -->
14             <result name="success">index.jsp</result>
15             
16         </action>
17     </package>    
18 
19 </struts>    

9:配置到第八步,基本结束,开始写代码,创建一个实体类Admin;如下所示:

 1 package com.bie.po;
 2 
 3 /** 
 4 * @author 作者:别先生
 5 * @version 创建时间:2017年6月10日 下午2:08:17 
 6 * 类说明 
 7 */
 8 
 9 public class Admin {
10 
11     private Integer adminId;//管理员编号
12     private String adminName;//管理员姓名
13     private String adminPwd;//管理员密码
14     
15     public Integer getAdminId() {
16         return adminId;
17     }
18     public void setAdminId(Integer adminId) {
19         this.adminId = adminId;
20     }
21     public String getAdminName() {
22         return adminName;
23     }
24     public void setAdminName(String adminName) {
25         this.adminName = adminName;
26     }
27     public String getAdminPwd() {
28         return adminPwd;
29     }
30     public void setAdminPwd(String adminPwd) {
31         this.adminPwd = adminPwd;
32     }
33     @Override
34     public String toString() {
35         return "Admin [adminId=" + adminId + ", adminName=" + adminName + ", adminPwd=" + adminPwd + "]";
36     }
37 
38     
39 }

10:创建好实体类之后配置一个hibernate的映射文件;Admin.hbm.xml,代码如下所示:

 1 <?xml version="1.0" encoding="UTF-8" ?>
 2 <!DOCTYPE hibernate-mapping PUBLIC 
 3     "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
 4     "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
 5 
 6 <!-- hibernate的实体类映射文件的配置,package包指出总包所在 -->
 7 <hibernate-mapping package="com.bie.po">
 8 
 9     <!-- class的name是实体类的名字,table是数据表名 -->
10     <class name="Admin" table="admin">
11         <id name="adminId">
12             <!-- native设置自增 -->
13             <generator class="native"></generator>
14         </id>
15         
16         <!-- 非主键的映射 -->
17         <property name="adminName" length="20"></property>
18         <property name="adminPwd" length="20"></property>
19     </class>
20 
21 </hibernate-mapping>    
22     

11:创建好实体类之后开始创建dao层的接口和实现类,如AdminDao接口和AdminDaoImpl实现类;

 1 package com.bie.dao;
 2 
 3 import java.util.List;
 4 
 5 import com.bie.po.Admin;
 6 
 7 /** 
 8 * @author 作者:别先生
 9 * @version 创建时间:2017年6月10日 下午2:25:01 
10 * 类说明 
11 */
12 
13 public interface AdminDao {
14 
15     /**
16      * 查询管理员的信息
17      * @param admin
18      * @return
19      */
20     public List<Admin> selectAdmin();
21 }
 1 package com.bie.dao.impl;
 2 
 3 import java.util.List;
 4 
 5 import org.hibernate.SessionFactory;
 6 
 7 import com.bie.dao.AdminDao;
 8 import com.bie.po.Admin;
 9 
10 /** 
11 * @author 作者:别先生
12 * @version 创建时间:2017年6月10日 下午2:25:15 
13 * 类说明 
14 */
15 
16 public class AdminDaoImpl implements AdminDao{
17 
18     //IOC容器注入sessionFactory对象
19     private SessionFactory sessionFactory;
20     public void setSessionFactory(SessionFactory sessionFactory) {
21         this.sessionFactory = sessionFactory;
22     }
23     
24     public List<Admin> selectAdmin() {
25         
26         List<Admin> list =  sessionFactory.getCurrentSession().createQuery(" from Admin ").list();
27         
28         return list;
29     }
30 
31     
32 }

12:创建好dao层接口和实现类之后开始创建service层的接口和实现类,如AdminService接口和AdminServiceImpl实现类;

 1 package com.bie.service;
 2 
 3 import java.util.List;
 4 
 5 import com.bie.po.Admin;
 6 
 7 /** 
 8 * @author 作者:别先生
 9 * @version 创建时间:2017年6月10日 下午2:29:20 
10 * 类说明 
11 */
12 
13 public interface AdminService {
14 
15     public List<Admin> selectAdmin();
16 }
 1 package com.bie.service.impl;
 2 
 3 import java.util.List;
 4 
 5 import com.bie.dao.AdminDao;
 6 import com.bie.po.Admin;
 7 import com.bie.service.AdminService;
 8 
 9 /** 
10 * @author 作者:别先生
11 * @version 创建时间:2017年6月10日 下午2:29:40 
12 * 类说明 
13 */
14 
15 public class AdminServiceImpl implements AdminService{
16 
17     //ioc容器注入
18     private AdminDao adminDao;
19     public void setAdminDao(AdminDao adminDao) {
20         this.adminDao = adminDao;
21     }
22     
23     public List<Admin> selectAdmin(){
24         
25         List<Admin> list = adminDao.selectAdmin();
26         
27         return list;
28     }
29 }

13:当写好service层之后开始写action层AdminAction,这里传递前台页面的参数;代码如下所示:

 1 package com.bie.action;
 2 
 3 import java.util.List;
 4 import java.util.Map;
 5 
 6 import org.apache.struts2.interceptor.RequestAware;
 7 
 8 import com.bie.po.Admin;
 9 import com.bie.service.AdminService;
10 import com.opensymphony.xwork2.ActionSupport;
11 
12 /** 
13 * @author 作者:别先生
14 * @version 创建时间:2017年6月10日 下午2:36:07 
15 * 类说明 
16 */
17 
18 public class AdminAction extends ActionSupport implements RequestAware{
19 
20     
21     private static final long serialVersionUID = 1L;
22 
23     //ioc容器注入
24     private AdminService adminService;
25     public void setAdminService(AdminService adminService) {
26         this.adminService = adminService;
27     }
28     
29     // 接收框架运行时候传入的代表request对象的map
30     private Map<String, Object> request;
31     public void setRequest(Map<String, Object> request) {
32         this.request = request;
33     }
34     
35     
36     //ioc容器注入
37     private Admin admin;
38     public void setAdmin(Admin admin) {
39         this.admin = admin;
40     }
41     public Admin getAdmin() {
42         return admin;
43     }
44     
45     public String selectAdmin(){
46         
47         List<Admin> list = adminService.selectAdmin();
48         
49         //将信息保存到request中
50         request.put("list", list);
51         
52         return "success";
53     }
54     
55 
56 }

14:当后台写完之后开始写前台测试页面:页面如下所示

 1 <%@ page language="java" contentType="text/html; charset=UTF-8"
 2     pageEncoding="UTF-8"%>
 3 <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
 4 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 5 <html>
 6 <head>
 7 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 8 <title>主页显示管理员的信息</title>
 9 </head>
10 <body>
11 
12 <a href="select_selectAdmin.action">查询所有用户信息</a>
13 
14 <center>
15     <table cellspacing="20" cellpadding="20">
16         <tr>
17             <th>管理员编号</th>
18             <th>管理员姓名</th>
19             <th>管理员密码</th>
20         </tr>
21         <c:forEach items="${list }" var="list">
22         <tr>
23             <td>${list.adminId}</td>
24             <td>${list.adminName}</td>
25             <td>${list.adminPwd   }</td>
26         </tr>
27         </c:forEach>
28     </table>
29 </center>
30 
31 </body>
32 </html>

直接访问index.jsp页面,测试结果如下所示:

ssh配置版框架搭建先到这吧,以后有机会再深学吧。时间过的挺快的,马上就该出去找工作了...... 增删改查功能日后有时间再写吧......

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

相关文章

来自专栏GreenLeaves

九、将cs文件快速的转换成可执行文件和响应文件(配置编译开关的文件)

1、将包含多个类型的源代码文件转换为可以部署的文件。有如下Program.cs的文件,代码如下: public sealed class Program...

2747
来自专栏noteless

[二] JavaIO之File详解 以及FileSystem WinNTFileSystem简介

1.可选的与系统有关的前缀  字符串   比如盘符,"/" 表示 UNIX 中的根目录,"\\\\" 表示 Microsoft Windows UNC 路径名

1283
来自专栏蓝天

C/C++常见gcc编译链接错误解决方法

用“-Wl,-Bstatic”指定链接静态库,使用“-Wl,-Bdynamic”指定链接共享库,使用示例: -Wl,-Bstatic -lmysq...

1413
来自专栏linux系统运维

原 shell脚本介绍, shell脚本

1953
来自专栏GuZhenYin

ASP.NET Core文件上传与下载(多种上传方式)

前言 前段时间项目上线,实在太忙,最近终于开始可以研究研究ASP.NET Core了. 打算写个系列,但是还没想好目录,今天先来一篇,后面在整理吧. ASP.N...

4296
来自专栏Python爱好者

Java基础笔记11

974
来自专栏领域驱动设计DDD实战进阶

02-TypeScript中新的字符串

TypeScript中引入了字符串模板,通过字符串模板可以方便的实现字符串换行的连接、方便变量的使用等。 1.在WebStorm中新建一个文件,后缀名为ts。 ...

3274
来自专栏GuZhenYin

ASP.NET Core文件上传与下载(多种上传方式)

官方机器翻译的地址:https://docs.microsoft.com/en-us/aspnet/core/mvc/models/file-uploads

1090
来自专栏运维

Saltstack源码安装zabbix_agent客户端

这里目前只是告诉客户端安装vim-enhanced、lrzsz这2个软件,可以根据实际情况自行安装依赖软件,pkg安装模块目前支持apt与yum。

421
来自专栏mySoul

Python 小入坑

分类 解释型语言,面向对象,动态数据类型的高级语言,其开源遵守GPL协议。支持多种编程范式, 面向对象,函数,过程,命令式。拥有动态类型系统和垃圾回收功能。创始...

632

扫码关注云+社区