Struts使用Json插件,两种配置方法

第一种:使用配置文件

1>我们新建一个配置文件struts-ajax.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
	"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
	"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
	<package name="ajax-json-plugin" extends="json-default">
		  <action name="employeeJson" class="com.kaishengit.action.AjaxAction">
			<result name="success" type="json">
				<param name="root">employeeList</param>
				<param name="noCache">true</param>
				<param name="enableGZIP">true</param>
				<param name="excludeNullProperties">true</param>
			</result>
		</action>  
	</package>
</struts>
 

2>我们将此配置文件导入主配置文件struts.xml中

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
	"-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN"
	"http://struts.apache.org/dtds/struts-2.1.7.dtd">
<struts>
	
	<include file="struts-ajax.xml"></include>
	<package name="myPackage" extends="struts-default">
	
	</package>
</struts>

 3>客户端index.jsp,我使用的是Jquery结合Json

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

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>json</title>
  </head>
  
  <body>
    
    
    <script type="text/javascript" src="js/jquery.js"></script>
    <script type="text/javascript">
    	$(document).ready(function(){
    		$.get("employeeJson.action",function(json){
    			alert(json);
    		});
    	});
    </script>
  </body>
</html>
 

4>服务端代码

package com.kaishengit.action;

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

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;

import com.kaishengit.service.EmployeeService;

public class AjaxAction {
	private EmployeeService employeeService;
	private List<Employee> employeeList;
	
	public String execute(){
		
		employeeList = employeeService.findAllByJson();
		
		return "success";
	}
	

	public List<Employee> getEmployeeList() {
		return employeeList;
	}
	public void setEmployeeList(List<Employee> employeeList) {
		this.employeeList = employeeList;
	}

	@Autowired
	public void setEmployeeService(EmployeeService employeeService) {
		this.employeeService = employeeService;
	}
	
}

这样就ok啦

第二种:使用annotation(注解)

1>我们新建一个配置文件struts-ajax.xml,把其中Action节点去除

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
	"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
	"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
	<package name="ajax-json-plugin" extends="json-default"/> 
</struts>
 

2>我们将此配置文件导入主配置文件struts.xml中(跟第一种一样)

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
	"-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN"
	"http://struts.apache.org/dtds/struts-2.1.7.dtd">
<struts>
	
	<include file="struts-ajax.xml"></include>
	<package name="myPackage" extends="struts-default">
	
	</package>
</struts>

 3>客户端index.jsp,我使用的是Jquery结合Json(跟第一种一样)

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

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>json</title>
  </head>
  
  <body>
    
    
    <script type="text/javascript" src="js/jquery.js"></script>
    <script type="text/javascript">
    	$(document).ready(function(){
    		$.get("employeeJson.action",function(json){
    			alert(json);
    		});
    	});
    </script>
  </body>
</html>
 

4>服务端代码,使用annotation(注解)

package com.kaishengit.action;

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

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;

import com.kaishengit.service.EmployeeService;

@ParentPackage(value="ajax-json-plugin")
public class AjaxAction {
	private EmployeeService employeeService;
	private List<Employee> employeeList;
	
	@Action(value="employeeJson",results={@Result(name="success",params={"root","employeeList","noCache","true","enableGZIP","true","excludeNullProperties","true"},type="json")})
	public String execute(){
		
		employeeList = employeeService.findAllByJson();
		
		return "success";
	}
	
	
	
	public List<Employee> getEmployeeList() {
		return employeeList;
	}
	public void setEmployeeList(List<Employee> employeeList) {
		this.employeeList = employeeList;
	}


	@Autowired
	public void setEmployeeService(EmployeeService employeeService) {
		this.employeeService = employeeService;
	}
	
	
}

  ok!

猜你喜欢

转载自meigesir.iteye.com/blog/1487099