初探Convention Plugin

本文介绍Struts2最新发布的2.1.6版本中ConventionPlugin插件的基本使用方法。该插件通过注解配置Action,简化了开发流程。文章详细展示了如何设置必要的Jar包及web.xml配置,并通过一个简单的示例演示了如何定义Action及其结果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

昨天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="你的路径"/>

 

好了, 基本就是这样了. 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值