传统我们开发一般使用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. 作业07之《MVC模式》

    MVC(Model View Controller)模型-视图-控制器 MVC与模板概念的理解 MVC本来是存在于Desktop程序中的,M是指数据模型,V是指用户界面,C则是控制器.使用MVC的目的 ...

  2. 玲珑杯#20 C 漆黑的太阳——莫队

    题目:https://www.ifrog.cc/acm/problem/1155 题解:https://www.ifrog.cc/acm/solution/28 1.如何不重复计算一个值 自己想的是对 ...

  3. day36 类的三大特性---封装以及Property特性

    目录 类的封装 如果真的要拿 类的property特性 setter & deleter 类属性用法 类与对象的绑定方法和非绑定方法 对象方法&类方法&静态方法 隐藏模块内的函 ...

  4. Docker拉取images时报错Error response from daemon

    docker拉取redis时,抛出以下错误: [master@localhost ~]$ docker pull redis Using default tag: latest Error respo ...

  5. 【ubuntu子系统】使用windows自带的ubuntu子系统

      在windows10系统中,自带了一款ubuntu子系统,就像是一个应用程序,一款软件,提供ubutnu的terminal窗口,可以使用对应的命令行模式.最重要的是,可以直接用来连接linux服务 ...

  6. 拷贝构造和拷贝赋值、静态成员(static)、成员指针、操作符重载(day06)

    十七 拷贝构造和拷贝赋值 浅拷贝和深拷贝 )如果一个类中包含指针形式的成员变量,缺省的拷贝构造函数只是复制了指针变量的本身,而没有复制指针所指向的内容,这种拷贝方式称为浅拷贝. )浅拷贝将导致不同对象 ...

  7. CSS 利用transform达到居中效果

    <body> <div class="center"> .... </div> </body> 让left和top都是50%,这在水 ...

  8. JavaSE 学习笔记之封装(四)

    封 装(面向对象特征之一):是指隐藏对象的属性和实现细节,仅对外提供公共访问方式. 好处:将变化隔离:便于使用:提高重用性:安全性. 封装原则:将不需要对外提供的内容都隐藏起来,把属性都隐藏,提供公共 ...

  9. 洛谷 P2341 BZOJ 1051 [HAOI2006]受欢迎的牛

    题目描述 每头奶牛都梦想成为牛棚里的明星.被所有奶牛喜欢的奶牛就是一头明星奶牛.所有奶 牛都是自恋狂,每头奶牛总是喜欢自己的.奶牛之间的“喜欢”是可以传递的——如果A喜 欢B,B喜欢C,那么A也喜欢C ...

  10. 百度之星2014复赛 - 1002 - The Query on the Tree

    先上题目: The Query on the Tree Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (J ...