Long time no update, recently because the team other things delay did not continue to study, but the heart is very uneasy, take time to get their weak points to consolidate again!
Asynchronous refresh itself is very useful, SSH framework for me, there is no difficulty, but the combination of AJAX processing some additions and deletions to check the page, feel helpless, or the foundation to grasp the first
Let's look at this feature first:
1. Jar Package Used
: Http://pan.baidu.com/s/1hspDeoW
2, index.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" >
3. Back-end action
Package Action;import Java.util.hashmap;import Java.util.map;import javax.servlet.http.httpservletrequest;import Net.sf.json.jsonobject;import Org.apache.struts2.interceptor.servletrequestaware;import Org.apache.struts2.json.jsonutil;import Sun.org.mozilla.javascript.internal.json.jsonparser;import Com.opensymphony.xwork2.actionsupport;public class Jsonaction extends Actionsupport implements servletrequestaware{ Private static final Long Serialversionuid = 1l;private httpservletrequest request;private string Result;public string get Result () {return result;} public void Setresult (String result) {This.result = result;} @Overridepublic void Setservletrequest (HttpServletRequest arg0) {this.request = arg0;} /** * Processing Ajax request */public string Executeajax () {try {//Get data string name = Request.getparameter ("name"); int age = Integer.parse Int (Request.getparameter ("Age")); String position = Request.getparameter ("position"); System.out.println ("name==" +name+ "age==" +age+ "position==" +position);//store data in map and convert to JSon type data, you can also manually construct JSON type data map<string,object> map = new hashmap<string, object> (); Map.put ("name", name); Map.put ("Age", age); Map.put ("position", position); Jsonobject JSON =jsonobject.fromobject (map);//Convert Map object to JSON type data result = Json.tostring ();//Assign value to result, Passed to page System.out.println ("result===" +result);} catch (Exception e) {e.printstacktrace ();} return SUCCESS;}}
4. Struts.xml Configuration
<?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.i18n.encoding "value=" UTF-8 "></constant>< Package Name= "simpleton" extends= "Struts-default,json-default" ><action name= "jsonaction" method= "ExecuteAjax "Class=" action. Jsonaction "><!--return JSON type data--><result type=" JSON "><param name=" root ">result</param> <!--result is the variable name set in the action and the data that the page needs to return, which must have a setter and getter method--></result></action></package ></struts>
Summary: In the process I encountered the problem of missing jar package, about the version of the Lang package, I put 2, no conflict, meet the requirements.
Java.lang.noclassdeffounderror:org/apache/commons/lang3/stringutils
Struts+ajax+jquery: Implementing asynchronous new data