guice整合struts2与jpa,guice的使用(九)
传统我们开发一般使用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&useUnicode=true&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&useUnicode=true&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的使用(九)的更多相关文章
- guice整合struts2,guice的使用(八)
平时我们习惯用了spring整合struts2,今天我们就来见识一下guice整合struts2吧. 看web.xml配置: <?xml version="1.0" enco ...
- guice 整合ninja framework(七)
ninja是一个优秀的,轻量级的mvc框架,它与google guice整合比较好.下面看一下例子: 我们在web.xml 配置一下: <listener> <listener-cl ...
- Struts2的使用以及Spring整合Struts2
一.如何单独使用Struts2 (1)引入struts2的jar包 commons-fileupload-1.2.1.jar freemarker-2.3.15.jar ognl-2.7.3.jar ...
- spring整合struts2
1. Spring 如何在 WEB 应用中使用 ? 1). 需要额外加入的 jar 包: spring-web-4.0.0.RELEASE.jarspring-webmvc-4.0.0.RELEASE ...
- Spring 整合 Struts2
1. Spring 如何在 WEB 应用中使用 ? 1). 需要额外加入的 jar 包: spring-web-4.0.0.RELEASE.jar spring-webmvc-4.0.0.RELEAS ...
- Spring学习6-Spring整合Struts2
一.Spring为什么要整合Struts2 Struts2与Spring进行整合的根本目的就是要让 Spring为Struts2的Action注入所需的资源对象,它们整合的原理则是只要导入了s ...
- 基于注解整合struts2与spring的时候如果不引入struts2-spring-plugin包自动装配无效
基于注解整合struts2与spring的时候如果不引入struts2-spring-plugin包,自动装配将无效,需要spring注入的对象使用时将抛出空指针异常(NullPointerExcep ...
- Spring(四):Spring整合Hibernate,之后整合Struts2
背景: 上一篇文章<Spring(三):Spring整合Hibernate>已经介绍使用spring-framework-4.3.8.RELEASE与hibernate-release-5 ...
- Maven项目整合Struts2框架
-------------------------siwuxie095 Maven 项目整合 Struts2 框架 1. ...
随机推荐
- POJ_2186_Popular Cows_强连通分量
Popular Cows Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 30680 Accepted: 12445 De ...
- PAT_A1151#LCA in a Binary Tree
Source: PAT A1151 LCA in a Binary Tree (30 分) Description: The lowest common ancestor (LCA) of two n ...
- 【剑指Offer】30、连续子数组的最大和
题目描述: HZ偶尔会拿些专业问题来忽悠那些非计算机专业的同学.今天测试组开完会后,他又发话了:在古老的一维模式识别中,常常需要计算连续子向量的最大和,当向量全为正数的时候,问题很好解决.但是 ...
- VUE常见问题解决
1.vue模板加载顺序 computed:例如分页的配置: created:dom加载前一般用来生成dom mounted:dom加载后用来覆盖渲染或者基于dom的操作 2.关于this指向的问题 通 ...
- Linux思维导图之计划任务
查漏补缺,理解概念,及时总结,互相交流,欢迎拍砖. 用yum install -y vixie-cron这个命令进行安装计划任务服务,可以在安装之前使用crontab -e进行检测一下,服务器是否安装 ...
- BZOJ 3218 UOJ #77 A+B Problem (主席树、最小割)
大名鼎鼎的A+B Problem, 主席树优化最小割-- 调题死活调不对,一怒之下改了一种写法交上去A了,但是改写法之后第4,5个点常数变大很多,于是喜提UOJ全站倒数第三 目前还不知道原来的写法为什 ...
- mysql 7 种 join
一. select * from A inner join B on A.key = B.key 二. select * from A left join B on A.key = B.key 三. ...
- springMVC知识点复习
@ResponseBody和@RequestBody的使用 <html> <script type="text/javascript" src="rel ...
- UVA 10039 Railroads
这道题好吧,一开始便是拓扑排序的想法,搞了好久,试了多组测试数据,没错啊,可是没过...作孽啊,竟然忘了拓扑不能处理环,白浪费了一晚上... 只好用动态规划了.. DP[time][city]表示在t ...
- HDU 3691
一个源点,一个汇点,明显是网络流的问题,但据说用网络流来求最小割,会超时..囧,那出题的人是怎么想的... 用SW的算法来求最小割. #include <iostream> #includ ...