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 Test  :<input type="text"><br>
   <input type="button" value=" Test  "/><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  Represents a declaration for serialization is not being stored  

	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.
  • del.icio.us
  • StumbleUpon
  • Digg
  • TwitThis
  • Mixx
  • Technorati
  • Facebook
  • NewsVine
  • Reddit
  • Google
  • LinkedIn
  • YahooMyWeb

Related Posts of struts2 + json + jquery examples

  • In the servlet use Bean

    According to Sun's definition, JavaBean is a reusable software components. In fact JavaBean is a Java class, through the package into a property and methods of treatment of a function or a business object, referred to as bean. Because JavaBean is ...

  • hibernate generic generic DAO

    hibernate generic generic DAO

  • Servlet brief introduction

    Servlet brief introduction: Servlet is a small application server Are used to complete the B / S architecture, the client requests the response to treatment Platform independence, performance, able to run thread Servlet API for Servlet provides the s ...

  • First Hibernate Example

    Curd a simple example. Source does not contain the dependent libraries, or playing too much of the package. PO object Note: One must have the default constructor 2 non-final modified. Otherwise useless lazy loading. UserDAOImpl category code, and oth ...

  • Spring2.0 + hibernate3.1 + log4j + mysql demo

    applicationContext.xml Non-attachment jar package, necessary friends can send an email to todd.liangt @ gmail.com

  • Struts2 + hibernate + spring problem user log in

    dao layer services layer action jsp <tr> <td align="center"> <b> user name: </ b> </ td> <td> <s: textfield name = "czyNumber" cssClass = "textstyle" theme = "simple" size = &q

Leave a Reply

Recent
Recent Entries
Tag Cloud
Random Entries