Jquery Ajax returns JSON data in another example (do not inherit JSON-default)

Source: Internet
Author: User
Tags date1 dateformat tld

 

package org.lsy.test.ajax.action;import java.io.PrintWriter;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.apache.struts2.ServletActionContext;import org.lsy.test.ajax.service.AjaxService;import org.lsy.test.ajax.service.AjaxServiceImpl;import org.rd.framework.common.container.ContainerManager;import org.rd.framework.struts.action.BodyListAction;import org.rd.framework.struts.action.CommonAction;import org.rd.framework.util.ContextUtil;import com.opensymphony.xwork2.ActionContext;import com.opensymphony.xwork2.ActionSupport;public class CompareDateAction  extends CommonAction {//private AjaxService ajaxService = (AjaxService)ContainerManager.getComponent(AjaxService.AJAX_BEAN_ID);private AjaxService ajaxService = new AjaxServiceImpl();private String result; public String execute() throws Exception{return SUCCESS;}public String isRightDate() throws Exception{//ContextUtil.put("dd","df", ContextUtil.SCOPE_SESSION);ActionContext ctx = ActionContext.getContext();HttpServletRequest req=(HttpServletRequest)ctx.get(ServletActionContext.HTTP_REQUEST);HttpServletResponse resp=(HttpServletResponse)ctx.get(ServletActionContext.HTTP_RESPONSE);resp.setContentType("text/xml");req.setCharacterEncoding("UTF-8");resp.setCharacterEncoding("UTF-8");PrintWriter pw = resp.getWriter(); String datefrom = ""; String dateto = ""; if(req.getParameter("datefrom")!=null){datefrom=req.getParameter("datefrom").toString();}if(req.getParameter("dateto")!=null){dateto=req.getParameter("dateto").toString();}//"[{'jj':'jjaa'}]"if(datefrom.trim().equals("")||dateto.trim().equals("")){pw.println("Y");this.result="{'re':'Y'}";}else if(ajaxService.isRightDate(datefrom, dateto)){pw.println("Y");//this.result="Y";this.result="{'re':'Y'}";}else{pw.println("N");//this.result="N";this.result="{'re':'N'}";}return NONE;}public String getResult() {return result;}public void setResult(String result) {this.result = result;}}

 

 

<?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="ajaxutil" extends="struts-default" namespace="/ajaxutil"><!-- ajaxutil begin --><action name="isrightdate" class="org.lsy.test.ajax.action.CompareDateAction" method="isRightDate">     </action><!-- ajaxutil end --></package></struts>

 

<% @ Page contenttype = "text/html; charset = UTF-8 "%> <% @ taglib uri ="/WEB-INF/crm_taglist.tld "prefix =" CRM "%> <% @ taglib uri ="/WEB-INF/component. TLD "prefix =" CX "%> <% @ taglib uri ="/Struts-tags "prefix =" S "%> <% @ page import =" Java. util. *, org. lsy. test. work. model. work "%> <% string Path = request. getcontextpath (); string basepath = request. getscheme () + ": //" + request. getservername () + ":" + request. getserverport () + req Uest. getcontextpath () + "/"; %> <input type = "hidden" id = "basepath" value = "<% = basepath %>"/> <SCRIPT src = "<% = basepath %> Work/jquery-1.7.1.min.js "> </SCRIPT> <% -- %> <! -- Data start --> <SCRIPT src = "<% = basepath %> Work/UI/jquery. UI. core. JS "> </SCRIPT> <SCRIPT src =" <% = basepath %> Work/UI/jquery. UI. widget. JS "> </SCRIPT> <SCRIPT src =" <% = basepath %> Work/UI/jquery. UI. datepicker. JS "> </SCRIPT> <LINK rel =" stylesheet "href =" <% = basepath %> Work/themes/base/jquery.ui.all.css "> <LINK rel =" stylesheet "href = "<% = basepath %> Work/demos/demos.css"> <! -- Data end --> <! -- Tabs start --> <LINK rel = "stylesheet" href = "<% = basepath %> Work/themes/base/jquery.ui.all.css"> <SCRIPT src = "<% = basepath %> Work/UI/jquery. UI. tabs. JS "> </SCRIPT> <! -- Tabs end --> <SCRIPT >$ (function () {$ ("# reviewdate "). datepicker ({showon: "button", buttonimage: "<% = basepath %> Work/demos/images/calendar.gif", buttonimageonly: true }); $ ("# reviewdate "). datepicker ("option", "dateformat", 'yy-mm-dd');}); $ (function () {$ ("# reviewdatefrom "). datepicker ({showon: "button", buttonimage: "<% = basepath %> Work/demos/images/calendar.gif", buttonimageonly: true}); $ ("# reviewd Atefrom "). datepicker ("option", "dateformat", 'yy-mm-dd');}); $ (function () {$ ("# reviewdateto "). datepicker ({showon: "button", buttonimage: "<% = basepath %> Work/demos/images/calendar.gif", buttonimageonly: true }); $ ("# reviewdateto "). datepicker ("option", "dateformat", 'yy-mm-dd ');}); //////////////////////////////////////// // $ (function () {$ ("# tabsquery "). tabs ({collapsible: true });});/////////////// //////////////////////////////////////// /// // </SCRIPT> <% list res = new arraylist (); if (Session. getattribute ("worklist ")! = NULL) {res = (arraylist) session. getattribute ("worklist ");} %> <input type = "hidden" id = "thelen" value = ""/> <input type = "hidden" id = "" value = ""/> <input type = "hidden" id = "" value = ""/> <Table> <tr width = "100%"> <TD width = "50%"> <Table border = "1"> <tr> <TD> <Form ID = "lsy_test_work" name = "lsy_test_work" method = "Post" Action = ""> <! -- Query area start --> <Div class = "Demo"> <Div id = "tabsquery"> <ul> <li> <a href = "tabsquery-1"> close query area </A> </LI> </ul> <Div id = "tabsquery-1"> <p> <strong> open the query area </strong> 11 </P> <p> 22 <Table> <tr> <TD> job title: <input type = "text" id = "worktitle" value = ""/> <br> INSTRUCTOR: <input type = "text" id = "teacher" value = ""/> <br> Course name: <input type = "text" id = "Course" value = ""> <br> Student name: <input type = "text" id = "student" value = ""> <br> correction Date: From <input type = "text" id = "reviewdatefrom" value = ""> <br>: <input type = "text" id = "reviewdateto" value = ""> <br> <input type = "button" onclick = "querywork () "value =" query "/> </TD> </tr> </table> </P> </div> <! -- End demo --> <! -- Query area end --> </form> </TD> </tr> <TD> <Table id = "maindatatable" border = "1"> <tr> <TH> assignment title </Th> <TH> instructor </Th> <TH> Course name </Th> <TH> content </Th> <TH> Student name </Th> <TH> class name </Th> <TH> id </Th> </tr> <% IF (res! = NULL) {for (INT I = 0; I <res. size (); I ++) {work WW = (work) res. get (I); %> <tr onclick = "F1 (<% = I + 1%>)"> <TD> <% = ww. getworktitle () %> </TD> <% = ww. getteacher () %> </TD> <% = ww. getcourse () %> </TD> <% = ww. getworkcontent () %> </TD> <% = ww. getstudent () %> </TD> <% = ww. getclassname () %> </TD> <% = ww. GETID () %> </TD> </tr> <% }%> </table> </TD> </tr> </table> </TD> <table> <form name = "addform" id = "addfo RM "Action =" <% = PATH %>/work/addwork. action "method = post> <Table> <tr> <TD width =" 30% "align =" right "> job title: </TD> <input type = "text" id = "eworktitle" name = "work. worktitle "value =" "> </TD> <TD width =" 30% "align =" right "> INSTRUCTOR: </TD> <input type = "text" id = "eteacher" name = "work. teacher "value =" "> </TD> </tr> <TD width =" 30% "align =" right "> Course name: </TD> <input type = "text" id = "ecourse" name = "work. course" Value = ""> </TD> <TD width = "30%" align = "right"> content: </TD> <input type = "text" id = "eworkcontent" name = "work. workcontent "value =" "> </TD> </tr> <TD width =" 30% "align =" right "> Student name: </TD> <input type = "text" id = "estudent" name = "work. student "value =" "> </TD> <TD width =" 30% "align =" right "> Class Name: </TD> <input type = "text" id = "eclassname" name = "work. classname "value =" "></TD> </tr> <TD width =" 30%" Lign = "right"> correction Date: </TD> <input type = "text" id = "reviewdate" value = ""> </TD> <TD width = "30%" align = "right"> </TD> </tr> <TD width = "30%" align = "right"> </TD> <input type = "button" id = "exinzeng" onclick = "addwork () "value =" add "> </TD> <input type =" button "id =" exinzeng "onclick =" delwork () "value =" delete "> </TD> <input type =" button "id =" eedit "onclick =" editwork () "value =" submit changes "> </TD> </tr> <Input type = "hidden" id = "Eid" value = ""/> </table> </form> </table> </TD> </tr> </table> <% string actiontype = ""; if (request. getattribute ("actiontype ")! = NULL) {actiontype = (string) request. getattribute ("actiontype ");} %> <input type = "hidden" id = "actiontype" value = "<% = actiontype %>"/> <SCRIPT> // var basepath = $ ("# basepath" ). val (); var basepath = document. getelementbyid ("basepath "). value; // alert (basepath); var Len = $ ("# maindatatable tr TD "). length; var thelen = Len/6; function F1 (rownum) {// alert ("dddddd"); var STR = "# maindatatable TR: eq (" + rownum + ") "; document. gete Lementbyid ("Eid "). value = $ ("# maindatatable TR: eq (" + rownum + ")" 2.16.children().eq(62.16.html (); document. getelementbyid ("eworktitle "). value = $ ("# maindatatable TR: eq (" + rownum + ")" 2.16.children().eq(02.16.html (); document. getelementbyid ("eteacher "). value = $ ("# maindatatable TR: eq (" + rownum + ")" 2.16.children().eq(12.16.html (); document. getelementbyid ("ecourse "). value = $ ("# maindatatable TR: eq (" + rownum + ")" 2.16.children().eq(22.16.html (); D Ocument. getelementbyid ("eworkcontent "). value = $ ("# maindatatable TR: eq (" + rownum + ")" 2.16.children().eq(32.16.html (); document. getelementbyid ("estudent "). value = $ ("# maindatatable TR: eq (" + rownum + ")" 2.16.children().eq(42.16.html (); document. getelementbyid ("eclassname "). value = $ ("# maindatatable TR: eq (" + rownum + ")" 2.16.children().eq(52.16.html (); // alert ($ ("# maindatatable TR: eq (1) "Maid (); // alert ($ (" # maindatat Able TR: eq (1) "2.16.children().eq(22.16.html (); // alert ($ (" # maindatatable TR: eq (1) "2.16.children().eq(32.16.html ()); // alert ($ ("# maindatatable TR: eq (1)" 2.16.children().eq(42.16.html (); // alert ($ ("# maindatatable TR: eq (1) "Begin (); // alert ($ (" # maindatatable TR: eq (1) "2.16.children().eq(62.16.html (); // alert(this%.html ()); // alert ($ (this ). text ();} function addwork () {var eworktitle = document. getelementbyid ("ew Orktitle "). value; var eteacher = document. getelementbyid ("eteacher "). value; var ecourse = document. getelementbyid ("ecourse "). value; var eworkcontent = document. getelementbyid ("eworkcontent "). value; var estudent = document. getelementbyid ("estudent "). value; var eclassname = document. getelementbyid ("eclassname "). value;/* var url = basepath + "work/addwork. do? Worktitle = "+ eworktitle +" & Teacher = "+ eteacher +" & course = "+ ecourse +" & workcontent = "+ eworkcontent +" & Student = "+ estudent + "& classname = "+ eclassname; * // document. getelementbyid ("TT "). value = URL; // window. location. href = URL; document. addform. submit ();} function delwork () {var worktitle = document. getelementbyid ("eworktitle "). value; var Teacher = document. getelementbyid ("eteacher "). value; var course = document. getelementbyid ("Ecourse "). value; var student = document. getelementbyid ("estudent "). value; var workcontent = document. getelementbyid ("eworkcontent "). value; var classname = document. getelementbyid ("eclassname "). value; var id = document. getelementbyid ("Eid "). value; var url = basepath + "work/delwork. action? Worktitle = "+ worktitle +" & Teacher = "+ teacher +" & course = "+ course +" & Student = "+ student +" & workcontent = "+ workcontent + "& classname = "+ classname +" & id = "+ ID; // alert ("del --" + URL); window. location. href = URL;} function editwork () {var worktitle = document. getelementbyid ("eworktitle "). value; var Teacher = document. getelementbyid ("eteacher "). value; var course = document. getelementbyid ("ecourse "). value; var student = document. getelem Entbyid ("estudent "). value; var workcontent = document. getelementbyid ("eworkcontent "). value; var classname = document. getelementbyid ("eclassname "). value; var id = document. getelementbyid ("Eid "). value; var url = basepath + "work/editwork. action? Worktitle = "+ worktitle +" & Teacher = "+ teacher +" & course = "+ course +" & Student = "+ student +" & workcontent = "+ workcontent + "& classname = "+ classname +" & id = "+ ID; window. location. href = URL;} function querywork () {var worktitle = document. getelementbyid ("worktitle "). value; var Teacher = document. getelementbyid ("teacher "). value; var course = document. getelementbyid ("Course "). value; var student = document. getelementbyid ("student "). val UE; var url = basepath + "work/querywork. Action? Worktitle = "+ worktitle +" & Teacher = "+ teacher +" & course = "+ course +" & Student = "+ student; // alert ("query --" + URL); window. location. href = URL;} var actiontype = document. getelementbyid ("actiontype "). value; If (actiontype = 'otherquery') {var url = basepath + "work/querywork. action "; window. location. href = URL ;} //////////////////////////////////////// //// // $ (document ). ready (function () {// hidecol (1); // hide the second column // $ ('table TD '). fin D ('td: eq (1 )'). hide (); // $ ('table tr '). find ('td: eq (1 )'). hide (); // $ ('# maindatatable TD '). find ('td: eq (0 )'). hide (); // $ ('# maindatatable tr '). find ('td: eq (0 )'). hide (); var Len = $ ("# maindatatable tr TD "). length; var thelen = Len/6; // var Len = $ ("# Mm tr TD "). length; // alert (LEN/6); // alert ("Number of table rows" + thelen); $ ("# thelen "). val (thelen); $ ("# reviewdatefrom "). change (function () {var date1 =$ ("# reviewdatefrom "). val (); var d Ate2 = $ ("# reviewdateto"). Val (); var url = basepath + "ajaxutil/isrightdate. Action? Reviewdatefrom = "+ date1 +" & reviewdateto = "+ date2; alert ($ (" # reviewdatefrom "). val () + "\ r" + URL); jquery. ajax ({URL: URL, type: 'post', async: false, datatype: "text", // data: 'SQL =' + 'hkhkhhhkhk ', success: function (result) {alert ("from ----- 333 ----" + result); If (""! = Result) {alert ("from ---------" + result) ;}}) ;}); // end blur $ ("# reviewdateto "). change (function () {var date1 =$ ("# reviewdatefrom "). val (); var date2 = $ ("# reviewdateto "). val (); var url = basepath + "ajaxutil/isrightdate. action? Reviewdatefrom = "+ date1 +" & reviewdateto = "+ date2; alert ($ (" # reviewdateto "). val () + "\ r" + URL); $. get (URL, function (result) {alert ("Data HHSs:" + result) ;}); // end get}); // end blur }); // end ready //////////////////////////////////// ///// </SCRIPT>

 

 

 

In this example, private string result is useless. We usually configure this

<Struts>
<Package name = "ajaxutil" extends = "JSON-Default" namespace = "/ajaxutil">
<! -- Ajaxutil begin -->

<Action name = "isrightdate" class = "org. lsy. Test. Ajax. Action. comparedateaction" method = "isrightdate">

<Result name = "success" type = "JSON">
<Param name = "root"> result </param>
</Result>
</Action>

<! -- Ajaxutil end -->
</Package>
</Struts>

 

And write Ajax like this:

Jquery. Ajax ({
URL: URL,
Type: 'post ',
Async: false,
Datatype: "JSON ",
// Data: 'SQL =' + 'hkhkhhhkhk ',
Success: function (result ){
Alert ("from ----- 333 ----" + result );
If (""! = Result ){
Alert ("from ---------" + result );
}
}
});

In this way, the value to be returned is put in the resuest variable of the Java class, and the value of this parameter is sent to the Ajax

Callback function.

In my example, we inherit extends = "struts-Default" and return none in the Java class.

Datatype: "text" in Ajax, so the returned content is not private string result; it is PW. println ("Y ");

(PW is printwriter PW = resp. getwriter ();). The returned result is output to the callback function using printwriter.

 

 

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.