昨天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="你的路径"/>
好了, 基本就是这样了.
分享到:
相关推荐
从struts2.1版本开始,Convention Plugin作为替换替换Codebehind Plugin来实现Struts2的零配置。 • 包命名习惯来指定Action位置 • 命名习惯制定结果(支持JSP,FreeMarker等)路径 • 类名到URL的约定转换 • 包名...
struts2-convention-plugin-2.3.32
convention-plugin 插件使用说明
struts2-convention-plugin-2.3.24.1
struts2-convention-plugin-2.3.15.1.jar
struts2 convention plugin(插件)测试项目
struts2-convention-plugin-2.1.6.jar
struts2-convention-plugin-2.1.8.jar
Struts开始使用convention-plugin代替codebehind-plugin来实现struts的零配置,使用Convention插件,你需要此JAR文件
struts2采用convention-plugin实现零配置,希望大家用的到!
struts2-convention-plugin-2.3.24.jar
struts2-convention-plugin-2.3.1.2.jar
struts2-convention-plugin-2.3.1.jar,使用注解的方式代替xml配置action,必须要引用这个包。
struts2-convention-plugin-2.3.4.1.jar
struts2-convention-plugin-2.3.15.3.jar struts2配置Action注解要用到的包,真正实现零配置
从struts2.1开始,struts2不再推荐使用Codebehind作为零配置插件,而是改为使用Convention插件来支持零配置,和Codebehind相比,Convention插件更彻底,该插件完全抛弃配置信息,不仅不需要是使用struts.xml文件进行...
中文convention,struts2注解,struts注解,注解实现
从struts2.1版本开始,Convention Plugin作为替换替换Codebehind Plugin来实现Struts2的零配置。 • 包命名习惯来指定Action位置 • 命名习惯制定结果(支持JSP,FreeMarker等)路径 • 类名到URL的约定转换 • 包名...