Webbuilder和Spring框架搭建
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
1.搭建好Spring环境或者Spring+Hibernate环境
2.下载Webbuilder使用源码搭建好Webbuilder环境
Webbuilder配置要点。
Tomcat的Context.xml中添加
<Resource
name="jdbc/wb_mysql"
auth="Container"
type="javax.sql.DataSource"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://127.0.0.1:3306/webbuilder?autoReconnect=true"
username="root"
password=""
maxActive="20"
maxIdle="10"
maxWait="-1"/>
对应的jar包也要有;
项目的Web.xml中配置
<?xml version="1.0"encoding="UTF-8"?>
<web-app xmlns:xsi="/2001/XMLSchema-instance"
xmlns="/xml/ns/javaee"
xmlns:web="/xml/ns/javaee"
xsi:schemaLocation="/xml/ns/javaee
/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID"
version="3.0">
<!-- xmlns="/xml/ns/javaee" -->
<!-- 項目名稱 -->
<display-name>WebBuilder</display-name>
<welcome-file-list>
<welcome-file>main</welcome-file>
</welcome-file-list>
<!-- 配置资源,Spring配置文件 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:/applicationContext*.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</ listener-class>
</listener>
<!-- WebBuilder核心服務 -->
<servlet>
<description>WebBuilder Core Service</description>
<display-name>main</display-name>
<servlet-name>main</servlet-name>
<servlet-class>mon.Main</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>main</servlet-name>
<url-pattern>/main</url-pattern>
</servlet-mapping>
<servlet>
<description>Task Service Initializer</description>
<display-name>taskservice</display-name>
<servlet-name>taskservice</servlet-name>
<servlet-class>mon.TaskService</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet>
<description>WebBuilder Resource Cache</description>
<display-name>wbcache</display-name>
<servlet-name>wbcache</servlet-name>
<servlet-class>mon.WbCache</servlet-class>
<load-on-startup>3</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>wbcache</servlet-name>
<url-pattern>/webbuilder/*</url-pattern>
</servlet-mapping>
<resource-ref>
<res-ref-name>jdbc/wb_mysql</res-ref-name>这就是上面Context.xml配置的<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
</web-app>
配置Webbuilder的config文件,避免每次进入都让你重新安装一次
{
cacheCheckModified: true,
cacheEnabled: true,
cacheGzipMinSize: 6144,
charset: "",
dbType: "",
fileNameCharset: "",
finalMethod: "",
initMethod: "",
jndi: "java:comp/env/jdbc/wb_mysql",
log: true,
printError: true,
respGzipMinSize: 10240,
serialNumber: "",
serverId: "2",
startTask: true
}
配置到这里,基本可以登录到Webbuilder了,Spring的配置自己网上百度,我的是这样的<?xml version="1.0"encoding="UTF-8"?>
<beans xmlns="/schema/beans"
xmlns:xsi="/2001/XMLSchema-instance"
xmlns:aop="/schema/aop"
xmlns:context="/schema/context"
xmlns:tx="/schema/tx"
xsi:schemaLocation="/schema/beans
/schema/beans/spring-beans.xsd
/schema/aop
/schema/aop/spring-aop-2.5.xsd
/schema/context
/schema/context/spring-context-2.5.xsd
/schema/tx
/schema/tx/spring-tx-2.5.xsd">
<!-- 启用spring注解支持 -->
<context:annotation-config/>
<bean id="SpringContextUtil"
class="com.xietian.spring.SpringContextUtil" lazy-init="false"/>
<!-- 配置sessionFactory -->
<bean id="dataSource"class="mons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName"value="com.mysql.jdbc.Driver"/>
<property name="url"value="jdbc:mysql://127.0.0.1:3306/webbuilder"/> <property name="username"value="root"/>
<property name="password"value=""/>
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> <property name="dataSource"ref="dataSource"/>
<property name="hibernateProperties">
<props>
<prop
key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
<!-- 配置註解,掃描包 -->
<property name="packagesToScan">
<list>
<value>com.xietian</value>
</list>
</property>
</bean>
<!-- 配置事务 -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager"> <property name="sessionFactory"ref="sessionFactory"/> </bean>
<tx:advice id="txadvice"transaction-manager="transactionManager"> <tx:attributes>
<!-- 对get/load/search开头的方法要求只读事务 -->
<tx:method name="get*"propagation="SUPPORTS"read-only="true"/>
<tx:method name="load*"propagation="SUPPORTS"read-only="true"/>
<tx:method name="search*"propagation="SUPPORTS"read-only="true" />
<tx:method name="query*"propagation="SUPPORTS"read-only="true" />
<tx:method name="find*"propagation="SUPPORTS"read-only="true"/>
<!-- 对其它方法要求事务 -->
<!--
<tx:method name="*" propagation="REQUIRED" />
-->
</tx:attributes>
</tx:advice>
<!-- spring事務 -->
<!-- (* com.evan.crm.service.*.*(..))中几个通配符的含义: -->
<!-- 第一个 * ——通配随便率性返回值类型 -->
<!-- 第二个 * ——通配包com.evan.crm.service下的随便率性class -->
<!-- 第三个 * ——通配包com.evan.crm.service下的随便率性class的随便率性办法-->
<!-- 第四个 .. ——通配办法可以有0个或多个参数 -->
<aop:config proxy-target-class="true">
<aop:pointcut id="bussinessService"expression="execution(* *..service..*Service*.*(..))"/>
<aop:advisor pointcut-ref="bussinessService"advice-ref="txadvice"/> </aop:config>
<!-- 自动扫描包 start xietian -->
<context:component-scan base-package="com.xietian"/>
<!-- 自动扫描包 end xietian -->
<!--基于@AspectJ切面的驱动器,如果没有这句话切面的代码是不会执行的 -->
<aop:aspectj-autoproxy/>
<!--
<bean name="TestAction" class="com.xietian.action.TestAction"/>
-->
</beans>
接下来就自己写一个SpringContextUtil方法,就上面红字地方的,方法如下
package com.xietian.spring;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
/**
* <p>ClassName: SpringContextUtil</p>
* <p>Description: 在Spring容器之外获取Spring bean实例的工具类</p>
* <p>Date: 2015-5-8</p>
*/
public class SpringContextUtil implements ApplicationContextAware {
/**
* Spring配置文件上下文
*/
private static ApplicationContext applicationContext;
/**
* 实现ApplicationContextAware接口的回调方法,设置上下文环境
* @param applicationContext
* @throws BeansException 异常
*/
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
SpringContextUtil.applicationContext = applicationContext;
}
/**
*
* <p>Description: 取得ApplicationContext上下文</p>
* @return ApplicationContext上下文
*/
public static ApplicationContext getApplicationContext() {
return applicationContext;
}
/**
*
* <p>Description: 取得bean实例</p>
* @param name bean名称
* @return bean实例
* @throws BeansException 异常
*/
public static Object getBean(String name) throws BeansException {
return applicationContext.getBean(name);
}
// 如果需要其他的方法可以在此处定义追加
}
找到Webbuilder的源码中SysUtil类,在executeMethod方法中
Class<?> cls = Class.forName(className);
cls.getMethod(methodName, HttpServletRequest.class,
HttpServletResponse.class).invoke(cls, request, response); 修改成
if(classMethodName.startsWith("com.xietian")) {// com.xietian这是自己用Spring 管理的bean所在的文件夹
Object obj = SpringContextUtil.getBean(cls.getSimpleName());
cls.getMethod(methodName, HttpServletRequest.class,
HttpServletResponse.class).invoke(obj, request, response);
} else {
cls.getMethod(methodName, HttpServletRequest.class,
HttpServletResponse.class).invoke(cls, request, response);
}
对应的Action如下:
package com.xietian.action;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import ponent;
import com.webbuilder.utils.WebUtil;
@Component("TestAction")
public class TestAction {
public void outString(HttpServletRequest request, HttpServletResponse response) throws Exception {
System.out.println("Test");
WebUtil.response(response, (Object)"sss");
}
}
以上完成就OK。
可以正确的使用Webbuilder负责前台,替代S2SH框架的Struts2,采用Spring 管理后面,WebBuilder开发前台十分的方便。
本项目Webbuilder+Spring4+Hibernate4
Jar包。