spring2.5.6+struts2.1.6+jpa+annotation傻瓜教程
中国IT不缺牛人,缺的是我这样无私奉献的普通人
先装个MyEclipse吧 MyEclipse-7.0-win32.exe
涉及版权问题,key自己baidu吧
new一个web project jdk1.5以上(struts2.1之后好象只支持JDK1.5以上版本,懒得baidu了,应该没记错)
web.xml配置
<?xml version="1.0" encoding="UTF-8"?><web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <!-- spring 上下文配置路径 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:/spring/applicationContext-*.xml</param-value> </context-param> <!-- spring 上下文监听器 --> <listener> <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class> </listener> <!-- 注意哦,这里不再是hibernate的OpenSessionInViewFilter了 --> <filter> <filter-name>openEntityManagerInViewFilter</filter-name> <filter-class> org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter </filter-class> </filter> <filter-mapping> <filter-name>openEntityManagerInViewFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- struts 控制器 --> <filter> <filter-name>struts2</filter-name> <filter-class> org.apache.struts2.dispatcher.FilterDispatcher </filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- 强制编码UTF-8 --> <filter> <filter-name>characterEncodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> <init-param> <param-name>forceEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>characterEncodingFilter</filter-name> <url-pattern>*.jsp</url-pattern> </filter-mapping></web-app>
?
spring配置文件
<?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:p="http://www.springframework.org/schema/p"xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsdhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsdhttp://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"default-autowire="byName"><!-- 这里让spring通过annotation自动装配bean --><context:component-scan base-package="*"/> <context:annotation-config /><!-- 数据源 --><bean id="dataSource" value="oracle.jdbc.driver.OracleDriver" /> <property name="url" value="jdbc:oracle:thin:@localhost:1521:xe" /> <property name="username" value="***" /> <property name="password" value="***" /> </bean> <!-- 值对象annotation --><bean /> <!-- plug in vendor-specific behavior into Spring's EntityManagerFactory creators 翻译过来就是插入特定厂商的信息比如ORACLE--><bean id="jpaVendorAdapter" value="ORACLE" /> <property name="showSql" value="true" /> </bean> <!-- 实体管理工厂类 --> <bean id="entityManagerFactory" ref="dataSource" /> <property name="jpaVendorAdapter" ref="jpaVendorAdapter" /> </bean> <!-- 事务管理类 --> <bean id="transactionManager" ref="entityManagerFactory" /> </bean> <!-- 打开事务annotation --> <tx:annotation-driven transaction-manager="transactionManager" /></beans>
?在src建个META-INF文件夹,新建persistence.xml文件
<persistence xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd" version="1.0"> <persistence-unit name="punit"> </persistence-unit></persistence>
?
接下来把jar包扔到WEB-INF/lib下,之所以要先写配置文件后扔jar包,是为了根据启动错误信息一步一步加jar包
链接给大家友情提供了 struts2.1.6??? spring2.5.6
先把struts几个包拷出来
xwork-2.1.2.jar
struts2-core-2.1.6.jar
freemarker-2.3.13.jar
ognl-2.6.11.jar
commons-fileupload-1.2.1.jar
commons-logging-api-1.1.jar
struts2-convention-plugin-2.1.6.jar
struts2-spring-plugin-2.1.6.jar
接下来是spring
spring.jar
然后是hibernate,不用另外下载hibernate,spring的lib文件夹里就有了,慢慢找,顺便熟悉下结构
hibernate3.jar
hibernate-annotations.jar
hibernate-entitymanager.jar
hibernate-commons-annotations.jar
slf4j-api-1.5.0.jar
slf4j-log4j12-1.5.0.jar
log4j-1.2.15.jar
asm-2.2.3.jar
cglib-nodep-2.1_3.jar
antlr-2.7.6.jar
dom4j-1.6.1.jar
javassist-3.4.GA.jar
persistence.jar
jta.jar
c3p0-0.9.1.2.jar
commons-collections.jar
最后是oracle驱动包,oracle安装目录下jdbc文件夹里有
ojdbc14.jar
配置就此结束
有人要问,为啥没有struts.xml文件呢,呵呵,用annotation做了
BaseAction.java 基类,继承ActionSupport类和Preparable接口
package com.demo.action;import com.opensymphony.xwork2.ActionSupport;import com.opensymphony.xwork2.Preparable;public class BaseAction extends ActionSupport implements Preparable {/** * */private static final long serialVersionUID = 1L;public void prepare() throws Exception {// TODO Auto-generated method stub}}?TestAction.java 继承BaseAction
?
package com.demo.action;import org.apache.struts2.convention.annotation.Action;import org.apache.struts2.convention.annotation.Result;public class TestAction extends BaseAction {/** * */private static final long serialVersionUID = 1L;@Action(value = "foo", results = { @Result(name = "success", location = "/pages/welcome.jsp") })public String test() {return SUCCESS;}}?
在WebRoot下建个pages文件夹,里面放个welcome.jsp文件,上面的就能看明白了
?
欢迎大家批评!
?
?
?
?
?
struts2.1之后,就开始不使用 org.apache.struts2.dispatcher.FilterDispatcher了。官方推荐使用org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
jdk1.6下最好使用ojdbc6.jar 而不是ojdbc14.jar