一、导入所需包,包括Struts2所需的各jar包,再导入struts2-convention-plugin-2.1.jar和jsonplugin-0.34.jar
二、在Web.xml添加Struts2
web.xml
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
三、修改struts.xml,配置struts2-convention-plugin。并修改convention-plugin默认的结果资源路径为webroot/
struts.xml
<struts>
<!-- 结果资源所在路径 -->
<constant name="struts.convention.result.path" value="/"/>
</struts>
四、编写Action代码
com.fish.action.json.JsonTestAction
package com.fish.action.json;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import com.opensymphony.xwork2.ActionSupport;
@ParentPackage("json-default")
@Result(type="json",name="test")
public class JsonTestAction extends ActionSupport {
private static final long serialVersionUID = 4242612202520616657L;
private String name = "fish119";
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Action(value="test",results={@Result(type="json",name="test")})
public String test() throws Exception{
this.name += ": Test method!!";
return "test";
}
@Action(results={@Result(type="json",name="success")})
public String execute() throws Exception{
this.name +=": This is the default method!";
return SUCCESS;
}
}
五、编写页面文件
index.html
<!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">
<script type="text/javascript" src="js/jquery.js"></script>
<title>Insert title here</title>
<script type="text/javascript">
function alt() {
$.getJSON("json/json-test.action", function(json){alert(json.name);});
};
function altTest() {
$.getJSON("json/json-test!test.action", function(json){alert(json.name);});
};
</script>
</head>
<body>
<button id="a" onclick="alt();">点我-Default</button>
<button id="b" onclick="altTest();">点我-Test</button>
</body>
</html>
六、测试
在地址栏输入 http://localhost:8080/ConventionTest/index.html
点击“点我-Default”按钮,调用action中的execute方法,弹出“This is the default method!”
点击“点我-Test”按钮,调用action中的test方法,弹出“Test method!!”
分享到:
相关推荐
struts2-convention-plugin-2.1.6.jar
不论高低版本,要使用struts2-core这个jar包,当又需struts2-convention-plugin.jar时勿必要使两者版本一致哦,否则会有DefError、Unable to read class诸等错误
struts2-convention-plugin-2.1.6.jar //2* struts2-spring-plugin-2.1.6.jar //1 xwork-2.1.2.jar //2 xerces-2.6.2.jar //3 xml-apis.jar //3 注:以下包为数据源连接池 相关 用到那个添加相应的包 c3p0-0.9.1.2...
NULL 博文链接:https://jackroomage.iteye.com/blog/888807
将 Struts 2 项目下 struts2-convention-plugin-2.1.6.jar、struts2-rest-plugin-2.1.6.jar 两个 JAR 包复制到 Web 应用的 WEB-INF/lib 路径下。 由于 Struts 2 的 REST 插件还需要将提供 XML、JSON 格式的数据,...
commons-fileupload-1.2.1 ...struts2-json-plugin-2.1.8.1 struts2-embeddedjsp-plugin-2.1.8.1 struts2-core-2.1.8.1 struts2-convention-plugin-2.1.8.1 xwork-core-2.1.6 struts2-spring-plugin-2.1.8.jar
从 Struts 2.1 开始,Struts 2 改为使用 Convention 插件来支持零配置。Convention 插件彻底地抛弃了配置信息,不仅不需要使用 struts.xml 文件进行配置,甚至不需要使用 Annotation 进行配置。而是由 Struts 2 根据...
比较全的java开发包,要用到的朋友可以下载 ant.jar antlr-2.7.2.jar antlr-2.7.6.jar aopalliance-1.0.jar avalon-framework-4.2.0.jar ...struts2-json-plugin-2.1.8.1.jar xwork-core-2.1.6.jar
1.1.jar,junit-4.5.jar,mysql-connector-java-3.1.13-bin.jar,ognl-2.6.11.jar,slf4j-api-1.5.8.jar,slf4j-nop-1.5.8.jar,spring.jar,struts2-convention-plugin-2.1.6.jar,struts2-core-2.1.6.jar,struts2-spring-...
从struts2.1开始,struts2不再推荐使用Codebehind作为零配置插件,而是改为使用Convention插件来支持零配置,和Codebehind相比,Convention插件更彻底,该插件完全抛弃配置信息,不仅不需要是使用struts.xml文件进行...
从struts2.1版本开始,Convention Plugin作为替换替换Codebehind Plugin来实现Struts2的零配置。• 包命名习惯来指定Action位置• 命名习惯制定结果(支持JSP,FreeMarker等)路径• 类名到URL的约定转换• 包名...