struts2.18整合json和ExtJs4.0实例

用到的jar包有:

commons-beanutils-1.7.0.jar、commons-collections-3.2.jar、commons-fileupload-1.2.1.jar、commons-io-1.3.2.jar、commons-lang-2.3.jar、commons-logging-1.0.4.jar、freemarker-2.3.15.jar、json-lib-2.1.jar、ognl-2.7.3.jar、struts2-core-2.1.8.1.jar、struts2-json-plugin-2.1.8.1.jar、xwork-core-2.1.6.jar

这些包在struts2.1.8的lib文件夹中都有,还需要加上一个ezmorph-1.0.4.jar,本实例中没有用到jsonplugin-0.33.jar包。

一、首先写一个javabean:Person.java

 

package com.leo.bean;

public class Person {
	private String name;
	private int age;
	private String sex;
	private String birthday;

	public Person(String name, int age, String sex, String birthday) {
		super();
		this.name = name;
		this.age = age;
		this.sex = sex;
		this.birthday = birthday;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

	public String getSex() {
		return sex;
	}

	public void setSex(String sex) {
		this.sex = sex;
	}

	public String getBirthday() {
		return birthday;
	}

	public void setBirthday(String birthday) {
		this.birthday = birthday;
	}

}

 

 二、写Action:ExtjsAction.java

 

package com.leo.action;

import java.util.ArrayList;
import java.util.List;

import com.leo.bean.Person;
import com.opensymphony.xwork2.ActionSupport;

public class ExtjsAction extends ActionSupport {
	private long results;
	private List items;

	public long getResults() {
		return results;
	}

	public void setResults(long results) {
		this.results = results;
	}

	public List getItems() {
		return items;
	}

	public void setItems(List items) {
		this.items = items;
	}

	public String execute() throws Exception {
		this.results = 3;
		Person p1 = new Person("张三", 29, "男", "1990-10-22");
		Person p2 = new Person("李四", 28, "男", "1991-03-30");
		Person p3 = new Person("王五", 27, "女", "1993-08-17");
		this.items = new ArrayList();
		this.items.add(p1);
		this.items.add(p2);
		this.items.add(p3);
		return SUCCESS;
	}
}

 

 三、写配置文件

1.web.xml

 



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

 

 2.struts.xml

 




	
	
		
			
		
	
    

 

 四、最后写jsp

index.jsp

 

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>


	

		ExtJs与Struts2结合
		
		
		
		
		
		
		
		
		
	
	
	

 

 

五、效果图

你可能感兴趣的:(ext4.0)