1.Struts2强大,但也有很多需要解决的问题。就拿我现在这Blog这个FCKEditor来说吧,解决Struts2或Webwork的方案一直没有终止过。我试图修改FCKEditor Java版本的源代码,想改成 extends ActionSuppot, 但发现就算实现了,也代价非常大(可能FCKEdtitor的配置不能按官方来了),最后弄来弄个,选了个折中的办法。
Struts2使用不了FCKEditor的原因是因为Struts2的Filter,如果修改一下Struts2在Web.xml里的注册。
xml 代码
-
- <filter>
- <filter-name>struts2filter-name>
- <filter-class>
- org.apache.struts2.dispatcher.FilterDispatcher
- filter-class>
- filter>
-
-
- <filter-mapping>
- <filter-name>struts2filter-name>
- <url-pattern>/*url-pattern>
- filter-mapping>
- -->
-
- <filter-mapping>
- <filter-name>struts2filter-name>
- <url-pattern>*.actionurl-pattern>
- filter-mapping>
-
- <filter-mapping>
- <filter-name>struts2filter-name>
- <url-pattern>*.manageurl-pattern>
- filter-mapping>
注意这个时候action,manage的后缀要与struts.properties一致,不然会报错。
配置完后,所有用到过Struts2标签的页面都要通过Action转发过来,这样就不会报Struts2标签错误了。这对喜欢把JSP放在WEB-INF下的Web开发人员来说,不是什么问题。。
2.关于关闭Struts2自动验证的烦人英文问题。
应该有不少使用Struts2的人都是从Struts1过来的,但Struts2和Struts1是完全不同的,所以在配置Struts2的时间,难免有点畏缩,喜欢用它的默认配置。比如在自己建立的Struts.xml里,大家一般都这么写:
xml 代码
-
- "">
- <struts>
-
- <include file="struts-default.xml" />
- <include file="struts_xml/welcome.xml" />
- <include file="struts_xml/employee.xml" />
- <include file="struts_xml/department.xml" />
- <include file="struts_xml/nunEnterprise.xml" />
- <include file="struts_xml/enterprise.xml" />
- <include file="struts_xml/foreignEnterprise.xml" />
- <include file="struts_xml/exhibition.xml" />
- <include file="struts_xml/training.xml" />
- <include file="struts_xml/serviceEnterprise.xml" />
- <include file="struts_xml/mailController.xml" />
- <include file="struts_xml/enterpriseSearchController.xml" />
- <include file="struts_xml/foreignEnterpriseSearchController.xml" />
- <include file="struts_xml/serviceEnterpriseSearchController.xml" />
- <include file="struts_xml/nunEnterpriseSearchController.xml" />
- <include file="struts_xml/jChartController.xml" />
-
-
- <package name="default" extends="struts-default">
- <global-results>
- <result name="Exception">/exception.jspresult>
- <result name="SQLException">/exception.jspresult>
- global-results>
-
-
- <global-exception-mappings>
- <exception-mapping exception="java.sql.SQLException"
- result="SQLException" />
- <exception-mapping exception="java.lang.Exception"
- result="Exception" />
- global-exception-mappings>
- package>
-
- struts>
里面有一个叫“struts-default.xml”, 这是Struts2默认的重要配置文件。而且interceptor默认的配置为:
xml 代码
- <interceptor-stack name="defaultStack">
- <interceptor-ref name="exception"/>
- <interceptor-ref name="alias"/>
- <interceptor-ref name="servletConfig"/>
- <interceptor-ref name="prepare"/>
- <interceptor-ref name="i18n"/>
- <interceptor-ref name="chain"/>
- <interceptor-ref name="debugging"/>
- <interceptor-ref name="profiling"/>
- <interceptor-ref name="scopedModelDriven"/>
- <interceptor-ref name="modelDriven"/>
- <interceptor-ref name="fileUpload"/>
- <interceptor-ref name="checkbox"/>
- <interceptor-ref name="staticParams"/>
- <interceptor-ref name="params">
- <param name="excludeParams">dojo\..*param>
- interceptor-ref>
- <interceptor-ref name="conversionError"/>
- <interceptor-ref name="validation">
- <param name="excludeMethods">input,back,cancel,browseparam>
- interceptor-ref>
- <interceptor-ref name="workflow">
- <param name="excludeMethods">input,back,cancel,browseparam>
- interceptor-ref>
- interceptor-stack>
是不是什么interceptor都给你加载了。如果你这个时候使用了Validation.xml,那么一旦出现类型转换错误的失误,就会出现一些烦的英文,解决办法是:
xml 代码
-
- "">
- <struts>
-
- <package name="employee" extends="default">
-
- <interceptors>
- <interceptor-stack name="modefied">
- <interceptor-ref name="exception" />
- <interceptor-ref name="alias" />
- <interceptor-ref name="servlet-config" />
- <interceptor-ref name="prepare" />
- <interceptor-ref name="i18n" />
- <interceptor-ref name="chain" />
- <interceptor-ref name="debugging" />
- <interceptor-ref name="profiling" />
- <interceptor-ref name="scoped-model-driven" />
- <interceptor-ref name="model-driven" />
- <interceptor-ref name="fileUpload" />
- <interceptor-ref name="checkbox" />
- <interceptor-ref name="static-params" />
- <interceptor-ref name="params" />
-
- <interceptor-ref name="validation">
- <param name="excludeMethods">
- input,back,cancel,browse
- param>
- interceptor-ref>
- <interceptor-ref name="workflow">
- <param name="excludeMethods">
- input,back,cancel,browse
- param>
- interceptor-ref>
- interceptor-stack>
- interceptors>
- <default-interceptor-ref name="modefied" />
-
-
-
-
-
- <action name="employeeLogin"
- class="cn.gov.tdb.controller.EmployeeLoginController"
- method="login">
- <result name="success">welcome.jsp?active=1result>
- <result name="input">login.jspresult>
- action>
-
- <action name="employeeLogout"
- class="cn.gov.tdb.controller.EmployeeLoginController"
- method="logout">
- <result name="success">index.jspresult>
- <result name="input">index.jspresult>
- action>
-
-
-
-
-
- <action name="employeeCRUD"
- class="cn.gov.tdb.controller.EmployeeController"
- method="saveEmployee">
- <result name="success">welcome.jsp?active=1result>
- <result name="input">employeeForm.jspresult>
- action>
-
- <action name="listEmployees"
- class="cn.gov.tdb.controller.EmployeeController"
- method="listEmployees">
- <result name="success">listEmployees.jspresult>
- <result name="input">listEmployees.jspresult>
- action>
-
-
- package>
- struts>
自己重定义一个interceptor, 把
xml 代码
- <interceptor-ref name="conversionError"/>
删除就可以了。这样,烦人的英文就解决了
3.Struts2需要注意的一个小地方
今天在运行Struts2时,无意间发现程序在写Struts.xml有这样的问题:
以下是本人一个Action映射的配置文件
xml 代码
- <action name="saveExhibitionAppTable"
- class="cn.gov.tdb.controller.EnterpriseController"
- method="saveExhibitionAppTable">
- <result name="input">exhibitionAppTable.jspresult>
- <result name="success" type="redirect-action">
- generateBarcode.tdbdb?getTichetSource=${getTichetSource}&companyId=${companyId}&serviceId=${serviceId}
- result>
- action>
没有什么特别的地方就是在
xml 代码
- <result name="success" type="redirect-action">
- generateBarcode.tdbdb?getTichetSource=${getTichetSource}&companyId=${companyId}&serviceId=${serviceId}
- result>
这里返回的是一个redirect-action, ${getTicketSource}等这三个参数是这个Action的三个属性(有get,set方法的),我想把它们传给generateBarcode.tdbdb, 却发现一直取不到这三个传过来的参数,空指针报错.
然后我又把配置文件改成如下,弄成generateBarcode.jsp发现值确实已经传过来了,看来Struts2在redirec-action的时候已经处理过了.
xml 代码
- <result name="success">
- generateBarcode.jsp?getTichetSource=${getTichetSource}&companyId=${companyId}&serviceId=${serviceId}
- result>
查了查Strut2的文档,发现有以下配置,也就是说这个时候把要传递的参数如果分开来的话,通过redirec-action也是可以正常传过来的.
xml 代码
- <action name="saveExhibitionAppTable"
- class="cn.gov.tdb.controller.EnterpriseController"
- method="saveExhibitionAppTable">
- <result name="input">exhibitionAppTable.jspresult>
- <result name="success" type="redirect-action">
- <param name="actionName">generateBarcodeparam>
- <param name="namespace">/param>
- <param name="getTichetSource">${getTichetSource}param>
- <param name="companyId">${companyId}param>
- <param name="serviceId">${serviceId}param>
- result>
- action>