Introduction to Struts2+ajax+json Integration __js

Source: Internet
Author: User

First, prepare jar bag, do not underestimate this step, the beginning of everything difficult. Summed up there are the following three kinds of collocation.

1. Xwork-core-2.1.6.jar and Struts2-json-plugin-2.1.8.jar. If you want to use Struts2-json-plugin-2.1.8.jar this support method, your Xwork-core-*.jar cannot choose 2.2.1 and the above version, because xwork-core-*. There are no Org.apache.commons.lang packages in the 2.2.1 and above versions of the jar. When you start Tomcat, it will appear: Java.lang.NoClassDefFoundError:org.apache.commons.lang.xwork.StringUtils.

2. Xwork-2.1.2.jar and Jsonplugin-0.34.jar. If you want to use Jsonplugin-0.34.jar this support way, that needs to switch your Xwork-core-*.jar to Xwork-2.1.2.jar. Because Jsonplugin-0.34.jar needs com.opensymphony.xwork2.util.TextUtils.

Support for this class. Xwork-core-*.jar's 2.2.1 version is found in the class and not in Xwork-core-2.1.6.jar.


3. struts2.3.7 version, suitable for Struts2-json-plugin-2.3.4.1.jar and Json-lib-2.3-jdk15.jar


Two, below see what the action is.

Public String Editproblem () {
		question= (question) this.getsession (). Get ("Question_info");
		This.pid=string.valueof (Question.getid ());
		Question.setcontent (this.newcontent);
		Question.setmodifydatetime (This.getcurrentdate ());
		Showproblemslogic.updateproblem (question)//Update database record return		
		SUCCESS
	}

Very simple, is a way to edit the problem, the author is doing an interactive question and answer platform, users can also edit their own questions after the question.
Above is the action code that implements this feature. Notice that there are a lot of attributes in this action that Struts.xml can write.

<action name= "Editproblem" class= "problemsaction" method= "Editproblem" >
          	<result name= "Input" >/ problems.jsp</result>
          	<result type= "json" >
          		<param name= "Includeproperties" >newcontent </param> 
          	</result>          	       
  

Includeproperties the configuration of this parameter corresponds to the data in the success callback function in the following AJAX submission code, and if data requires more content, you can add more properties to the Includeproperties parameter. The configuration of this parameter is critical, the configuration is wrong, success callback function will not be executed, so be sure to pay attention to.


Third, look at the Ajax function is how to write it?

<script>
$ (document). Ready (function () {   
	$ ("#edit_content_button"). Click (function () {     	   
		var params = $ ("#edit_form"). Serialize ()//To specify that the parameters in the form are converted to query strings
		//Using $.ajax ({}) in jquery; Ajax Method
		$.ajax ({
			URL: "Editproblem",//This is the corresponding action name
			type: "POST",
			data:params,
			dataType: " JSON ",
			success:function (data) {	Ajax request to submit a successful callback function						
				$ (" #qcontent "). CSS (" display "," none ");
				$ ("#newqContent"). empty ();
				$ ("#newqContent"). Append (data.newcontent);				
				$ (' #edit_form '). Toggle ();
				Alert ("modified successfully.") ");
			},
			error:function (data) {
				alert (arguments[1])
;});};}); </script>

In this way, the integration even if it took shape, can be implemented in STRUTS2, the page does not refresh the form submitted ~



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.