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

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

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

然后看一下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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
<filter>
<filter-name>guiceFilter</filter-name>
<filter-class>com.google.inject.servlet.GuiceFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>guiceFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<listener>
<listener-class>com.ming.core.web.listener.GoogleGuiceServletContextListener</listener-class>
</listener> <welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>

然后是struts.xml的配置:

 <?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd"> <struts>
<!-- 引用guice代理 -->
<constant name="struts.objectFactory" value="guice" />
<constant name="struts.i18n.encoding" value="UTF-8" />
<constant name="struts.enable.DynamicMethodInvocation" value="true"/>
<include file="com/ming/user/action/userStruts.xml"></include>
</struts>

然后是jpa的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_2_0.xsd"
version="2.0">
<persistence-unit name="myunit" transaction-type="RESOURCE_LOCAL"> <provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>com.ming.user.entity.Student</class>
<properties>
<property name="hibernate.connection.provider_class"
value="org.hibernate.service.jdbc.connections.internal.C3P0ConnectionProvider" />
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect" />
<property name="hibernate.c3p0.max_size" value="2" />
<property name="hibernate.c3p0.min_size" value="1" />
<property name="hibernate.c3p0.timeout" value="120" />
<property name="hibernate.c3p0.max_statements" value="100" />
<property name="hibernate.c3p0.idle_test_period" value="120" />
<property name="hibernate.c3p0.acquire_increment" value="1" />
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver" />
<property name="hibernate.connection.url"
value="jdbc:mysql://localhost:3306/test?autoReconnect=true&amp;useUnicode=true&amp;characterEncoding=gbk" />
<property name="hibernate.connection.username" value="root" />
<property name="hibernate.connection.password" value="root" />
<property name="hibernate.temp.use_jdbc_metadata_defaults"
value="false" />
</properties> <!-- <properties> <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect"
/> <property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"
/> <property name="hibernate.connection.driver" value="com.mysql.jdbc.Driver"
/> <property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/test?autoReconnect=true&amp;useUnicode=true&amp;characterEncoding=UTF-8"
/> <property name="hibernate.connection.user" value="root" /> <property name="hibernate.connection.password"
value="root" /> <property name="hibernate.temp.use_jdbc_metadata_defaults"
value="false"/> </properties> -->
</persistence-unit>
</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下。

框架例子下载:源码下载

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

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

    平时我们习惯用了spring整合struts2,今天我们就来见识一下guice整合struts2吧. 看web.xml配置: <?xml version="1.0" enco ...

  2. guice 整合ninja framework(七)

    ninja是一个优秀的,轻量级的mvc框架,它与google guice整合比较好.下面看一下例子: 我们在web.xml 配置一下: <listener> <listener-cl ...

  3. Struts2的使用以及Spring整合Struts2

    一.如何单独使用Struts2 (1)引入struts2的jar包 commons-fileupload-1.2.1.jar freemarker-2.3.15.jar ognl-2.7.3.jar ...

  4. spring整合struts2

    1. Spring 如何在 WEB 应用中使用 ? 1). 需要额外加入的 jar 包: spring-web-4.0.0.RELEASE.jarspring-webmvc-4.0.0.RELEASE ...

  5. Spring 整合 Struts2

    1. Spring 如何在 WEB 应用中使用 ? 1). 需要额外加入的 jar 包: spring-web-4.0.0.RELEASE.jar spring-webmvc-4.0.0.RELEAS ...

  6. Spring学习6-Spring整合Struts2

    一.Spring为什么要整合Struts2     Struts2与Spring进行整合的根本目的就是要让 Spring为Struts2的Action注入所需的资源对象,它们整合的原理则是只要导入了s ...

  7. 基于注解整合struts2与spring的时候如果不引入struts2-spring-plugin包自动装配无效

    基于注解整合struts2与spring的时候如果不引入struts2-spring-plugin包,自动装配将无效,需要spring注入的对象使用时将抛出空指针异常(NullPointerExcep ...

  8. Spring(四):Spring整合Hibernate,之后整合Struts2

    背景: 上一篇文章<Spring(三):Spring整合Hibernate>已经介绍使用spring-framework-4.3.8.RELEASE与hibernate-release-5 ...

  9. Maven项目整合Struts2框架

    -------------------------siwuxie095                                 Maven 项目整合 Struts2 框架         1. ...

随机推荐

  1. swiper和Navigator组件

    <swiper class="index-banner" indicator-dots="{{true}}" autoplay="{{true} ...

  2. ESP32 开发笔记(十二)LittlevGL 添加自定义字体和物理按键

    LittlevGL 添加自定义字体获取字库 ttf 文件可以从一些网站上获取字库文件,比如请注意字体许可证 生成源文件使用 LittlevGL 提供的字库文件转换工具,将 ttf 字库文件转换为源文件 ...

  3. eas之style接口

    Obj可以是KDTable对象,也可以是IRow,IColumn,ICell对象锁定Obj.getStyleAttributes().setLocked(true);Obj.getStyleAttri ...

  4. Codeforces Round #548 (Div. 2) C. Edgy Trees

    You are given a tree (a connected undirected graph without cycles) of 

  5. RF学习使用记录【4】

    四 Extending Robot Framework 4.1 Creating test libraries RF的测试能力由测试库支持决定,已经有许多的测试库,有一些随着RF框架安装,但是更多的需 ...

  6. (34)Spring Boot的启动器Starter详解【从零开始学Spring Boot】

    Spring Boot应用启动器基本的一共有N(现知道的是44)种:具体如下: 1)spring-boot-starter 这是Spring Boot的核心启动器,包含了自动配置.日志和YAML. 2 ...

  7. mybatis使用-helloword(一)

    前言 首先感谢https://my.oschina.net/zudajun/blog/665956(jd上也出书了貌似)  这位作者.让自己能系统的看完和理解第一个框架的源码(其实我反复看了4遍以上, ...

  8. 0208MySQL5.7之Group Replication

    转自http://blog.itpub.net/29510932/viewspace-2055679/ MySQL Group Replication: Hello World! 对测试版(on la ...

  9. 设计模式C++实现——组合模式

    模式定义: 组合模式同意你将对象组合成树形结构来表现"总体/部分"层次结构.组合能让客户以一致的方式处理个别对象以及对象组合. 这个模式可以创建一个树形结构,在同一个结构中处理嵌套 ...

  10. TestNG升级

    TestNG 6.5.1 or above is required,please update your TestNG or uncheck 'Use project TestNG jar' from ...