Convention plugin都有那些有约定

Convention plugin都有那些有约定?

从action到jsp以及包的名称,几乎都有约定。下面简单总结一下。

1、jsp默认的映射文件夹

如果你没有在struts.xml中指定struts.convention.result.path的值,所有的action将默认被映射到WEB-INF/content这个文件夹下面。例如你定义了一个login.action,哪么struts2将在WEB-INF/content下面寻找login.jsp文件。

2、action默认的url和命名空间

如果你没有配置struts的struts.convention.package.locators属性,哪么strusts2将搜索包名中包含struts, struts2, action or actions等关键字类,并且类实现com.opensymphony.xwork2.Action或者名称以 Action 结尾的类。

引用struts的文档。这些类。

com.example.actions.MainAction

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

com.example.struts.company.details.ShowCompanyDetailsAction

将被映射成struts的命名空间(蓝色字体为命名空间)

com.example.actions.MainAction -> /

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

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

相应的Action对应的URL(蓝色字体为URL)

com.example.actions.MainAction -> /main

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

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

3、Action返回结果的映射

Convention Plugin会在系统启动的时候把Action的映射都关联起来。

引用struts的一个样例。

package com.example.actions;

import com.opensymphony.xwork2.ActionSupport;

public class HelloWorld extends ActionSupport {

private String message;

public String getMessage() {

    return message;

}

public String execute() {

    if (System.currentTimeMillis() % 2 == 0) {

      message = "It's 0";

     return "zero";

    }

    message = "It's 1";

    return SUCCESS;

}

}

如果当前的action对应的页面为hello.jsp那么当返回为“zero”时,系统将去寻找hello-zero.jsp。

详情请参考http://struts.apache.org/2.1.6/docs/convention-plugin.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值