CodeWeblog.com » import java,java util,utf 8 » struts2 + json + jquery examples

struts2 + json + jquery examples

struts2.1.X version of ajax theme has moved to dojo-plugin plug-in, usage has changed, according to online reviews of the ajax theme struts2 efficiency a bit low, do not recommend the use of the Internet now follow in the example in struts2 json-plugin plug-ins use a ajax.

input.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'input.jsp' starting page</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
 <script type="text/javascript" src="js/jquery.js"></script>

  </head>
  
  <body>
  
  <script type="text/javascript">
function test1(){

	var check=$("#check").val();
	var time=new Date();
	$.ajax({
		cache:false,
		url:'input.action',
		type:'post',
		dataType:'json',
		data:{check:check,t:time},
		success:update_page
	});
}
function update_page(json){
	var str="name="+json.name+",password="+json.pass+",relation="+json.relation;
	$("#test").html(str);
}
</script>
   ajax测试:<input type="text"><br>
   <input type="button" value="测试"/><br>
   <div></div>
  </body>
</html>


inputAction.java

In getXxx () method before the @ JSON (name = "XXX") Note You can rename the name of json transmission

package input;

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

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class InputAction extends ActionSupport {


	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private String name;
	private String pass;
	private List<String> relation;
	private transient String check;//transient 表示声明序列化的时候不被存储的

	public String getCheck() {
		return check;
	}
	public void setCheck(String check) {
		this.check = check;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getPass() {
		return pass;
	}
	public void setPass(String pass) {
		this.pass = pass;
	}
	
	public List<String> getRelation() {
		return relation;
	}
	public void setRelation(List<String> relation) {
		this.relation = relation;
	}
	
	public String execute() throws Exception {
		if("xing".equals(check)){
			name="xing";
			pass="password";
			relation=new ArrayList<String>();
			relation.add("one");
			relation.add("two");
			relation.add("three");
		}
		return SUCCESS;
	}
	
}


struts.xml

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN"
    "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
	<constant name="struts.custom.i18n.resourse" value="globalMessages"></constant>
	<constant name="struts.i18n.encoding" value="utf-8"></constant>
	
	<package name="input" extends="json-default">
	  <action name="input">
	    <result type="json"></result>
	  </action>
	</package>


</struts>


Json-plugin official Web site: http://cwiki.apache.org/S2PLUGINS/json-plugin.html

Json data format can also refer to other articles of this blog.
Digg Technorati StumbleUpon Mixx del.icio.us Reddit BlinkList Furl YahooMyWeb feedburner

Tags: import java (RSS), java util (RSS), utf 8 (RSS), script type (RSS), plug ins (RSS), arraylist (RSS), keyword3 (RSS), type button (RSS), input type text (RSS), language java (RSS), link rel (RSS), page language (RSS), css href (RSS), control content (RSS), string path (RSS), base href (RSS), starting page (RSS), relation test (RSS), time success (RSS), url input (RSS)

Permalink: http://www.codeweblog.com/struts2-json-jquery-examples/

1 comments to “struts2 + json + jquery examples”

  1. jed on 2009-09-29 03:50:22 :
    In your example theres no id for #check and #test. Cant understand how it works.

Leave a reply