guice整合struts2与jpa,guice的使用(九)

传统我们开发一般使用ssh,但是有些微服务应用的项目我们不需要这么臃肿的框架做开发,于是采用了guice+struts2+guice作为框架组合进行了开发。

先看我们项目引用的jar包:

使用的时候一定要主要jar的版本问题.我项目在jdk1.7上面开发的

然后看一下web.xml的配置:

 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_3_0.xsd" version="3.0">
 3   <filter>
 4     <filter-name>guiceFilter</filter-name>
 5     <filter-class>com.google.inject.servlet.GuiceFilter</filter-class>
 6   </filter>
 7   <filter-mapping>
 8     <filter-name>guiceFilter</filter-name>
 9     <url-pattern>/*</url-pattern>
10   </filter-mapping>
11   <listener>
12     <listener-class>com.ming.core.web.listener.GoogleGuiceServletContextListener</listener-class>
13   </listener>
14
15   <welcome-file-list>
16     <welcome-file>index.jsp</welcome-file>
17   </welcome-file-list>
18 </web-app>

然后是struts.xml的配置:

 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     <!-- 引用guice代理 -->
 8     <constant name="struts.objectFactory" value="guice" />
 9     <constant name="struts.i18n.encoding" value="UTF-8" />
10     <constant name="struts.enable.DynamicMethodInvocation" value="true"/>
11     <include file="com/ming/user/action/userStruts.xml"></include>
12 </struts>

然后是jpa的persistence.xml的配置:

 1 <persistence xmlns="http://java.sun.com/xml/ns/persistence"
 2     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 3     xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
 4     version="2.0">
 5     <persistence-unit name="myunit" transaction-type="RESOURCE_LOCAL">
 6
 7         <provider>org.hibernate.ejb.HibernatePersistence</provider>
 8         <class>com.ming.user.entity.Student</class>
 9         <properties>
10             <property name="hibernate.connection.provider_class"
11                 value="org.hibernate.service.jdbc.connections.internal.C3P0ConnectionProvider" />
12             <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect" />
13             <property name="hibernate.c3p0.max_size" value="2" />
14             <property name="hibernate.c3p0.min_size" value="1" />
15             <property name="hibernate.c3p0.timeout" value="120" />
16             <property name="hibernate.c3p0.max_statements" value="100" />
17             <property name="hibernate.c3p0.idle_test_period" value="120" />
18             <property name="hibernate.c3p0.acquire_increment" value="1" />
19             <property name="hibernate.show_sql" value="true" />
20             <property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver" />
21             <property name="hibernate.connection.url"
22                 value="jdbc:mysql://localhost:3306/test?autoReconnect=true&amp;useUnicode=true&amp;characterEncoding=gbk" />
23             <property name="hibernate.connection.username" value="root" />
24             <property name="hibernate.connection.password" value="root" />
25             <property name="hibernate.temp.use_jdbc_metadata_defaults"
26                 value="false" />
27         </properties>
28
29         <!-- <properties> <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect"
30             /> <property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"
31             /> <property name="hibernate.connection.driver" value="com.mysql.jdbc.Driver"
32             /> <property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/test?autoReconnect=true&amp;useUnicode=true&amp;characterEncoding=UTF-8"
33             /> <property name="hibernate.connection.user" value="root" /> <property name="hibernate.connection.password"
34             value="root" /> <property name="hibernate.temp.use_jdbc_metadata_defaults"
35             value="false"/> </properties> -->
36     </persistence-unit>
37 </persistence>

然后是module的一些配置:

package com.ming.core.web.listener;

import com.google.inject.Guice;
import com.google.inject.Injector;
import com.google.inject.servlet.GuiceServletContextListener;
import com.ming.user.UserModule;

public class GoogleGuiceServletContextListener extends GuiceServletContextListener {

    @Override
    protected Injector getInjector() {

        return Guice.createInjector(new UserModule());
        //如果绑定多个module,需要像下面这样就可以了
        //return Guice.createInjector(new UserModule(),new UserModule());
    }

}
package com.ming.user;

import org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter;

import com.google.inject.AbstractModule;
import com.google.inject.Singleton;
import com.google.inject.persist.PersistFilter;
import com.google.inject.persist.jpa.JpaPersistModule;
import com.google.inject.servlet.ServletModule;
import com.google.inject.struts2.Struts2GuicePluginModule;
public class UserModule extends AbstractModule {
    @Override
    protected void configure() {
        install(new ServletModule(){
            @Override
            protected void configureServlets() {
                install(new JpaPersistModule("myunit")); //这个一定要写最前面
                install(new Struts2GuicePluginModule());//这个是struts2与guice组件结合的注入
                bind(StrutsPrepareAndExecuteFilter.class).in(Singleton.class);//这个类似struts2的那个过滤
                filter("/*").through(StrutsPrepareAndExecuteFilter.class);
                filter("/*").through(PersistFilter.class);
            }
        });

    }

}

以上是几个重要的配置文件。

下面是项目结构:

注意persistence.xml这个配置文件要放在src下的META-INF下。

框架例子下载:源码下载

时间: 2024-08-25 18:28:44

guice整合struts2与jpa,guice的使用(九)的相关文章

guice整合struts2,guice的使用(八)

平时我们习惯用了spring整合struts2,今天我们就来见识一下guice整合struts2吧. 看web.xml配置: <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee"

SSH框架手动整合——Struts2+Hibernate4+Spring4

最近遇到过一些框架方面的问题,其中有MyBatis.SSH.SpringMVC,其中SSH的一些配置有些忘的差不多了,也有一些同事问了这些问题,前几个月也整合过SSH框架,那个时候是直接拿别人的Jar包直接整合框架,好像是Struts2+Hibernate3+Spring4,这次是相关的Jar从相关的官网下的. 我整合的环境: --Win 7 64 --MySQL 5.6 --MyEclipse 2014 --Jar包:struts-2.3.28.spring-framework-4.0.4.R

Struts2的使用以及Spring整合Struts2

一.如何单独使用Struts2 (1)引入struts2的jar包 commons-fileupload-1.2.1.jar freemarker-2.3.15.jar ognl-2.7.3.jar struts2-core-2.1.8.jar xwork-core-2.1.6.jar (2)在项目的src下引入struts.xml文件 <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts

Spring整合Struts2

Spring整合Struts21整合目的:让Spring的IOC容器去管理Struts2的Action, 2Struts2是web开源框架,Spring要整合Struts2,也就是说要在web应用使用Spring①. 需要额外加入的 jar 包:spring-web-4.0.0.RELEASE.jarspring-webmvc-4.0.0.RELEASE.jar ②. Spring 的配置文件, 和非 WEB 环境没有什么不同 ③. 需要在 web.xml 文件中加入如下配置: <!-- 配置

整合Struts2.2+Spring3.0

2014-08-08 学习李刚老师的j2ee整合struts2+spring3 JAR包链接 http://download.csdn.net/detail/u010393809/7732235 项目outline 1.引入JAR包,上面已经贴了JAR包下载链接 2.配置Struts2,只需要引入struts2必需的那几个包,此时暂时不要引入struts2-spring-plugin-2.2.1.jar,不然会抛出javaPointerNull的异常 配置web.xml; 配置struts.xm

整合Struts2框架和Spring框架

-----------------------siwuxie095 整合 Struts2 框架和 Spring 框架 1.导入相关 jar 包(共 27 个) (1)导入 Struts2 的基本 jar 包(13 个) 其中: 在 Struts2 和 Hibernate 中,都有 javassist,会产生冲突, 选择高版本,删除低版本即可 (2)导入 Spring 的核心 jar 包和日志相关的 jar 包(6 个) Commons Logging 下载链接: http://commons.a

整合struts2+spring+hibernate

 一.准备struts2+spring+hibernate所需要的jar包: 新建web项目并将jar包引入到工程项目中. 二.搭建struts2环境 a.在web项目的web.xml中加入struts2的过滤器,以过滤用户的所有请求,该过滤器将请求交给对应的Action处理 . <filter> <filter-name>Struts2</filter-name> <filter-class> org.apache.struts2.dispatcher

Spring4学习笔记-Spring4整合Struts2

Person.java public class Person { private String username; public void setUsername(String username) { this.username = username; } } PersonService.java public class PersonService { public void save() { System.out.println("PersonService save...");

Flexigrid的使用(整合Struts2)

Flexigrid是一个jQuery表格插件 下载地址:http://download.csdn.net/detail/itmyhome/7613879 使用方法: 一.相关资源文件的引入 <link rel="stylesheet" type="text/css" href="css/flexigrid.css"> <script type="text/javascript" src="js/jq