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

阅读更多

一、导入所需包,包括Struts2所需的各jar包,再导入struts2-convention-plugin-2.1.jar和jsonplugin-0.34.jar

 

二、在Web.xml添加Struts2

 

web.xml

 

 


		struts2
		org.apache.struts2.dispatcher.FilterDispatcher
	
	
		struts2
		/*
	

 

 

三、修改struts.xml,配置struts2-convention-plugin。并修改convention-plugin默认的结果资源路径为webroot/

 

struts.xml

 



	  
    

 

 

四、编写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

 






Insert title here








 

六、测试

 

在地址栏输入  http://localhost:8080/ConventionTest/index.html

 

点击“点我-Default”按钮,调用action中的execute方法,弹出“This is the default method!”

 

点击“点我-Test”按钮,调用action中的test方法,弹出“Test method!!”

  • ConventionTest.rar (25.7 KB)
  • 下载次数: 336

你可能感兴趣的:(json,Struts,Apache,jQuery,JavaScript)