传统我们开发一般使用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. JSP_内置对象_请求转发和请求重定向的区别

    请求重定向:客户端行为,response.sendRedirect(),从本质上将等同与两次请求,前一次请求request对象不会保存,地址栏的URL地址会改变. 请求转发:服务器行为,request ...

  2. POJ 2229 Sumsets(找规律,预处理)

    题目 参考了别人找的规律再理解 /* 8=1+1+1+1+1+1+1+1+1 1 8=1+1+1+1+1+1+1+2 2 3 8=1+1+1+1+2+2 8=1+1+1+1+4 4 5 8=1+1+2 ...

  3. linux文件的特殊权限及隐藏权限

    基础知识 相信大家应该都知道linux的文件基本权限,使用ls -l命令可以显示文件的基本权限,"-rwxrwxrwx.",第一位表示文件的属性(是文件-  ,目录d等),后面每隔 ...

  4. Java中==和equals()的区别

  5. 06.系统编程-3.进程VS线程比较

    1.定义的不同 ==进程是系统进行资源分配和调度的一个独立单位.== ==线程是进程的一个实体,是CPU调度和分派的基本单位==,它是比进程更小的能独立运行的基本单位.线程自己基本上不拥有系统资源,只 ...

  6. Problem 19

    Problem 19 You are given the following information, but you may prefer to do some research for yours ...

  7. 9.Spring的IOC+MyBaits+log4j+Servlet

    1.创建如下mysql脚本 drop table if exists book_info; create table if not exists book_info( book_id ) primar ...

  8. Ubuntu14.043下QT5.5的安装与一点问题

    请注明来自于 http://www.cnblogs.com/usegear/p/5100720.html 1.下载qt-opensource-linux-x86-5.5.0.run(去教育镜像网站下载 ...

  9. 理解__builtin_clz特性

    a.c: /************************************************************************* * File: a.c * Brief: ...

  10. 敏捷迭代:Sprint燃尽图的7个图形特征及说明的问题

    本文写于很多年前(2006),并在很多地方被引用.而现在,笔者对于Sprint燃尽图的理解有了戏剧性的变化--在看到很多团队滥用它之后.笔者不再建议团队做Sprint燃尽图,因为它们不仅不会增加多少有 ...