Struts2 + jquery + JSON for AJAX requests __JS

Source: Internet
Author: User
Tags java web

The project wants to use Ajax, so in the online grilled n More information, made a lot of errors, from today's work to now has been dealing with this problem, and finally to solve it.

When I see the AJAX display of the page, I am excited about the exception, in order to record their learning Ajax process, but also in order to make more people less detours, close-up of this article to remember.

Nonsense not to say, in order to better understand, I redo a small project to deepen the impression. Let's start our Ajax tour with this little project now.

First step: Create a Java Web project called Ajax.

Step Two: Add the Struts2 jar pack, which requires four packages Freemarker.jar Ognl.jar Struts2-core.jar Commons-fileupload.jar Commons-io.jar -2.1.6.jar (This package plus version number, because it is mentioned below), these six packages are struts must rely on the jar package, what to say.

Step three: Modify the Web.xml join struts filter, the code is as follows:

<?xml version= "1.0" encoding= "UTF-8"?> <web-app version= "2.5" xmlns= "Http://java.sun.com/xml/ns/javaee" Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" xsi:schemalocation= "Http://java.sun.com/xml/ns/javaee http ://java.sun.com/xml/ns/javaee/web-app_2_5.xsd "> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class> </filter> < filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </ filter-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </ Welcome-file-list> </web-app>

Fourth step: Add the JSON package, here need two: Json-lib.jar Jsonplugin.jar here to pay attention to a very important point, because JSON a large number of references to the Apache Commons package, so here to join, the need for a total of 4 Commons package,  In addition to the Commons package, you need to introduce a ezmorph package, so this step involves the introduction of 7 packages, listed below: Commons-collections.jar Commons-lang.jar Commons-beanutils.jar Commons-logging.jar Ezmorph.jar plus two packets of JSON with a total of seven, one-time join.

Fifth step: Write the background processing ajaxloginaction.action, the contents are as follows:

Package qy.test.action; Import Java.util.HashMap; Import Java.util.Map; Import Net.sf.json.JSONObject; Import Com.opensymphony.xwork2.ActionSupport; public class Ajaxloginaction extends Actionsupport {//user Ajax returns data private String result;//Struts Property-driven mode, automatically populate page properties to here PR Ivate String LoginName; private String password; @Override public String Execute () {//Use a map as an example map<string, string> map = new hashmap<string, string> (); Map to add a piece of data, record the page passed LoginName map.put ("name", This.loginname); The map object that will be returned is JSON-processed jsonobject JO = jsonobject.fromobject (map); The ToString method that invokes the JSON object converts to a string and then assigns a value to result This.result = Jo.tostring (); Can test result System.out.println (This.result); return SUCCESS; }//getter setter Method omitted}

Sixth step: Write the front desk index.jsp, attention to join jquery JS file contents are as follows:

<%@ page language= "java" contenttype= "text/html; Charset=utf-8 "pageencoding=" UTF-8 "%> <! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" >

Step Seventh: Add Struts.xml to the SRC directory and configure the corresponding XML file to provide data for the AJAX request. The code is as follows:

<?xml version= "1.0" encoding= "UTF-8"?> <! DOCTYPE struts Public "-//apache Software foundation//dtd struts Configuration 2.0//en" "http://struts.apache.org/dtds/ Struts-2.0.dtd "> <struts> <package name=" ajax "extends=" Json-default "> <action name=" Ajaxlogin " class= "Qy.test.action.AjaxLoginAction" > <!--return type is json defined in Sjon-default--> <result type= "JSON" > <! --The value of the root corresponds to the property of the value to be returned--> <!--The result value here is the result in the corresponding action--> <param name= "root" >result</param> & lt;/result> </action> </package> </struts>

Eighth step: If the fourth step does not join the Commons-logging.jar package, please remember to join

Nineth Step: Publish Run. Unfortunately, you will find a mistake,

Java.lang.ClassNotFoundException:com.opensymphony.xwork2.util.TextUtils:

This is a version of struts error because there is no textutils class in the Xwork2.1.6-core.jar, where the Xwork2.1.2-core.jar also added to the Classpath, Xwork2.1.2-core.jar contains this thing, we use the jar of xwork2.1.6, when you want to use Textutils, go to Xwork2.1.2-core.jar to find.

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.