数据库建表脚本不带,是很简单的一个表,把数据库环境搭建起来即可。
1、项目结构(Eclipse)
2、pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>jiabin</groupId> <artifactId>ssh-demo</artifactId> <packaging>war</packaging> <version>0.0.1-SNAPSHOT</version> <name>ssh-demo Maven Webapp</name> <url>http://maven.apache.org</url> <!-- bind params --> <properties> <org.apache.struts.version>2.3.1</org.apache.struts.version> <org.springframework.version>3.0.5.RELEASE</org.springframework.version> <spring.security.version>3.0.5.RELEASE</spring.security.version> </properties> <dependencies> <!-- struts core start --> <dependency> <groupId>org.apache.struts</groupId> <artifactId>struts2-core</artifactId> <version>${org.apache.struts.version}</version> </dependency> <dependency> <groupId>org.apache.struts</groupId> <artifactId>struts2-json-plugin</artifactId> <version>${org.apache.struts.version}</version> </dependency> <!-- struts core end --> <!-- spring list start --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${org.springframework.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${org.springframework.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>${org.springframework.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>${org.springframework.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${org.springframework.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>${org.springframework.version}</version> </dependency> <!-- 下面的三个包是在配置事务的时候用到的 spring 的依赖包 --> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.7.0</version> </dependency> <dependency> <groupId>aopalliance</groupId> <artifactId>aopalliance</artifactId> <version>1.0</version> </dependency> <dependency> <groupId>cglib</groupId> <artifactId>cglib-nodep</artifactId> <version>2.2.2</version> </dependency> <!-- spring list end --> <!-- struts + spring plugin --> <dependency> <groupId>org.apache.struts</groupId> <artifactId>struts2-spring-plugin</artifactId> <version>${org.apache.struts.version}</version> </dependency> <!-- hibernate core --> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate</artifactId> <version>3.2.1.ga</version> </dependency> <!-- spring + hibernate plugin --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-orm</artifactId> <version>${org.springframework.version}</version> </dependency> <!-- database mysql --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.18</version> </dependency> <!-- test jar --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <!-- database connection pool jar --> <dependency> <groupId>commons-dbcp</groupId> <artifactId>commons-dbcp</artifactId> <version>1.4</version> </dependency> <!-- md5密码提要jar包 --> <dependency> <groupId>commons-codec</groupId> <artifactId>commons-codec</artifactId> <version>1.5</version> </dependency> <!-- 日志打印 log4j 包 --> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.14</version> <scope>runtime</scope> </dependency> <!-- 日志记录依赖包,很多都依赖此包,像 log4j,json-lib 等等 --> <dependency> <groupId>commons-logging</groupId> <artifactId>commons-logging-api</artifactId> <version>1.1</version> </dependency> <!-- dom4j 解析 XML 文件的包 --> <dependency> <groupId>dom4j</groupId> <artifactId>dom4j</artifactId> <version>1.6.1</version> </dependency> <!-- JSTL 标签库 --> <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>2.5</version> </dependency> <dependency> <groupId>javax.servlet.jsp</groupId> <artifactId>jsp-api</artifactId> <version>2.2</version> </dependency> </dependencies> <build> <finalName>ssh-demo</finalName> </build> </project>
jta需要手动注册到本地库中,注册方式参考
http://www.blogjava.net/nkjava/archive/2009/02/12/254438.html
mvn命令是安装Maven之后才能使用(把maven的bin目录路径加入到环境变量中,和java一样),或者在bin目录下打开cmd(微软系统键+r,然后输入cmd,或者shift右键菜单有个命令行运行)
3、web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.3" 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_2_3.xsd"> <!-- Configuration of Log4j log4j的配置 start--> <context-param> <param-name>log4jConfigLocation</param-name> <param-value> <!-- log4j.properties文件路径 --> classpath:/ssh/config/log4j.properties </param-value> </context-param> <context-param> <param-name>log4jRefreshInterval</param-name> <param-value>60000</param-value> </context-param> <listener> <listener-class> org.springframework.web.util.Log4jConfigListener </listener-class> </listener> <!-- Configuration of Log4j log4j的配置 end--> <!-- spring app配置 start --> <filter> <filter-name>openSessionInViewFilter</filter-name> <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class> </filter> <filter-mapping> <filter-name>openSessionInViewFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:/ssh/spring/app-*.xml</param-value> </context-param> <listener> <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class> </listener> <!-- spring app配置 end --> <!-- struts前端转发器配置 start --> <filter> <filter-name>Struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> <!-- 自定义struts.xml配置文件位置,struts-default.xml,struts-plugin.xml为必填项,不能去除 --> <init-param> <param-name>config</param-name> <param-value>struts-default.xml,struts-plugin.xml,ssh/struts/struts.xml</param-value> </init-param> </filter> <filter-mapping> <filter-name>Struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- struts前端转发器配置 end --> </web-app>
4、现在添加web.xml中用到的配置文件
driver=com.mysql.jdbc.Driver url=jdbc:mysql://localhost:3306/test #driver=oracle.jdbc.OracleDriver #url=jdbc:oracle:thin:@localhost:1521:dab user=dab password=
#\u914D\u7F6E\u4E86\u63A7\u5236\u53F0\u548C\u6587\u672C\u8BB0\u5F55\u4E24\u79CD\u65B9\u5F0F log4j.rootLogger=DEBUG,CONSOLE,FILEOUT log4j.addivity.org.apache=true # CONSOLE log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender log4j.appender.Threshold=DEBUG log4j.appender.CONSOLE.Target=System.out log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout #log4j.appender.CONSOLE.layout.ConversionPattern=[framework] %d - %c -%-4r [%t] %-5p %c %x - %m%n log4j.appender.CONSOLE.layout.ConversionPattern=[%-5p] %d{yyyy-MM-dd HH\:mm\:ss} \:%m%n # # FILEOUT log4j.appender.FILEOUT=org.apache.log4j.RollingFileAppender log4j.appender.FILEOUT.File=${catalina.home}\\file.log log4j.appender.fileout.MaxFileSize=100000KB # default is true\uFF0Cappend to the file; if false, the replace the log file whenever restart system log4j.appender.FILEOUT.Append=true #RollingFileAppender\u6CA1\u6709DatePattern\u8FD9\u4E2A\u5C5E\u6027 log4j.appender.FILEOUT.layout=org.apache.log4j.PatternLayout #log4j.appender.CONSOLE.layout.ConversionPattern=[framework] %d - %c -%-4r [%t] %-5p %c %x - %m%n log4j.appender.FILEOUT.layout.ConversionPattern=[%-5p]_%d{yyyy-MM-dd HH:mm:ss} :%m%n
<?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:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:jee="http://www.springframework.org/schema/jee" xsi:schemaLocation=" http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd"> <!-- 自动扫描Bean --> <context:component-scan base-package="ssh" /> <!-- 组件自动扫描 --> <context:component-scan base-package="ssh.*"></context:component-scan> <!-- 加载配置参数 --> <context:property-placeholder location="classpath:/ssh/config/*.properties"/> <!-- 数据库连接池初始化 --> <bean id="dataSource" destroy-method="close" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="${driver}" /> <property name="url" value="${url}" /> <property name="username" value="${user}" /> <property name="password" value="${password}" /> <!-- data source configuration --> <property name="initialSize" value="60" /> <property name="maxActive" value="100" /> <property name="maxIdle" value="50" /> <property name="minIdle" value="10" /> </bean> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <!-- 注入dataSource连接资源 --> <property name="dataSource" ref="dataSource"> </property> <!-- 注入hibernate配置参数 --> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">org.hibernate.dialect.OracleDialect</prop> <prop key="hibernate.show_sql">true</prop> <prop key="hibernate.format_sql">true</prop> </props> </property> <!-- 注入hibernate映射描述文件 --> <property name="mappingResources"> <list> <value>ssh/hibernate/TbPeople.hbm.xml</value> </list> </property> </bean> <!-- 事务控制 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource" /> </bean> <tx:advice id="userTxAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="*" propagation="REQUIRED" read-only="false" rollback-for="java.lang.Exception" /> </tx:attributes> </tx:advice> <aop:config> <!-- 把事务控制在 Business 层 --> <aop:pointcut id="pc" expression="execution(* ssh.service.*.*(..))" /> <aop:advisor pointcut-ref="pc" advice-ref="userTxAdvice" /> </aop:config> </beans>
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd"> <struts> <package name="web-default" extends="struts-default"> <default-action-ref name="index"></default-action-ref> <action name="index"> <result>/WEB-INF/view/index.jsp</result> </action> </package> <package name="people" namespace="/people" extends="json-default"> <action name="test" class="peopleAction" method="test"> <result name="ok" type="json"> <param name="root"> pojos </param> </result> </action> </package> </struts>
5、再执行一次右键菜单 run as -> maven install -> run as server
index.jsp测试用的
<!DOCTYPE html> <html lang="zh-cn"> <head> <title></title> <script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.8.0.js"></script> </head> <body> <h2>Hello World!</h2> <script type="text/javascript"> $(function(){ $.get('http://localhost:8080/ssh-demo/people/test', function(data) { console.log(data); }); }); </script> </body> </html>
结果: