Struts2零配置

一、简介

Struts2零配置的使用方式体现了“约定优于配置”的开发思想,通过遵循一定的约定规则或使用一些专门的注解,省略XML文档中的配置信息达到简化开发的目的。

二、HelloWorld

我们首先通过一个简单的例子感受一下Struts2零配置的工作方式。

1.要求:不使用struts.xml配置文件访问Action类,并转向到一个结果页面。

2.做法:

①加入JAR包:struts2-convention-plugin-2.3.15.3.jar

②在index.jsp页面中添加超链接

<a href= "${pageContext.request.contextPath }/hello-world">HelloWorld</ a>

③创建/WEB-INF/content/hello-world.jsp

<h1> HelloWorld</h1 >

3.分析

以上操作相当于如下配置

<action name ="hello-world"

        class= "com.opensymphony.xwork2.ActionSupport"

        method= "execute">

     <result name= "success">/WEB-INF/content/hello-world.jsp </result >

</action >

在Struts2零配置工作模式下,结果资源默认保存在/WEB-INF/content目录下,文件名和URL地址一致。

三、创建Action

①包的要求

Convention插件会在名为struts、struts2、action或actions的包下查找Action类。参照如下配置

<constant name="struts.convention.package.locators"

value= "action,actions,struts,struts2"/>

说明:以上配置可以在如下文件中找到

struts2-convention-plugin-2.3.15.3.jar/struts-plugin.xml

例如:

com.atguigu.struts

com.atguigu.user.struts2

com.atguigu.book.action.show

com.atguigu.order.actions

②Action类的要求

实现com.opensymphony.xwork2.Action接口或类名以Action结尾。参照如下配置

<constant name= "struts.convention.action.suffix"

value="Action"/>

例如:

com.atguigu.actions.MainAction

com.atguigu.actions.products.Display (implements com.opensymphony.xwork2.Action)

com.atguigu.struts.company.details.ShowCompanyDetailsAction

 

四、访问Action

①名称空间:完整包名中关键包到类名之间的部分。上例中的Action类对应的名称空间是

com.atguigu.actions.MainAction -> /

com.atguigu.actions.products.Display -> /products

com.atguigu.struts.company.details.ShowCompanyDetailsAction -> /company/details

②URL地址:类名去掉Action后缀,将剩下的部分按照驼峰式命名规则拆分成独立的单词,并全部转为小写,再以“-”连接。所以访问上例中的Action类的完整URL地址是

com.atguigu.actions.MainAction -> /main

com.atguigu.actions.products.Display -> /products/display

com.atguigu.struts.company.details.ShowCompanyDetailsAction -> /company/details/show-company-details

参考以下配置

<constant name= "struts.convention.action.name.separator" value="-"/>

 

五、Result

①JSP文件默认保存的位置/WEB-INF/content目录,参考以下配置

<constant name= "struts.convention.result.path"

 value="/WEB-INF/content/"/>

②Action方法返回的字符串是success,则JSP文件的命名规则是:Action简单类名去掉Action后缀,将剩下的部分按照驼峰式命名规则拆分成独立的单词,并全部转为小写,再以“-”连接。则上例中的Action类对应的结果JSP是

com.atguigu.actions.MainAction ->/WEB-INF/content/main.jsp

com.atguigu.actions.products.Display ->/WEB-INF/content/display.jsp

com.atguigu.struts.company.details.ShowCompanyDetailsAction->/WEB-INF/content/show-company-details.jsp

③其实结果资源并非必须是JSP,URL地址和结果资源的对应关系参见下表

URL

Result

File that could match

Result Type

/hello

success

/WEB-INF/content/hello.jsp

Dispatcher

/hello

success

/WEB-INF/content/hello-success.htm

Dispatcher

/hello

success

/WEB-INF/content/hello.ftl

FreeMarker

/hello-world

input

/WEB-INF/content/hello-world-input.vm

Velocity

/test1/test2/hello

error

/WEB-INF/content/test/test2/hello-error.html

Dispatcher

④如果Action方法返回的不是success,而是其他值,则目标资源的命名规则是:xxx-resultCode.扩展名,例如:

Action类名

返回值

结果资源名

MainAction

save

main-save.jsp

ShowCompanyDetailsAction

zero

show-company-details-zero .jsp

 

 

 


本教程由尚硅谷教育大数据研究院出品,如需转载请注明来源。
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值