一、新建项目MyApp如下图所示:
new=>other=>web=>动态web工程(Dynamic web project)
点下一步,输入你的工程名字:myApp,configition选择custom(自定义方式),如下图:
然后选择你需要的东西或插件(可不选),接下来一路next,完成工程创建,现在你的工程目录如下:
二、copy相关的包到WEB-INF/lib下,这些包主要包括:
各个包的作用请自寻查找相关资料,当然这些包并不是每个都需要,如oscache.jar包,如果你没有用到cache的话,完全不必要包含进来。
三、配置web.xml,让你的spring和struts得到加载
spring的配置一般有两种方式:
1、利用ContextLoaderListener
Spring提供一个ContextLoaderListener类,该类可以作为Web应用的 Listener使用,它会在Web应用启动时自动查找WEB-INF/下的applicationContext.xml配置文件(Spring的配置文件),并且根据该文件来创建Spring容器。
因此,如果Web应用中只有一个Spring配置文件,并且文件名为applicationContext.xml,并将该文件放在Web应用的WEB-INF路径下,则只需在web.xml文件中增加如下一段即可:
<!-- 根据默认配置文件来初始化Spring容器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
2、如果有多个配置文件需要载入,则应该使用<context-param>元素来确定配置文件的文件名。ContextLoaderListener加载时,会查找名为contextConfigLocation的参数。因此,配置context-param时参数名字应该是contextConfigLocation。
因此,如果应用中的Spring配置文件有多个,则应该采用如下形式的web.xml文件来创建Spring容器。
<?xml version="1.0" encoding="GBK"?>
<!-- 指定Web应用配置文件的Schema信息 -->
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<!-- 确定多个配置文件 -->
<context-param>
<!-- 参数名为contextConfigLocation -->
<param-name>contextConfigLocation</param-name>
<!-- 多个配置文件之间以,隔开 -->
<param-value>/WEB-INF/daoContext.xml,/WEB-INF/applicationContext.xml</param-value>
</context-param>
<!-- 采用Listener完成Spring容器的初始化 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>
采用上面的一种配置,当服务启动时,容器会自动加载WEB-INF/applicationContext.xml文件的配置内容。
然后配置struts2,使其能够和spring一起工作,struts2提供了一个插件:struts2-spring-plugin,如上图,只需将
struts2-spring-plugin-2.0.11.jar拷贝到/WEB-INF/lib/下即可.
然后在web.xml中加入如下配置:
<!--处理hibernate的延迟加载-->
<filter>
<filter-name>hibernateFilter</filter-name>
<filter-class>
org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
</filter-class>
</filter>
<!---安装插件时用-->
<filter>
<filter-name>struts-cleanup</filter-name>
<filter-class>
org.apache.struts2.dispatcher.ActionContextCleanUp
</filter-class>
</filter>
<!----struts2的默认拦截器---->
<filter>
<filter-name>struts</filter-name>
<filter-class>
org.apache.struts2.dispatcher.FilterDispatcher
</filter-class>
<init-param>
<param-name>actionPackages</param-name>
<param-value>
org.apache.struts2.showcase.person
</param-value>
</init-param>
</filter>
<!----seimesh过滤器(用来保证网站更个一致性的插件)--->
<filter>
<filter-name>sitemesh</filter-name>
<filter-class>
com.opensymphony.module.sitemesh.filter.PageFilter
</filter-class>
</filter>
<filter-mapping>
<filter-name>struts-cleanup</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>sitemesh</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>struts</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
注意:第一个监听器是用来处理hibernate的延迟加载的,后三个为struts2或其插件的过滤器,注意后三个过滤器的顺序必须按以上顺序进行配置,否则可能找不到插件。
整个配置如下所示:
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<!-- 对url进行编码 -->
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>
org.springframework.web.filter.CharacterEncodingFilter
</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<!--处理hibernate的延迟加载-->
<filter>
<filter-name>hibernateFilter</filter-name>
<filter-class>
org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
</filter-class>
</filter>
<filter>
<filter-name>struts-cleanup</filter-name>
<filter-class>
org.apache.struts2.dispatcher.ActionContextCleanUp
</filter-class>
</filter>
<filter>
<filter-name>struts</filter-name>
<filter-class>
org.apache.struts2.dispatcher.FilterDispatcher
</filter-class>
<init-param>
<param-name>actionPackages</param-name>
<param-value>
org.apache.struts2.showcase.person
</param-value>
</init-param>
</filter>
<filter>
<filter-name>sitemesh</filter-name>
<filter-class>
com.opensymphony.module.sitemesh.filter.PageFilter
</filter-class>
</filter>
<filter-mapping>
<filter-name>struts-cleanup</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>sitemesh</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>struts</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<display-name>myApp</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
三、配置你的spring和struts以及hibernate
applicationContext.xml内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">
<!-- 数据源 -->
<bean id="myDataSource"
class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<!-----数据源----->
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url"
value="jdbc:mysql://localhost:3306/student?useUnicode=true&characterEncoding=utf8" />
<property name="username" value="root" />
<property name="password" value="123456" />
</bean>
<!-- SessionFactory -->
<bean id="mySessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="myDataSource" />
<property name="mappingResources">
<list>
<!--hibernate配置文件-->
<value>com/furniture/model/Student.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<value>
hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect
hibernate.show_sql=true
</value>
</property>
</bean>
<bean id="dao" class="com.furniture.dao.DAO">
<property name="sessionFactory" ref="mySessionFactory" />
</bean>
<!-- 事务-->
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="mySessionFactory" />
</bean>
<!---增删改接口----->
<bean id="persistenceDelegate" class="com.chineseall.service.dao.hibernate.DefaultPersistenceDelegate">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<!-- 查询接口 -->
<bean id="queryDelegate" class="com.chineseall.service.dao.hibernate.DefaultQueryDelegate">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<!-- cache接口 -->
<bean id="cacheAdministrator" class="com.opensymphony.oscache.general.GeneralCacheAdministrator" destroy-method="destroy"/>
<bean id="cache" factory-bean="cacheAdministrator" factory-method="getCache"/>
<!----service定义---->
<bean id="studentService"
class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="transactionManager" ref="transactionManager" />
<property name="target">
<bean class="com.furniture.service.StudentServiceImpl">
<property name="sessionFactory" ref="mySessionFactory" />
</bean>
</property>
<property name="transactionAttributes">
<props>
<prop key="*">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>
</beans>
在src下新建struts.xml.内容如下:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<constant name="struts.objectFactory" value="spring" />
<constant name="struts.devMode" value="true" />
<package name="struts-shop" extends="struts-default">
<interceptors>
<interceptor-stack name="myStack">
<interceptor-ref name="i18n" />
<interceptor-ref name="basicStack" />
</interceptor-stack>
</interceptors>
<default-interceptor-ref name="myStack" />
</package>
</struts>
如果在web.xml中没有配置sitemash这个插件,如上配置即可运行,为了支持 sitemesh,必须添加两个额外的配置文件:sitemesh.xml、decorators.xml,内容分别如下:
sitemesh.xml:
<sitemesh>
<property name="decorators-file" value="/WEB-INF/decorators.xml" />
<excludes file="${decorators-file}" />
<page-parsers>
<parser content-type="text/html"
class="com.opensymphony.module.sitemesh.parser.FastPageParser" />
</page-parsers>
<decorator-mappers>
<mapper
class="com.opensymphony.module.sitemesh.mapper.PageDecoratorMapper">
<param name="property.1" value="meta.decorator" />
<param name="property.2" value="decorator" />
</mapper>
<mapper
class="com.opensymphony.module.sitemesh.mapper.FrameSetDecoratorMapper">
</mapper>
<mapper
class="com.opensymphony.module.sitemesh.mapper.AgentDecoratorMapper">
<param name="match.MSIE" value="ie" />
<param name="match.Mozilla [" value="ns" />
<param name="match.Opera" value="opera" />
<param name="match.Lynx" value="lynx" />
</mapper>
<mapper
class="com.opensymphony.module.sitemesh.mapper.PrintableDecoratorMapper">
<param name="decorator" value="printable" />
<param name="parameter.name" value="printable" />
<param name="parameter.value" value="true" />
</mapper>
<mapper
class="com.opensymphony.module.sitemesh.mapper.RobotDecoratorMapper">
<param name="decorator" value="robot" />
</mapper>
<mapper
class="com.opensymphony.module.sitemesh.mapper.ParameterDecoratorMapper">
<param name="decorator.parameter" value="decorator" />
<param name="parameter.name" value="confirm" />
<param name="parameter.value" value="true" />
</mapper>
<mapper
class="com.opensymphony.module.sitemesh.mapper.FileDecoratorMapper">
</mapper>
<mapper
class="org.apache.struts2.sitemesh.NoneDecoratorMapper">
</mapper>
<mapper
class="com.opensymphony.module.sitemesh.mapper.ConfigDecoratorMapper">
<param name="config" value="${decorators-file}" />
</mapper>
</decorator-mappers>
</sitemesh>
decorators.xml:
<?xml version="1.0" encoding="utf-8"?>
<decorators defaultdir="/WEB-INF/decorators">
<!-- Any urls that are excluded will never be decorated by Sitemesh -->
<excludes>
<pattern>/styles/*</pattern>
<pattern>/scripts/*</pattern>
<pattern>/images/*</pattern>
<pattern>/dojo/*</pattern>
</excludes>
<decorator name="main" page="main.jsp">
<pattern>/*</pattern>
</decorator>
</decorators>
然后在WEB-INF/decorators/建立几个模板文件:
left.jsp、main.jsp、nav.jsp内容如下:
left.jsp:
<%@ page contentType="text/html;charset=utf-8"%>
<%@taglib prefix="s" uri="/struts-tags"%>
<link href="/styles/main.css" _fcksavedurl=""/styles/main.css"" type="text/css" rel="stylesheet" />
<table width="100%" border="0px" cellpadding="0" cellspacing="0" style="margin: 0px 0px;">
<tr bgcolor="#AD2810" height="28px"><td><b><font size="4" color="#FFFFFF"> Catalogue</font></b></td></tr>
<tr><td> </td></tr>
<tr><td><s:a href="#">这是链接</s:a></td></tr>
<tr><td><s:a href="#">这是链接</s:a></td></tr>
<tr><td><s:a href="#">这是链接</s:a></td></tr>
<tr><td><s:a href="#">这是链接</s:a></td></tr>
<tr><td><s:a href="#">这是链接</s:a></td></tr>
<tr><td><s:a href="#">这是链接</s:a></td></tr>
<tr><td><s:a href="#">这是链接</s:a></td></tr>
<tr><td><s:a href="#">这是链接</s:a></td></tr>
<tr><td><s:a href="#">这是链接</s:a></td></tr>
<tr><td><s:a href="#">这是链接</s:a></td></tr>
</table>
main.jsp:
<%@ taglib uri="sitemesh-decorator" prefix="decorator"%>
<%@taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
<link href="/styles/main.css" _fcksavedurl=""/styles/main.css"" type="text/css" rel="stylesheet" />
<title><decorator:title default="..chinese furniture" /></title>
<decorator:head />
</head>
<body>
<div class="main_container">
<div class="header">
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="4%" rowspan="3"><img src="/images/index/Z3.jpg" /></td>
<td width="24%" height="29"> </td>
<td width="19%" rowspan="3"> </td>
<td width="47%" rowspan="3" align="right"><s:i18n
name="globalMessages">
<s:text name="shop.company.introduce" />
</s:i18n></td>
<td width="6%" rowspan="3"> <img src="/images/index/4.jpg"></td>
</tr>
<tr>
<td height="46"><span class="STYLE1"> <strong>
<s:i18n name="globalMessages">
<s:text name="site.name" />
</s:i18n> </strong></span></td>
</tr>
<tr>
<td height="38"><span class="STYLE1"><strong>
<s:i18n name="globalMessages">
<s:text name="shop.company.name" />
</s:i18n></strong></span></td>
</tr>
</table>
</div>
<div class="navigator"><s:include value="nav.jsp"></s:include></div>
<div class="left"><s:include value="left.jsp"></s:include></div>
<div class="main"><decorator:body /></div>
<div class="footer"><s:i18n name="globalMessages">
<s:text name="shop.footer.info" />
</s:i18n></div>
</div>
</body>
</html>
nav.jsp:
<%@ page contentType="text/html;charset=utf-8"%>
<%@taglib prefix="s" uri="/struts-tags"%>
<link href="/styles/main.css" _fcksavedurl=""/styles/main.css"" type="text/css" rel="stylesheet" />
<table>
<tr>
<td valign="bottom"><s:a href="/index.jsp?request_locale=en_US" cssClass="navLink">
<s:i18n name="globalMessages">
<s:text name="shop.home" />
</s:i18n>
</s:a></td>
<td valign="bottom"><s:a href="#" cssClass="navLink">
<s:i18n name="globalMessages">
<s:text name="shop.online.lnventory" />
</s:i18n>
</s:a></td>
<td valign="bottom"><s:a href="#" cssClass="navLink">
<s:i18n name="globalMessages">
<s:text name="shop.packing.shipping" />
</s:i18n>
</s:a></td>
<td valign="bottom"><s:a href="#" cssClass="navLink">
<s:i18n name="globalMessages">
<s:text name="shop.order" />
</s:i18n>
</s:a></td>
<td valign="bottom"><s:a href="#" cssClass="navLink">
<s:i18n name="globalMessages">
<s:text name="shop.faq" />
</s:i18n>
</s:a></td>
<td valign="bottom"><s:a href="#" cssClass="navLink">
<s:i18n name="globalMessages">
<s:text name="shop.about" />
</s:i18n>
</s:a></td>
<td valign="bottom"><s:a href="#" cssClass="navLink">
<s:i18n name="globalMessages">
<s:text name="shop.connect.us" />
</s:i18n>
</s:a></td>
<td valign="bottom"><s:i18n name="globalMessages">
</s:i18n> <s:a href="/student/locale.action?request_locale=en_US" cssClass="navLink">English</s:a>
<s:a href="/student/locale.action?request_locale=zh_CN" cssClass="navLink">简体中文</s:a></td>
</tr>
</table>
部署你的项目到tomcat下,在WebContent搞一个index.jsp,启动tomcat,
项目配置完毕