前几天接触了一个比较老旧的项目,使用spring + struts2做的。发现里面有些代码看不懂了:
1.struts2配置文件中没有进行action的映射,而在路径访问的时候却可以去除xxxaction.do中的action变为xxx.do
2.有些根本没有匹配到的访问路径 竟然可以直接访问jsp或html页面:如 /controller/user/show.do 根本没有showAction而直接访问到了webContent下的/controller/user/show.jsp
3.方法调用后返回字符串默认忽略了".jsp",并且直接映射到了namespace下的资源路径中:如 return "list" ,如果当前action的namespace("/user/show"),则会加载/webContent/user/show/list.jsp
还有一些就不一一列举了。
由于struts2用的不是很多,看了一下引入的jar包,记得spring整合struts2必须要有struts2-spring-plugins才行,发现这个项目并没有,而是一个简单的struts2-core和
struts2-convention-plugins,然后上网上搜索了一下,才明白原来上面的那些实现均是来自于struts2-convention-plugins这个包。
先贴上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.devMode" value="true" />
<!-- 配置struts2的请求后缀 -->
<constant name="struts.action.extension" value="do" />
<!-- 配置页面返回的路径 这里/代表是WebContent的根路径 -->
<constant name="struts.convention.result.path" value="/"/>
<!-- 配置struts2管理action包路径,注意此处配置的包是一个文件名,即类似com.yyy和com/yyy都不可以,直接配置yyy即可 -->
<constant name="struts.convention.package.locators" value="yyy"/>
<constant name="struts.convention.default.parent.package" value="struts-default"/>
<package name="struts-default2" abstract="true">
<result-types>
<result-type name="chain" class="com.opensymphony.xwork2.ActionChainResult"/>
<result-type name="dispatcher" class="org.apache.struts2.dispatcher.ServletDispatcherResult" default="true"/>
<result-type name="freemarker" class="org.apache.struts2.views.freemarker.FreemarkerResult"/>
<result-type name="httpheader" class="org.apache.struts2.dispatcher.HttpHeaderResult"/>
<result-type name="redirect" class="org.apache.struts2.dispatcher.ServletRedirectResult"/>
<result-type name="redirectAction" class="org.apache.struts2.dispatcher.ServletActionRedirectResult"/>
<result-type name="stream" class="org.apache.struts2.dispatcher.StreamResult"/>
<result-type name="velocity" class="org.apache.struts2.dispatcher.VelocityResult"/>
<result-type name="xslt" class="org.apache.struts2.views.xslt.XSLTResult"/>
<result-type name="plainText" class="org.apache.struts2.dispatcher.PlainTextResult" />
</result-types>
<default-class-ref class="com.opensymphony.xwork2.ActionSupport" />
</package>
</struts>
这个插件基本上可以实现struts2的零配置。
这篇blog不是技术贴,只是提醒一下自己,很多技术在学的时候是固定死板的(实现方式可能比较单一),但经过扩展之后,功能可能会异常强大,而实现方式也会有很多种,即使用过好几年的技术,也可能在其他人的代码中,发现不一样的用法。