您的位置:首页 > 编程语言 > Java开发

struts-convention零配置--约定大于配置

2014-04-06 10:56 316 查看
如果之前没有用struts2 的convention,在开发中你会发现有大量的action需要去配置,而且每个action又有多个result需要配置。

用了struts的convention后将极大的减少这些操作。开发速度大大加快。

1.所需jar包 struts2-convention-plugin-2.2.1.1.jar

还要与之相当的xwork-core-2.1.6.jar的jar包,如果xwork版本较低时1.x.x会出现l类似异常

[java]
view plaincopyprint?

严重: Exception starting filter struts2 
java.lang.NoSuchMethodError: com.opensymphony.xwork2.util.finder.ClassFinder.<init>(Lcom/opensymphony/xwork2/util/finder/ClassLoaderInterface;Ljava/util/Collection;ZLjava/util/Set;Lcom/opensymphony/xwork2/util/finder/Test;)V 

严重: Exception starting filter struts2
java.lang.NoSuchMethodError: com.opensymphony.xwork2.util.finder.ClassFinder.<init>(Lcom/opensymphony/xwork2/util/finder/ClassLoaderInterface;Ljava/util/Collection;ZLjava/util/Set;Lcom/opensymphony/xwork2/util/finder/Test;)V
所以最好将xwork的jar包升级到2.x的

2.struts.xml中的一些常量配置

    2.1默认所有result对应的结果页面都放在WEB-INF/content目录下,你可以通过设置struts.convention.result.path这个属性的值来改变到其他路径。

    例如:

[java]
view plaincopyprint?

<constant name="struts.convention.result.path" value="/WEB-INF/page" /> 

<constant name="struts.convention.result.path" value="/WEB-INF/page" />


    2.2默认路径包含action,actions,struts,struts2的所有包都会被struts作为含有Action类的路径来搜索。你可以通过设置struts.convention.package.locators属性修改这个设置。

    例如:

[java]
view plaincopyprint?

<constant name="struts.convention.package.locators" value="web,action" /> 

<constant name="struts.convention.package.locators" value="web,action" />
则定义了在项目中,包路径包含web和action的将被视为Action存在的路径来进行搜索。
3.寻找Action类的规则,从前一步找到的package以及其子package中寻找(直接或间接)实现了com.opensymphony.xwork2.Action接口的实现类以及以Action结尾的类

例如:  

[java]
view plaincopyprint?

com.example.actions.MainAction 
   com.example.actions.products.Display (implements com.opensymphony.xwork2.Action) 
   com.example.struts.company.details.ShowCompanyDetailsAction 

com.example.actions.MainAction
com.example.actions.products.Display (implements com.opensymphony.xwork2.Action)
com.example.struts.company.details.ShowCompanyDetailsAction


4.命名空间。从定义的.package.locators标示开始到包结束的部分,就是命名空间。举个例子:

    com.ustb.web.user.userAction的命名空间是:”/user”。

    com.ustb.web.user.detail.UserAction的命名空间是:”/user/detail”

5.convention通过如下规则确定URL的具体资源部分:去掉类名的的尾部Action部分,然后将将各部分首字母转换小写。(我发现这完全取决于申请提交的action格式)

如果有返回的result值,这与返回的result值之间用‘-’分割开,这个符号可以设置struts.convention.action.name.separator如

[java]
view plaincopyprint?

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

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


例如:UserAction->user  UserDetailAction ->userDetail或者user-detail。

当申请的action为userDetail.action是对应的url页面就是userDetail.jsp

当申请的action为user-detail.action是对应的url页面就是user-detail.jsp
我发现这完全取决于申请提交的action格式

结合上面的。  

如果有result例如list 则 user-list.jsp 和 userDetail-list.jsp或user-detail-list.jsp

对于com.ustb.web.user.detail.UserDetailAction,映射的url路径就是/WEB-INF/content/user/detail/userDetail.jsp

或者/WEB-INF/content/user/detail/user-detail.jsp

如果有返回值result首先映射/WEB-INF/content/user/detail/userDetail-list.jsp,或者/WEB-INF/content/user/detail/user-detail-list.jsp,

如果未找到,其次映射/WEB-INF/content/user/detail/userDetail.jsp,/WEB-INF/content/user/detail/user-detail.jsp,
你可以自己去试一下

通过以上配置基本就可以搞定大部分的要求了,但是为了满足实际要求更灵活的配置。

struts-convention还提供了注解去满足实际中存在的变态要求。(世界)

1.@Action

[java]
view plaincopyprint?

package com.example.web; 
  
import com.opensymphony.xwork2.Action; 
import com.opensymphony.xwork2.ActionSupport; 
  
public class HelloActionextends ActionSupport { 

    @Action("action1") 
    public String method1() { 

        return SUCCESS; 
    } 
  
    @Action("/user/action2") 
    public String method2() { 
        return SUCCESS; 



package com.example.web;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionSupport;

public class HelloAction extends ActionSupport {
@Action("action1")
public String method1() {
return SUCCESS;
}

@Action("/user/action2")
public String method2() {
return SUCCESS;
}
}


方法名    默认调用路径    默认映射路径

method1    /hello!method1.action .    /WEB-INF/content/hello.jsp

method2    /hello!method2.action.    /WEB-INF/content/hello.jsp

通过@Action注释后

方法名    @Action注释后调用路径    @Action注释 后映射路径

method1    /action1!method1.action.    /WEB-INF/content/action1.jsp

method1    /user/action2!method2.action    /WEB-INF/content/user/action2.jsp

2.@Actions

[java]
view plaincopyprint?

package com.example.web; 
  
import com.opensymphony.xwork2.ActionSupport; 
import org.apache.struts2.convention.annotation.Action; 
import org.apache.struts2.convention.annotation.Actions; 
  
public class HelloActionextends ActionSupport { 

  @Actions({ 
    @Action("/different/url"), 
    @Action("/another/url") 
  }) 
  public String method1() { 

    return “error”; 

  } 

package com.example.web;

import com.opensymphony.xwork2.ActionSupport;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Actions;

public class HelloAction extends ActionSupport {
@Actions({
@Action("/different/url"),
@Action("/another/url")
})
public String method1() {
return “error”;
}


我们可以通过:/different/url!method1.action 或 /another/url!method1.action 来调用method1 方法。

对应的映射路径分别是

/WEB-INF/content/different/url-error.jsp;

/WEB-INF/content/another/url-error.jsp

可能误导了大家,一个方法被@Action注释后,只是多了一种调用方式,而不是说覆盖了原来的调用方式。

两种方式均可对method1方法进行调用,唯一的区别就是,两种调用的映射是不一样的,所以,想跳转到不同的界面,这是一个非常好的选择。

这相当于在调用方法的时候就已经指定好了返回的路径了。

3.@Namespace

[java]
view plaincopyprint?

package com.example.web; 
  
import com.opensymphony.xwork2.ActionSupport; 
import org.apache.struts2.convention.annotation.Action; 
import org.apache.struts2.convention.annotation.Actions; 
@Namespace("/other") 
public class HelloWorldextends ActionSupport { 

  
  public String method1() { 
    return “error”; 
  } 
    @Action("url") 
  public String method2() { 
return “error”; 
  } 
  
    @Action("/different/url") 
  public String method3() { 

return “error”; 

  } 


package com.example.web;

import com.opensymphony.xwork2.ActionSupport;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Actions;
@Namespace("/other")
public class HelloWorld extends ActionSupport {

public String method1() {
return “error”;
}
@Action("url")
public String method2() {
return “error”;
}

@Action("/different/url")
public String method3() {
return “error”;
}
}


通过 /other/hello-world!method1.action 访问method1 方法。

通过 /other/url!method2.action 访问method2 方法

通过 /different /url!method3.action 访问method3 方法

与@Action 注释不同的是,该注释覆盖了默认的namespace(这里是’/’),此时再用hello!method1.action 已经不能访问method1 了.

注意:这里是覆盖而不是并存。

4.@Results和@Result

1 全局的(global)。

全局results可以被action类中所有的action分享,这种results在action类上使用注解进行声明。

[java]
view plaincopyprint?

package com.example.actions; 
  
import com.opensymphony.xwork2.ActionSupport; 
import org.apache.struts2.convention.annotation.Action; 
import org.apache.struts2.convention.annotation.Actions; 
import org.apache.struts2.convention.annotation.Result; 
import org.apache.struts2.convention.annotation.Results; 
  
@Results({ 
  @Result(name="failure", location="/WEB-INF/fail.jsp") 
}) 
public class HelloWorldextends ActionSupport { 

  public String method1() { 
    return “failure”; 
  } 
    @Action("/different/url") 
  public String method2() { 
    return “failure”; 
  } 
  


package com.example.actions;

import com.opensymphony.xwork2.ActionSupport;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Actions;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;

@Results({
@Result(name="failure", location="/WEB-INF/fail.jsp")
})
public class HelloWorld extends ActionSupport {
public String method1() {
return “failure”;
}
@Action("/different/url")
public String method2() {
return “failure”;
}

}


当我们访问 /hello -world !method1.action 时,返回 /WEB-INF/fail.jsp

当我们访问 /hello -world !method2.action 时,返回 /WEB-INF/fail.jsp

当我们访问 /different/url!method2.action 时,返回 /WEB-INF/fail.jsp

2 本地的(local)。

本地results只能在action方法上进行声明。

Java代码

[java]
view plaincopyprint?

package com.example.actions; 
  
import com.opensymphony.xwork2.ActionSupport; 
import org.apache.struts2.convention.annotation.Action; 
import org.apache.struts2.convention.annotation.Actions; 
import org.apache.convention.annotation.Result; 
import org.apache.convention.annotation.Results; 
  
public class HelloWorldextends ActionSupport { 

    @Action(value="/other/bar",results={@Result(name ="error", location =
"www.baidu.com",type="redirect")}) 
  public String method1() { 
    return “error”; 
  } 


package com.example.actions;

import com.opensymphony.xwork2.ActionSupport;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Actions;
import org.apache.convention.annotation.Result;
import org.apache.convention.annotation.Results;

public class HelloWorld extends ActionSupport {
@Action(value="/other/bar",results={@Result(name = "error", location = "www.baidu.com",type="redirect")})
public String method1() {
return “error”;
}
}


当我们调用 /hello -world !method1.action 时,返回 /WEB-INF/content/hello-error.jsp

当我们调用 /other/bar!method1.action 时,返回 www.baidu.com
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  struts2 namespace