读书人

救命。SSH框架struts2默认theme成defa

发布时间: 2013-10-21 17:02:52 作者: rapoo

救命。。。SSH框架struts2默认theme成default了
各位,首先感谢前来帮助。。。 进入正题

环境:Myeclipse9.01 + jdk6.0 + tomcat6.0 + J2EE5 Library

说明:前几天对零配置SSH感兴趣,借鉴前人经验搭了个环境试验,都调通以后,结果出现一个很神奇的错误。我在jsp页面直接写<s:form></s:form>结果报错了。。。错误如下:
2012-3-29 18:08:41 org.apache.catalina.core.StandardWrapperValve invoke
严重: Servlet.service() for servlet jsp threw exception
java.io.FileNotFoundException: Template /template/default/form-close.ftl not found.
at freemarker.template.Configuration.getTemplate(Configuration.java:489)
at freemarker.template.Configuration.getTemplate(Configuration.java:452)
at org.apache.struts2.components.template.FreemarkerTemplateEngine.renderTemplate(FreemarkerTemplateEngine.java:96)
at org.apache.struts2.components.UIBean.mergeTemplate(UIBean.java:559)
at org.apache.struts2.components.UIBean.end(UIBean.java:513)
at org.apache.struts2.views.jsp.ComponentTagSupport.doEndTag(ComponentTagSupport.java:42)
at org.apache.jsp.index_jsp._jspx_meth_s_005fform_005f0(index_jsp.java:372)
at org.apache.jsp.index_jsp._jspService(index_jsp.java:264)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:386)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:88)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.struts2.dispatcher.ActionContextCleanUp.doFilter(ActionContextCleanUp.java:102)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:96)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.springframework.orm.hibernate3.support.OpenSessionInViewFilter.doFilterInternal(OpenSessionInViewFilter.java:198)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:859)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
at java.lang.Thread.run(Thread.java:619)
出错以后,就开始找原因,最终发现加上theme="simple"就一切正常了。我就纳闷了,struts2默认theme不是xhtml么,什么时候成default了? Template怎么去template/default下面找ftl文件呢?求真相...
配置上,struts只有一个xml文件,使用了struts-conversion-plugin-2.2.3.jar以及struts2-json-plugin-2.2.3.jar,其他的没有什么有struts配置的jar了,我的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>
<!-- =================================================== -->
<!-- ==================Parent package==================== -->
<!-- =================================================== -->
<package name="crud-default" extends="convention-default">
<interceptors>
<interceptor-stack name="crudStack">
<interceptor-ref name="store">
<param name="operationMode">AUTOMATIC</param>
</interceptor-ref>
<interceptor-ref name="paramsPrepareParamsStack" />
</interceptor-stack>
</interceptors>
<default-interceptor-ref name="crudStack" />
</package>
<!-- =================================================== -->
<!-- ================对象交给Spring管理================== -->
<!-- =================================================== -->


<constant name="struts.objectFactory" value="spring" />
<!-- =================================================== -->
<!-- =====================开发参数======================= -->
<!-- =================================================== -->
<!-- 开启使用开发模式,详细错误提示, 官方不建议使用
<constant name="struts.devMode" value="true" /> -->
<!-- 国际化资源重新加载
<constant name="struts.i18n.reload" value="true" /> -->
<!-- 配置文件重新加载
<constant name="struts.configuration.xml.reload" value="true" /> -->
<!-- convention类从新加载, 官方不建议使用
<constant name="struts.convention.classes.reload" value="false" /> -->
<!-- =================================================== -->
<!-- ======================全局配置====================== -->
<!-- =================================================== -->
<constant name="struts.ui.theme" value="simple" />
<!-- 设置local -->
<constant name="struts.locale" value="zh_CN"/>
<!-- 指定资源编码类型 -->
<constant name="struts.i18n.encoding" value="UTF-8" />
<!-- 默认后缀名 -->
<constant name="struts.action.extension" value="action,jhtml,," />
<!-- 国际化资源文件 -->
<constant name="struts.custom.i18n.resources"
value="resources/i18n/Common,
resources/i18n/System,
resources/i18n/Area,
resources/i18n/Air" />
<!-- =================================================== -->
<!-- =================conversion约定配置================= -->
<!-- =================================================== -->
<!-- 是否禁用扫描。一定要设为false,否则convention插件不起作用,零配置也没有意义。 -->
<constant name="struts.convention.action.disableScanning" value="false" />
<!--
设置Struts 2是否允许在Action名中使用斜线 。
如果开启action名称可以有“/”,一个请求地址有多个“/”,struts2就不会再使用类路径扫描的命名空间,只会使用配置的名称。
所以既想action名称里使用“/”,又想用struts2默认搜索的命名空间,只能自己修改一下convention插件的实现类了。
-->
<constant name="struts.enable.SlashesInActionNames" value="false" />
<!--
结果资源所在路径。
convention插件会自动在此路径中寻找文件。默认放到WEB-INF的目的的保护文件资源,只能通过程序内部跳转才能访问,
我们的权限拦截器或其他权限处理只要加到action上就可以了。
-->
<constant name="struts.convention.result.path" value="/" />
<!-- action后缀。比如为HelloWorldAction。按照配置,actionName为helloWorld。 -->
<constant name="struts.convention.action.suffix" value="Action" />
<!-- 名称首字母小写 -->
<constant name="struts.convention.action.name.lowercase" value="true" />
<!--
分隔符 一个action名字的获取。比如为HelloWorldAction。按照配置,actionName为helloWorld。
-->
<constant name="struts.convention.action.name.separator" value="" />
<!--
设置默认的父包,一般我们都设置一个default包继承自struts-default。大部分类再继承default。
如果有特殊的类需要特殊的包,只能在action中再指定父包了。
-->
<constant name="struts.convention.default.parent.package" value="crud-default" />

</struts>

我的配置文件中设置默认theme为simple显然也没有起到半分作用...

此外,<constant name="struts.convention.classes.reload" value="false" />我设置为true时,N多class文件报file not found异常,但这些class都在... 后来看官方说这个有问题,就不用了...

各位前辈,请不吝赐教~~ 这个神奇的名为default的theme哪儿来的呢?还是其他原因产生的?

十分感谢~~~!!!!!!
[解决办法]
摘至别人的一段,不知道有么有用:
------------------------------------------------
在项目中使用到Struts2的dojo时,出现java.io.FileNotFoundException: Template /template/ajax/head.ftl not found. 有时出现.../text.ftl not found. 错误。

分析一下:
从strtus2.1.x开始,Struts2团队将dojo从核心包里面提取出来,成为一个struts2的插件struts2-dojo-plugin-*.jar,因此在jsp页面上,若要使用dojo的话,必须引入如下标签库:<%@taglib prefix="ss" uri="/struts-dojo-tags"%/>。当然,若要使用Struts2的标签库,还需引入<%@taglib prefix="s" uri="/struts-tags"%/>标签库。

另外,Struts2的标签(无论是dojo或非dojo)均需要freeMarker模板文件*.ftl进行渲染和解释以生成Html页面,dojo或非dojo的标签库均配备一套自己的模板文件库,在jsp页面里,struts2根据各标签的theme来查找自己配置的相应模板文件*.ftl,若theme设置不对,则struts2就会从相应路径中找不到需要的ftl文件,就会报错../*.ftl not found.

当在jsp页面内,标签的主题(theme)若设置不正确,则会找不到相应的ftl文件。 比如下面代码:

<s:form method="post" validate="true" theme="xhtml">
<s:textfield label="Name" name="name" />
<s:password label="Password" name="password" />
<s:textfield label="Age" name="age" />
<ss:submit />
</s:form>

上述的theme定为xhtml,若是修改为theme="ajax",则在运行时jsp页面将不会被解析,提示.../text.ftl not found。 这是因为在struts2的核心包中没有template/ajax/text.ftl文件。



jsp文件中使用的标签是dojo的,就会根据theme的设置到与dojo配置的模板文件中去找,使用的标签为struts2的,就到与struts2配套的模板文件中去找。这样才能找得到自己需要的模板文件进行渲染。呵呵,经历了痛苦的探索之后,终于明白了个中原理。


[解决办法]
摘至别人的一段,不知道有么有用:
------------------------------------------------
在项目中使用到Struts2的dojo时,出现java.io.FileNotFoundException: Template /template/ajax/head.ftl not found. 有时出现.../text.ftl not found. 错误。

分析一下:
从strtus2.1.x开始,Struts2团队将dojo从核心包里面提取出来,成为一个struts2的插件struts2-dojo-plugin-*.jar,因此在jsp页面上,若要使用dojo的话,必须引入如下标签库:<%@taglib prefix="ss" uri="/struts-dojo-tags"%/>。当然,若要使用Struts2的标签库,还需引入<%@taglib prefix="s" uri="/struts-tags"%/>标签库。

另外,Struts2的标签(无论是dojo或非dojo)均需要freeMarker模板文件*.ftl进行渲染和解释以生成Html页面,dojo或非dojo的标签库均配备一套自己的模板文件库,在jsp页面里,struts2根据各标签的theme来查找自己配置的相应模板文件*.ftl,若theme设置不对,则struts2就会从相应路径中找不到需要的ftl文件,就会报错../*.ftl not found.

当在jsp页面内,标签的主题(theme)若设置不正确,则会找不到相应的ftl文件。 比如下面代码:

<s:form method="post" validate="true" theme="xhtml">
<s:textfield label="Name" name="name" />
<s:password label="Password" name="password" />
<s:textfield label="Age" name="age" />
<ss:submit />
</s:form>

上述的theme定为xhtml,若是修改为theme="ajax",则在运行时jsp页面将不会被解析,提示.../text.ftl not found。 这是因为在struts2的核心包中没有template/ajax/text.ftl文件。

jsp文件中使用的标签是dojo的,就会根据theme的设置到与dojo配置的模板文件中去找,使用的标签为struts2的,就到与struts2配套的模板文件中去找。这样才能找得到自己需要的模板文件进行渲染。呵呵,经历了痛苦的探索之后,终于明白了个中原理。

读书人网 >J2EE开发

热点推荐