Struts2使用struts2-convention-plugins

前几天接触了一个比较老旧的项目,使用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不是技术贴,只是提醒一下自己,很多技术在学的时候是固定死板的(实现方式可能比较单一),但经过扩展之后,功能可能会异常强大,而实现方式也会有很多种,即使用过好几年的技术,也可能在其他人的代码中,发现不一样的用法。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值