整合struts2.1.8.1+sitemesh2.4.2+freemarker2.3.15+spring2.5+hibernate3.3.1

由struts2.1.6升级,整合struts2.1.8.1+sitemesh2.4.2+freemarker2.3.15+spring2.5+hibernate3.3.1

由于struts2.1.8.1 sitemesh和freemarker的配置方式改变了,查遍了官方文档和百度 google都没有找到解决问题的办法,自己尝试了N种方法后才找到解决办法,在此分享下。

 

web.xml

<?xml version="1.0" encoding="utf-8"?>
<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">
   <display-name>iemis base system</display-name> 
    <context-param> 
    	<param-name>log4jConfigLocation</param-name>
		<param-value>classpath:log4j.properties</param-value>
    </context-param>
    
    <filter> 
	    <filter-name>SetCharacterEncoding</filter-name> 
	    <filter-class>com.iemis.base.util.SetCharacterEncodingFilter</filter-class> 
	    <init-param> 
	      	<param-name>encoding</param-name> 
	      	<param-value>UTF-8</param-value> 
	    </init-param> 
	    <init-param> 
			<param-name>ignore</param-name> 
			<param-value>true</param-value> 
	    </init-param> 
   </filter>
    
    <filter>
        <filter-name>encoding</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>
    
    <filter>
		<filter-name>springSecurityFilterChain</filter-name>
		<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
	</filter>
	
	<filter>
		<filter-name>openSessionInViewFilter</filter-name>
		<filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
	</filter>
 
	<filter>
        <filter-name>struts-prepare</filter-name>
        <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareFilter</filter-class>
    </filter>
    
     <filter>
        <filter-name>sitemesh</filter-name>
        <filter-class>org.apache.struts2.sitemesh.FreeMarkerPageFilter</filter-class>
    </filter>
	
	<filter>
        <filter-name>struts-execute</filter-name>
        <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsExecuteFilter</filter-class>
    </filter>
    
    <!-- 
    <filter>
        <filter-name>struts-cleanup</filter-name>
        <filter-class>org.apache.struts2.dispatcher.ActionContextCleanUp</filter-class>
    </filter>
    <filter>
        <filter-name>sitemesh</filter-name>
        <filter-class>org.apache.struts2.sitemesh.FreeMarkerPageFilter</filter-class>
    </filter>
    <filter>
        <filter-name>struts</filter-name>
        <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
    </filter>
    -->
	<filter-mapping> 
     <filter-name>SetCharacterEncoding</filter-name> 
     <servlet-name>action</servlet-name> 
   </filter-mapping>
   
	<filter-mapping>
		<filter-name>openSessionInViewFilter</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
    
	<filter-mapping>
        <filter-name>encoding</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping> 
	
	<filter-mapping>
		<filter-name>springSecurityFilterChain</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
	
  	<filter-mapping>
        <filter-name>struts-prepare</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-execute</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
	 
	 <!-- 
	 <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>
    
   <servlet>
		<servlet-name>jspSupportServlet</servlet-name>
		<servlet-class>org.apache.struts2.views.JspSupportServlet</servlet-class>
		<load-on-startup>1</load-on-startup>
	</servlet>
	
	<servlet>
        <servlet-name>sitemesh-freemarker</servlet-name>
        <servlet-class>com.opensymphony.module.sitemesh.freemarker.FreemarkerDecoratorServlet</servlet-class>
        <init-param>
            <param-name>TemplatePath</param-name>
            <param-value>/</param-value>
        </init-param>
        <init-param>
            <param-name>default_encoding</param-name>
            <param-value>UTF-8</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>sitemesh-freemarker</servlet-name>
        <url-pattern>*.action</url-pattern>
    </servlet-mapping>
    
	<servlet-mapping>
        <servlet-name>sitemesh-freemarker</servlet-name>
        <url-pattern>*.ftl</url-pattern>
    </servlet-mapping>
  
    <welcome-file-list> 
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
    <error-page>
      <error-code>403</error-code>
      <location>/error403.jsp</location>
    </error-page>
    <!-- error-page>
      <error-code>404</error-code>
      <location>/error.jsp</location>
    </error-page>
    <error-page>
      <exception-type>java.lang.Exception</exception-type>
      <location>/error.jsp</location>
    </error-page-->
</web-app>

 

注意:sitemesh-freemarker这个servlet要同时匹配action和ftl两个才可以,不能用/*匹配所有的,否则图片样式等也会给装饰。官方文档的访问后缀名就是.ftl不是action,但只配置了action ftl文件他就无法解析了,必须两个都配置了才行。

struts和sitemesh的配置也要用新的方式,否则freemarker里面action变量不能使用。

 

decorators.xml

<?xml version="1.0" encoding="UTF-8"?>
<!-- 
	定义构成复合视图的所有页面构件的描述
	(主要结构页面,header,footer...),如下例:
-->
<decorators defaultdir="/decorators">
	<!-- 在excludes元素下指定的页面将不会由SiteMesh来装饰 -->
	<excludes>
		<pattern>/index.jsp</pattern>
		<pattern>/scripts/*.*</pattern>
		<pattern>/images/*.*</pattern>
		<pattern>/styles/*.*</pattern>
		<pattern>/jasperreport/*.*</pattern>
	</excludes>
	
	<!-- iframe里面内容页面装饰器 -->
	<decorator name="simple" page="simple.ftl">
		<pattern>/browser/*/*/*.action</pattern>
		<pattern>/editor/*/*/*.action</pattern>
		<pattern>/admin/*/*/*.action</pattern>
		<pattern>/browser/home/*.action</pattern>
		<pattern>/editor/home/*.action</pattern>
		<pattern>/admin/*/*.action</pattern>
		<pattern>*.action</pattern>
	</decorator>
	
	<decorator name="complete" page="complete.ftl" webapp="/browser/">
		<pattern>/browser/*.action</pattern>
		<pattern>/editor/*.action</pattern>
		<pattern>/admin/*.action</pattern>
	</decorator>

</decorators> 
<!-- 
<decorator name="blog_editor" page="blog.ftl" webapp="/editor/">
		<pattern>/editor/*.action</pattern>
	</decorator>
	
	<decorator name="blog_browser" page="blog.ftl" webapp="/browser/">
		<pattern>/browser/*.action</pattern>
		<pattern>/login.action</pattern>
	</decorator>
	
	<decorator name="admin" page="admin.ftl" webapp="/admin/">
		<pattern>/admin/*.action</pattern>
	</decorator>

 -->
<!-- 
	<decorator name="panel" page="panel.jsp" />
	<decorator name="printable" page="printable.jsp" />
	
	<decorator name="printable" page="printable.jsp" role="customer"
	webapp="aaa" />
-->
<!-- 
	<decorator name="footer" page="headerfooter.jsp">
	<pattern>/index.jsp</pattern>
	</decorator>
-->
<!-- 
	其实,重要的工作就是制作装饰器页面本身(也就是包含结构和规则的页面),
	然后把他们描述到decorators.xml中。 
-->
<!-- 
	defaultdir: 包含装饰器页面的目录 
	page : 页面文件名 
	name : 别名 
	role : 角色,用于安全 
	webapp : 可以另外指定此文件存放目录 
	Patterns : 匹配的路径,可以用*,那些被访问的页面需要被装饰。
-->

 

struts.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN"
        "http://struts.apache.org/dtds/struts-2.1.dtd">
<constant name="struts.custom.i18n.resources" value="message"/>
 	<!-- 当struts配置文件修改后,系统是否重新加载配文件,默认是false -->
	<constant name="struts.configuration.xml.reload" value="true" />
<struts>
	<package name="default" extends="struts-default">
		<global-results>
			<result name="error" type="freemarker">/common/action_error.ftl</result>
		</global-results>
		
		<action name="login" class="com.iemis.base.action.LoginAction">
			<result name="input" type="freemarker">/common/login.ftl</result>
			<result name="success"  type="redirect">/index.jsp</result>
		</action>
	</package>
	
	<!-- browser -->
	<package name="browser" extends="struts-default" namespace="/browser">
		<global-results>
			<result name="error" type="freemarker">/common/action_error.ftl</result>
			<result name="input" type="freemarker">/common/action_blank.ftl</result>
			<result name="success" type="freemarker">/common/action_success.ftl</result>
		</global-results>
		
		<action name="home" class="com.iemis.base.action.BaseAction" method="complete">
			<result name="success" type="freemarker">home.ftl</result>
		</action>
	</package>
	<package name="browser_home" extends="struts-default" namespace="/browser/home">
		<global-results>
			<result name="error" type="freemarker">/common/action_error.ftl</result>
			<result name="input" type="freemarker">/common/action_blank.ftl</result>
			<result name="success" type="freemarker">/common/action_success.ftl</result>
		</global-results>
		
		<action name="show" class="com.iemis.base.action.BaseAction" method="show">
			<result name="success" type="freemarker">show.ftl</result>
		</action>
	</package>
	
</struts>

 

 

complete.ftl

<#assign page=JspTaglibs["/WEB-INF/tld/sitemesh-page.tld"]>
<#include "/common/util.ftl">
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title><@getText "page.title"/>--${title}</title>
		<link href="${base}/styles/util.css" type="text/css" rel="stylesheet" />
		<link href="${base}/styles/default.css" type="text/css" rel="stylesheet" />
		<link href="${base}/styles/complete.css" type="text/css" rel="stylesheet" />
		<link href="${base}/styles/coos/menu.css" type="text/css" rel="stylesheet" />
		<script type="text/javascript" src="${base}/scripts/coos.js"></script>
		<script type="text/javascript" src="${base}/scripts/coos.iemis.js"></script>
		<script type="text/javascript" src="${base}/scripts/coos.ui.menu.js"></script>
		<script type="text/javascript" src="${base}/scripts/coos.ui.dialogbox.js"></script>		
		${head}
    </head>
    <body>
		<div id="page">
			<#include "/common/head.ftl">
	        <#if actionErrors?size == 0>
	        	<div id="headNav">
			    </div>
			    <script type="text/javascript">
				coos.onloadEvent(function(){
					//创建一个对象
					var menu = new coosmenu("headNav");
					<#list baseMenus as menu>
				    	<#assign url><#t>
							<#if menu.url?exists>${base}${menu.url}<#else>#this</#if><#t>
						</#assign>
						<#assign isVisiable><#t>
						<#if menu.isVisiable==0>true<#else>false</#if><#t>
						</#assign>
						
						<#if !(menu.codeno?exists && ((!editor && menu.codeno=="userManage") || (!admin && menu.codeno=="systemconfig")))>
				    	menu.add(${menu.id},${menu.pid},'${menu.name}','${url}','${menu.title?default("null")}','${menu.target?default("null")}','${menu.isLast?default("null")}','${menu.icon?default("null")}','${menu.iconopen?default("null")}',${isVisiable});
				    	</#if>
				    </#list>
					});
					
				</script>
	        	${body}
	        <#else>
        	<div class="page_width">
        		${body}
        	</div>
        	</#if>
	        
	        <#include "/common/footer.ftl">
        </div>
    </body>
</html>

 

simple.ftl

<#assign page=JspTaglibs["/WEB-INF/tld/sitemesh-page.tld"]>
<#include "/common/util.ftl">
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>${title}</title>
		<link href="${base}/styles/util.css" type="text/css" rel="stylesheet" />
		<link href="${base}/styles/default.css" type="text/css" rel="stylesheet" />
		<link href="${base}/styles/simple.css" type="text/css" rel="stylesheet" />
		<script type="text/javascript" src="${base}/scripts/coos.js"></script>
		<script type="text/javascript" src="${base}/scripts/coos.iemis.js"></script>
		${head}
    </head>
    <body>
		<#if actionErrors?size == 0>
    	   <#include "/common/action_message.ftl">
    		${body}
        <#else>
    		${body}
    	</#if>
    </body>
</html>

 browser/home.ftl

<#include "/common/util.ftl">
<#assign title><@getText "page.title"/><@getText "page.home"/></#assign>
<html>
    <head>
        <title>${title}</title>
    </head>
    <body>
	<@box title "box_width">
			<@iframe "browser_home" "${base}/browser/home/show.action" /> 
	</@box>
    </body>
</html>

 

browser/home/show.ftl

<#include "/common/util.ftl">
<html>
    <head>
        <title><@getText "page.home"/></title>
    </head>
    <body>
    	<br /><br /><br /><br />
    	<div class="success">
    	${baseConfs["welcome"]?default("welcome")}
    	</div>
    </body>
</html>

 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:p="http://www.springframework.org/schema/p"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:jee="http://www.springframework.org/schema/jee"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="
			http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
			http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
			http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
			http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd
			http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
	
	<context:property-placeholder location="classpath:application.properties" />
	<import resource="classpath:application-dao.xml"/>
	<import resource="classpath:application-service.xml"/>
	<import resource="classpath:application-security.xml"/>
</beans>

 

注意:sitemesh-freemarker这个servlet要同时匹配action和ftl两个才可以,不能用/*匹配所有的,否则图片样式等也会给装饰。官方文档的访问后缀名就是.ftl不是action,但只配置了action ftl文件他就无法解析了,必须两个都配置了才行。如果你的模版不是以ftl为后缀的要改成对应的后缀,如.html .dec要改成对应的。

<servlet>
        <servlet-name>sitemesh-freemarker</servlet-name>
        <servlet-class>com.opensymphony.module.sitemesh.freemarker.FreemarkerDecoratorServlet</servlet-class>
        <init-param>
            <param-name>TemplatePath</param-name>
            <param-value>/</param-value>
        </init-param>
        <init-param>
            <param-name>default_encoding</param-name>
            <param-value>UTF-8</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>sitemesh-freemarker</servlet-name>
        <url-pattern>*.action</url-pattern>
    </servlet-mapping>
    
	<servlet-mapping>
        <servlet-name>sitemesh-freemarker</servlet-name>
        <url-pattern>*.ftl</url-pattern>
    </servlet-mapping>

 

struts和sitemesh的配置也要用新的方式,否则freemarker里面action变量不能使用。

<filter>
        <filter-name>struts-prepare</filter-name>
        <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareFilter</filter-class>
    </filter>
    
     <filter>
        <filter-name>sitemesh</filter-name>
        <filter-class>org.apache.struts2.sitemesh.FreeMarkerPageFilter</filter-class>
    </filter>
	
	<filter>
        <filter-name>struts-execute</filter-name>
        <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsExecuteFilter</filter-class>
    </filter>
<filter-mapping>
        <filter-name>struts-prepare</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-execute</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>

 

 

 

spring2.5和hibernate3.3.1的配置和以前版本一致,除了更新插件包不需要改动其他的东西。

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值