`
joeyhacker
  • 浏览: 94557 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

初探Convention Plugin

阅读更多

昨天struts2发布了2.1.6的GA版本,  其中有个Convention Plugin的插件号称要代替code behind . 关于Convention Plugin的文章目前很少, 下面我来说说它的简单用法, 请各位高手批评指正.

 

首先导入必要的jar包, struts2-convention-plugin-2.1.6 是必须的.  其他的大家都知道.

然写配置文件, struts2最新版本的例子程序中好像使用了新的过滤器. 就是那个ng包的.

web.xml

 

<?xml version="1.0" encoding="UTF-8"?>

<web-app id="starter" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

 <filter>

<filter-name>action2</filter-name>

<filter-class>

org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter

</filter-class>

 </filter>

 

<filter-mapping>

 

<filter-name>action2</filter-name>

<url-pattern>/*</url-pattern>

</filter-mapping>

 

<welcome-file-list>

 <welcome-file>index.jsp</welcome-file>

</welcome-file-list>

 

</web-app>


 

 

 

struts.xml文件可以不写, 因为action采用annotation的形式配置, 用如你的程序中使用REST则必须写struts.xml

<constant name="struts.convention.action.suffix" value="Controller"/>

<constant name="struts.convention.action.mapAllMatches" value="true"/>

<constant name="struts.convention.default.parent.package" value="rest-default"/>


 

下面我来写一个简单的action类演示一下.

 

package com.example.actions;

/**
 *
 * @author joey
 */
import com.opensymphony.xwork2.ActionSupport;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;

@Results({
    @Result(name = "failure", location = "fail.jsp")
})
public class HelloWorld extends ActionSupport {

    @Action(value = "/url",
    results = {@Result(name = "success", location = "http://struts.apache.org", type = "redirect")})
            
    public String url1() {
        return SUCCESS;
    }

    @Action(value = "/url2",
    results = {@Result(name = "success", location = "http://www.baidu.com", type = "redirect")})

    public String url2() {
        return SUCCESS;
    }


    public String index(){
        System.out.println("index is running;");
        return SUCCESS;
    }
}

 

 

*index() 是必须的方法, 不写会报错.  好像execute()方法一样, 是个入口函数.

 

index.jsp

 

<%-- 
    Document   : index
    Created on : 2009-1-15, 12:24:10
    Author     : joey
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   "http://www.w3.org/TR/html4/loose.dtd">

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
       测试:<a href="url">1</a> , <a href="url2">2</a>
     </body>
</html>

 

 

 

 

 

fail.jsp 这个文件默认位置在/WEB-INF/content/  , 这个路径是可以修改的,  在配置文件中: <constant name="struts.convention.result.path" value="你的路径"/>

 

好了, 基本就是这样了. 

5
1
分享到:
评论
4 楼 wangjian3q 2009-02-27  
这样不好呀!
   那struts2.0 的核心拦截器如何用呢??????
3 楼 Arden 2009-01-21  
晕死是跟struts2-spring-plugin-2.1.6.jar这个包放在一块就会出问题。
2 楼 Arden 2009-01-21  
这个东西不能用,一加这个包struts2-convention-plugin-2.1.6.jar就报错,tomcat就启动不起来。
1 楼 Acaleph 2009-01-19  
这是codebehind plugin返回类型的注释写法,在convention plugin中的type = ServletActionRedirectResult.class用什么来替代了?
@Result(name = "save", location = "role!doList.action", type = ServletActionRedirectResult.class)

相关推荐

Global site tag (gtag.js) - Google Analytics