`

struts2 零配置

 
阅读更多

        零配置并不是没有配置,而是通过约定大于配置的方式,大量通过约定来调度页面的跳转而使得配置大大减少。所以,首先应该了解下convention-plugin的约定:
1. 默认所有的结果页面都存储在WEB-INF/content下,你可以通过设置struts.convention.result.path这个属性的值来改变到其他路径。如:

 

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

 则将路径配置到了WEB-INF/page 下,意味着action返回的时候将以/WEB-INF/page为基础路径。

 

 

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

 

 

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

 则定义了在项目中,包路径包含web和action的将被视为Action存在的路径来进行搜索。 

 

Com.framework.web.*/Com.framework.action.*都将被视为含有Action的包路径而被搜索。

 

3. 接着,Convention从前一步找到的package以及其子package中寻找 com.opensymphony.xwork2.Action 的实现以及以Action结尾的类:

 

 

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

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

 

Com.framework.web.user.userAction的命名空间是:”/user”。Com.framework.web.user.detail.UserAction的命名空间是:”/user/detail”

5. Convention通过如下规则确定URL的具体资源部分:去掉类名的Action部分。然后将将每个分部的首字母转为小写,用’-’分割,你可以设置struts.convention.action.name.separator 如

 

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

 还是举个例子:

 

UserAction->user  UserDetailAction ->user-detail。结合上面的。对于com.ustb.web.user.detail.UserDetailAction,映射的url就是/WEB-INF/content/user/detail/user-detail.jsp

6. struts支持.jsp .html .htm .vm格式的文件。
下面是action和结果模版的映射关系:

 

 

下面是actiong和结果模版的映射关系:

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

 

以上的内容参考struts2的文档http://struts.apache.org/2.1.6/docs/convention-plugin.html

 

当然,简单的通过默认的方式来进行配置不能完全满足实际项目的需要。所幸,convention的零配置是非常灵活的,可以用注解形式做配置,添加注解后注解和默认形式都是可以访问的。

 

 

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

 

 

通过@Actions注释

 

 

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注释后,只是多了一种调用方式,而不是说覆盖了原来的调用方式。比如对于如下例子:

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 {
  @Action("/another/url")
  public String method1() {
    return “error”;
  }

 

 

我们调用method1方法可以通过两种方式:
 /hello!method1.action 映射 url:/WEB-INF/content/hello-error.jsp 
2
 /another/url!method1.action 映射 url:/WEB-INF/content/another/url-error.jsp 
可见,两种方式均可对method1方法进行调用,唯一的区别就是,两种调用的映射是不一样的,所以,想跳转到不同的界面,这是一个非常好的选择。

 

通过@Namespace 注释

 

 

  1. package com.example.web;  
  2.   
  3. import com.opensymphony.xwork2.ActionSupport;   
  4. import org.apache.struts2.convention.annotation.Action;  
  5. import org.apache.struts2.convention.annotation.Actions;  
  6. @Namespace("/other")  
  7. public class HelloWorld extends ActionSupport {  
  8.   
  9.   public String method1() {  
  10.     return “error”;  
  11.   }  
  12.     @Action("url")  
  13.   public String method2() {  
  14. return “error”;  
  15.   }  
  16.   
  17.     @Action("/different/url")  
  18.   public String method3() {  
  19. return “error”;  
  20.   }  
  21. }  

 

通过 /other/hello-world!method1.action 访问method1 方法。
通过
 /other/url!method2.action 访问method2 方法
通过
 /different /url!method3.action 访问method3 方法 
与@Action 注释不同的是,该注释覆盖了默认的namespace(这里是’/’),此时再用hello!method1.action 已经不能访问method1 了.

 

@Results和@Result 
1 全局的(global)。 
全局results可以被action类中所有的action分享,这种results在action类上使用注解进行声明。

 

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方法上进行声明。

 

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;

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零配置

    Struts2零配置是Struts2框架的一种简化配置方式,旨在减少XML配置文件的使用,提高开发效率。在传统的Struts2应用中,开发者需要在多个XML文件中配置Action、结果页面、拦截器等,而“零配置”则是对这种繁琐配置的...

    struts2零配置

    "Struts2零配置"是Struts2框架中的一个重要特性,旨在提高开发效率,减少繁琐的XML配置工作。这个特性允许开发者在不编写大量XML配置文件的情况下,依然能实现应用的正常运行。 在传统的Struts2应用中,开发者需要...

    struts2零配置个人整理文档

    ### Struts2零配置个人整理文档 #### 一、Convention插件详解 ##### 1. 设置结果页面路径 - **背景介绍**:Struts2框架提供了便捷的方式来处理Web请求,并返回响应视图。为了简化配置过程,Struts2提供了一个名为...

    Struts2零配置+FreeMarker用户管理系统(UMS)

    这个名为"Struts2零配置+FreeMarker用户管理系统(UMS)"的项目,旨在通过注解的方式展示如何在不编写XML配置文件的情况下,利用Struts2和FreeMarker创建一个用户管理系统。 首先,我们来了解一下Struts2框架。Struts...

    struts2零配置入门代码

    在“struts2零配置入门代码”这个主题中,我们将深入探讨如何在不编写大量XML配置文件的情况下,启动并运行一个基本的Struts2应用程序。 Struts2的核心在于它的Action类,它是业务逻辑处理的主要组件。在“零配置”...

    struts2 零配置所需的包及示例

    "零配置"是指在Struts2中可以通过不写XML配置文件,而是利用注解或者Java配置来实现应用程序的配置。这种方式使得代码更加简洁,也更易于维护。下面我们将深入探讨Struts2的零配置实现及其所需的包和示例。 首先,...

    struts2零配置[总结].pdf

    在早期,Struts2主要依赖XML配置文件来定义和管理应用程序的行为,但随着技术的进步和对简洁配置的需求增加,Struts2引入了零配置的概念,以减少繁琐的XML配置。 "Struts2零配置"指的是尽量减少或消除传统Struts2...

    MyFramework - struts2 零配置:convention

    "MyFramework - Struts2 零配置:convention" 主题着重于介绍如何使用Struts2的Convention插件实现“零配置”开发,极大地简化了传统XML配置的繁琐过程。在传统的Struts2应用中,开发者需要为每个Action类和结果页面...

    struts2 spring4 hibernate4 Struts2零配置,json基本架包

    struts2 spring4 hibernate4 Struts2零配置基本架包 struts2-json-plugin-2.5.5.jar struts2-convention-plugin-2.5.5.jar

    简述STRUTS2_Convention零配置

    STRUTS2 Convention 零配置是Struts2框架中的一种高级特性,旨在简化应用程序的配置,让开发者能够更快地构建MVC应用。从Struts2.1版本开始,推荐使用Convention插件替代Codebehind插件,因为它更加自动化,几乎无需...

    struts2零配置convention-plugin

    从struts2.1开始,struts2不再推荐使用Codebehind作为零配置插件,而是改为使用Convention插件来支持零配置,和Codebehind相比,Convention插件更彻底,该插件完全抛弃配置信息,不仅不需要是使用struts.xml文件进行...

    STRUTS2零配置.doc

    Struts2是一款流行的Java web开发框架,其零配置特性始于版本2.1,主要通过引入Convention插件实现。Convention插件旨在简化应用配置,避免过度依赖XML或注解,通过预定义的规则自动配置Action、结果视图以及Action...

    struts2零配置[收集].pdf

    Struts2是一个流行的Java web开发框架,...总的来说,Struts2 LightURL插件是Struts2框架向更简洁配置方式迈出的一步,它整合了类似插件的优点,减少了开发者在XML配置上的工作量,使Struts2应用的构建更加灵活和高效。

    S2SH全注解的增删改查(struts2零配置)

    网上也很少关于全注解配置的例子,这里做了一个关于Struts2.2.3+Spring 3.0.6 + Hibernate3.6的一个很简陋增删改查,以供参考,主要是一个全注释的例子和对新的SSH2的整合。附带sql和jar包

Global site tag (gtag.js) - Google Analytics