最近有很多人不知道如何搭建基于SSI(struts2,spring,ibatis)的J2EE开发环境,这里给大家一个demo,供初学者使用,该框架是基于MVC的,并且已经做好了文件的分层等,并加入了日志文件,好了,废话不多说了
1.搭建struts2开发环境:
(1)找到开发strust2应用所需要使用到的jar文件
(2).编写struts.xml文件
struts.xml(struts的总配置文件)
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" 3 "http://struts.apache.org/dtds/struts-2.0.dtd"> 4 5 <struts> 6 <constant name="struts.objectFactory" value="org.apache.struts2.spring.StrutsSpringObjectFactory"/> 7 <include file="resources/webapp/struts/struts-product.xml"></include> 8 </struts>
struts-product.xml
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" 3 "http://struts.apache.org/dtds/struts-2.0.dtd"> 4 5 <struts> 6 <package name="product" extends="struts-default"> 7 <action name="findProductList" class="productAction" method="findProductList"> 8 <result name="success">/welcome.jsp</result> 9 </action> 10 </package> 11 </struts>
(3).在web.xml中加入strust2框架启动配置
<!-- Struts核心过滤器 -->
1 <filter> 2 <filter-name>struts2</filter-name> 3 <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class> 4 <init-param> 5 <param-name>struts.action.extension</param-name> 6 <param-value>htm</param-value> 7 </init-param> 8 </filter> 9 <filter-mapping> 10 <filter-name>struts2</filter-name> 11 <url-pattern>/*</url-pattern> 12 </filter-mapping>
2.搭建spring开发环境:
(1)找到开发spring应用所需要使用到的jar文件
(2).编写spring.xml文件
spring-applicationContext.xml(spring的总配置文件)
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" 3 "http://www.springframework.org/dtd/spring-beans.dtd"> 4 <beans> 5 <import resource="resources/spring/spring-dataResources.xml"/> 6 <import resource="resources/webapp/spring-action-product.xml"></import> 7 <import resource="resources/spring/spring-service-product.xml"></import> 8 <import resource="resources/spring/spring-dao-product.xml"></import> 9 </beans>
spring-dataResources.xml(配置数据源的配置文件)
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" 3 "http://www.springframework.org/dtd/spring-beans.dtd"> 4 5 <beans> 6 <bean id="myDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> 7 <property name="driverClassName" value="com.mysql.jdbc.Driver" /> 8 <property name="url" value="jdbc:mysql://127.0.0.1:3306/shop" /> 9 <property name="username" value="root" /> 10 <property name="password" value="123456" /> 11 </bean> 12 <bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean"> 13 <property name="configLocation" value="classpath:sql-map-config.xml" /> 14 <property name="dataSource" ref="myDataSource" /> 15 </bean> 16 </beans>
spring-action-product.xml(spring 对action层的配置)
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"> 3 <beans> 4 <bean id="productAction" class="com.yangyang.action.product.ProductAction" singleton="false" autowire="byName" /> 5 <!-- <bean id="productAction" class="com.yangyang.action.product.ProductAction" scope="prototype" autowire="byName" /> --> 6 </beans>
spring-service-product.xml(spring 对service层的配置)
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" 3 "http://www.springframework.org/dtd/spring-beans.dtd"> 4 <beans> 5 <bean id="productManager" class="com.yangyang.manager.product.impl.ProductManagerImpl" autowire="byName"/> 6 </beans>
spring-dao-product.xml(spring 对dao层的配置)
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" 3 "http://www.springframework.org/dtd/spring-beans.dtd"> 4 5 <beans> 6 <bean id="productDao" class="com.yangyang.dao.product.impl.ProductDaoImpl"> 7 <property name="sqlMapClient"> 8 <ref bean="sqlMapClient" /> 9 </property> 10 </bean> 11 </beans>
(3).在web.xml中加入spring框架启动配置
<!-- spring 基本配置 -->
1 <context-param> 2 <param-name>contextConfigLocation</param-name> 3 <param-value>classpath:spring-applicationContext.xml</param-value> 4 </context-param> 5 <listener> 6 <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 7 </listener>
3.搭建ibatis的环境:
(1)找到开发ibatis应用所需要使用到的jar文件
(2)编写sql-map-config文件
sql-map-config.xml(ibatis的配置文件)
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE sqlMapConfig PUBLIC "-//ibatis.apache.org//DTD SQL Map Config 2.0//EN" 3 "http://ibatis.apache.org/dtd/sql-map-config-2.dtd"> 4 5 <sqlMapConfig> 6 <settings enhancementEnabled="true" maxTransactions="40" maxRequests="32" maxSessions="10"/> 7 <!--SQL 映射文件列表 --> 8 <sqlMap resource="resources/ibatis/sqlmap-product.xml"/> 9 </sqlMapConfig>
sqlmap-product.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE sqlMap PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN" 3 "http://ibatis.apache.org/dtd/sql-map-2.dtd"> 4 <sqlMap> 5 <select id="findProductList" resultClass="Long"> 6 SELECT MONTH('2012-12-08') 7 </select> 8 </sqlMap>
4.配置log4j.xml
(1)找到开发ibatis应用所需要使用到的jar文件
(2).两种方式加载log4j的打印信息
方法一:在src目录下加入log4j. properties
log4j.properties(打印配置的信息)
1 log4j.rootLogger=DEBUG, stdout, fileout 2 #log4j.logger.test=info 3 #log4j.logger.org.apache.jasper = DEBUG 4 #log4j.logger.org.apache.catalina.startup.TldConfig = DEBUG 5 #log4j.logger.org.apache.catalina.session.ManagerBase = DEBUG 6 7 log4j.logger.com.fiscal = DEBUG 8 log4j.logger.com.system = DEBUG 9 10 log4j.logger.com.ibatis = DEBUG 11 log4j.logger.com.ibatis.common.jdbc.SimpleDataSource = DEBUG 12 log4j.logger.com.ibatis.common.jdbc.ScriptRunner = DEBUG 13 log4j.logger.com.ibatis.sqlmap.engine.impl.SqlMapClientDelegate = DEBUG 14 log4j.logger.java.sql.Connection = DEBUG 15 log4j.logger.java.sql.Statement = DEBUG 16 log4j.logger.java.sql.PreparedStatement = DEBUG, fileout 17 log4j.logger.java.sql.ResultSet = DEBUG 18 19 log4j.appender.stdout=org.apache.log4j.ConsoleAppender 20 21 log4j.appender.fileout=org.apache.log4j.RollingFileAppender 22 log4j.appender.fileout.File=C:\ibatis.log 23 log4j.appender.fileout.MaxFileSize=10000KB 24 25 log4j.appender.stdout.layout=org.apache.log4j.PatternLayout 26 log4j.appender.stdout.layout.ConversionPattern=[%-5p] %d{yyyy-MM-dd HH:mm:ss} :%m%n 27 log4j.appender.fileout.layout=org.apache.log4j.PatternLayout 28 log4j.appender.fileout.layout.ConversionPattern=[%-5p]_%d{yyyy-MM-dd HH:mm:ss} :%m%n 29 30 #log4j.appender.stdout.layout=org.apache.log4j.SimpleLayout 31 32 # log4j.logger.org=info
第二种方式直接在项目中加入:log4j.xml(只打印sql)
log4j.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 3 <!DOCTYPE log4j:configuration SYSTEM "log4j.dtd"> 4 5 <log4j:configuration xmlns:log4j='http://jakarta.apache.org/log4j/'> 6 7 <appender class="org.apache.log4j.ConsoleAppender" name="console"> 8 <layout class="org.apache.log4j.PatternLayout"> 9 <param name="ConversionPattern" 10 value="%d %-5p [%t] %-17c{2} (%13F:%L) %3x - %m%n" /> 11 <!-- param name="ConversionPattern" 12 value="=%d %-5p [%t] %-17c{2} (%13F:%L) %3x - %m%n" /--> 13 </layout> 14 </appender> 15 <appender class="org.apache.log4j.RollingFileAppender" 16 name="file"> 17 <param name="File" value="MengBackup.log" /> 18 <param name="MaxFileSize" value="100KB" /> 19 <param name="MaxBackupIndex" value="2" /> 20 <layout class="org.apache.log4j.PatternLayout"> 21 <param name="ConversionPattern" 22 value="=%d %-5p [%t] %-17c{2} (%13F:%L) %3x - %m%n" /> 23 </layout> 24 </appender> 25 <logger name="com.ibatis"> 26 <level value="debug" /> 27 </logger> 28 <logger name="com.ibatis.common.jdbc.SimpleDataSource"> 29 <level value="debug" /> 30 </logger> 31 <logger name="com.ibatis.common.jdbc.ScriptRunner"> 32 <level value="debug" /> 33 </logger> 34 <logger name="com.ibatis.common.jdbc.SqlMapClientDelegate"> 35 <level value="debug" /> 36 </logger> 37 <logger name="java.sql.Connection"> 38 <level value="debug" /> 39 </logger> 40 <logger name="java.sql.Statement"> 41 <level value="debug" /> 42 </logger> 43 <logger name="java.sql.PreparedStatement"> 44 <level value="debug" /> 45 </logger> 46 <logger name="java.sql.ResultSet"> 47 <level value="debug" /> 48 </logger> 49 <root> 50 <priority value="DEBUG" /> 51 <appender-ref ref="console" /> 52 <appender-ref ref="file" /> 53 </root> 54 </log4j:configuration>
目录结构:
5.最后是整个搭建好的项目:
上传附件的功能没找到