`
fish119
  • 浏览: 146350 次
  • 性别: Icon_minigender_1
  • 来自: 沈阳
社区版块
存档分类
最新评论

struts2.1.6利用convention-plugin和json-plugin实现json零配置

    博客分类:
  • Java
阅读更多

一、导入所需包,包括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!!”

4
0
分享到:
评论
2 楼 xwq18 2011-07-27  
非常感谢!
1 楼 niukai_2000 2010-10-30  
没成功,怎么弄?

相关推荐

    struts2-convention-plugin-2.1.6.jar

    struts2-convention-plugin-2.1.6.jar

    struts2-convention-plugin-2.1.6.jar 和struts2-core-2.1.6.jar

    不论高低版本,要使用struts2-core这个jar包,当又需struts2-convention-plugin.jar时勿必要使两者版本一致哦,否则会有DefError、Unable to read class诸等错误

    struts2.1.6+spring2.0+hibernate3.2常用配置包

    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...

    struts2版本 2.1.6 必须的jar包 和 web.xml 文件的修改

    NULL 博文链接:https://jackroomage.iteye.com/blog/888807

    struts2的rest风格插件实例

    将 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 格式的数据,...

    struts 2.1.8 jar 完整开发

    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

    Struts2-rest插件(有注释)

    从 Struts 2.1 开始,Struts 2 改为使用 Convention 插件来支持零配置。Convention 插件彻底地抛弃了配置信息,不仅不需要使用 struts.xml 文件进行配置,甚至不需要使用 Annotation 进行配置。而是由 Struts 2 根据...

    javaweb开发包大全

    比较全的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

    ssh框架所需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注解使用说明文档

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

    struts2注解详细说明

    从struts2.1版本开始,Convention Plugin作为替换替换Codebehind Plugin来实现Struts2的零配置。• 包命名习惯来指定Action位置• 命名习惯制定结果(支持JSP,FreeMarker等)路径• 类名到URL的约定转换• 包名...

Global site tag (gtag.js) - Google Analytics