AJax¼¼Êõ

Struts2½áºÏAjaxʵÏֵǼ

×ÖºÅ+ ×÷ÕߣºH5Ö®¼Ò À´Ô´£ºH5Ö®¼Ò 2017-05-06 13:12 ÎÒÒªÆÀÂÛ( )

Ç°ÑÔ£ºStruts2×÷Ϊһ¿îÓÅÐãµÄMVC¿ò¼Ü£¬ºÍAjax½áºÏÔÚÒ»Æð£¬Óû§¾Í»áÓÐÁ¼ºÃµÄÌåÑ飬±¾Æª²©ÎÄÎÒÃÇÀ´Ä£ÄâÒ»¸ö¼òµ¥µÄµÇ¼²Ù×÷£¬ÊµÏÖAjaxµÄÒì²½ÇëÇó£¬ÆäÖÐStruts2½øÐÐ

±êÇ©£ºform   Ê¹Óà  equals   ·þÎñ   ÓÅÐã   ¶þ½øÖÆ   ¼¼Êõ   www   ¶¨Ïò   

Ç°ÑÔ£ºStruts2×÷Ϊһ¿îÓÅÐãµÄMVC¿ò¼Ü£¬ºÍAjax½áºÏÔÚÒ»Æð£¬Óû§¾Í»áÓÐÁ¼ºÃµÄÌåÑ飬±¾Æª²©ÎÄÎÒÃÇÀ´Ä£ÄâÒ»¸ö¼òµ¥µÄµÇ¼²Ù×÷£¬ÊµÏÖAjaxµÄÒì²½ÇëÇó£¬ÆäÖÐStruts2½øÐеÄÊÇÁ´½Ó´¦Àí£¬Action¶¯Ì¬´¦ÀíÇëÇó£¬Ajax¸ºÔð½øÐÐÒì²½´¦Àí£¬´«ËÍÊý¾ÝÓë·µ»ØÊý¾Ý£¬ÆäÖÐÖ÷ҪʹÓõļ¼ÊõÓУºstruts2+Ajax+Jquery+Css,ºÃ°É£¬·Ï»°²»¶à˵£¬ÈÃÎÒÃÇÀ´¿´¿´Õâ¸öʵÀý°É¡£

µÚÒ»²½£ºÐ½¨Ò»¸öDynamic web project,È»ºóÆðÃû£ºStruts2Ajax,ÒýÈëÏà¹ØµÄjar°ü£¬ÔÚweb.xmlÎļþÖÐÅäÖÃstruts2µÄfilter.ÎÒÃǶ¼ÖªµÀеÄÇëÇóÊ×ÏȽøÈëµÄ.xmlÎļþ£¬ËùÒÔÅäÖÃÉϹýÂËÆ÷£¬ÕâÑùÇëÇó¾Í»á×Ô¶¯½øÈë½»¸østruts2´¦ÀíÁË¡£

<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee "> <display-name>Struts2Ajax</display-name> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>

¡¡¡¡

µÚ¶þ²½£ºÎÒÃÇÀ´ÅäÖÃStruts2µÄÅäÖÃÎļþ£¬ÆäÖÐÖ÷ÒªÊÇÅäÖÃAction£¬Ò²¾ÍÊÇÇëÇó¹ýÀ´½»¸øË­½øÐд¦Àí£¬×¢ÒâÕâÀïµÄresult Type="stream"ÊÇioÖÐÁ÷µÄ·½Ê½£¬ËüÊä³öµÄÊǶþ½øÖÆÊý¾Ý£¬ÎÒÃǵÄcontentTypeÊÇhtmlÎļþ£¬Ëü×îÖջ᷵»ØÒ³ÃæÒÔhtmlµÄÐÎʽ³öÏÖÔÚÒ³ÃæϽÇ

<?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> <package extends="struts-default"> <action> <result type="stream"> <param>text/html</param> <param>result</param> </result> </action> </package> </struts>

¡¡¡¡µÚÈý²½£ºÎÒÃÇн¨Ò»¸ö°ü£¬ÆðÃûcom.wyq.Action£¬Ö÷ÒªÊÇÀ´´æ·ÅÇëÇóµÄAction¡£Ð½¨À࣬LoginACtion,´úÂëÈçÏ£º

package com.wyq.Action; import java.io.ByteArrayInputStream; import java.io.InputStream; import com.opensymphony.xwork2.Action; public class LogAction implements Action{ private String user; private String pass; private InputStream inputStream; public String getUser() { return user; } public void setUser(String user) { this.user = user; } public String getPass() { return pass; } public void setPass(String pass) { this.pass = pass; } public void setInputStream(InputStream inputStream) { this.inputStream = inputStream; } public InputStream getResult() { return inputStream; } @Override public String execute() throws Exception { String sus="¹§Ï²Ä㣬µÇ¼³É¹¦£¡"; String fail="¶Ô²»Æð£¬Óû§ÃûºÍÃÜÂ벻ƥÅä"; inputStream=user.equals("wangyongqiang")&&pass.equals("123")?new ByteArrayInputStream(sus.getBytes("UTF-8")):new ByteArrayInputStream(fail.getBytes("UTF-8")); return SUCCESS; } }

¡¡¡¡µÚËIJ½£ºÎÒÃÇÀ´±àдÇëÇóµÄÒ³Ã棬loginForm.jsp£¬ÆäÖÐÒªÒýÈëJquery¿ò¼Ü£¬Ê¹Óõ½css£¬ÕâÀïΪÁË·½±ãÆð¼û£¬ÎÒ²ÉÓõķ½Ê½ÊÇÒýÈëÒ»¸ö»¥ÁªÍøÖеÄjquery×ÊÔ´£¬È»ºó½èÖúÓÚJqueryµÄAjax£¬ÊµÏÖÒì²½ÇëÇó

<%@ page contentType="text/html; charset=GBK" language="java" errorPage="" %> <%@ taglib prefix="s" uri="/struts-tags" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>ʹÓÃstream½á¹ûʵÏÖAjax</title> <script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.7.2.min.js" type="text/javascript"> </script> </head> <body> <s:form> <s:textfield label="Óû§Ãû"/> <s:textfield label="ÃÜÂë"/> <tr><td colspan="2"> <input type="button" value="Ìá½»"/> </td></tr> </s:form> <div> </div> <script type="text/javascript"> // ΪidΪloginBnµÄ°´Å¥°ó¶¨Ê¼þ´¦Àíº¯Êý $("#loginBn").click(function() { $("#show").hide(); // Ö¸¶¨Ïòlogin·¢ËÍÇëÇó£¬ÒÔidΪloginForm±íµ¥Àï¸÷±íµ¥¿Ø¼þ×÷ΪÇëÇó²ÎÊý $.get("login" , $("#loginForm").serializeArray() , // Ö¸¶¨»Øµ÷º¯Êý function(data , statusText) { $("#show").height(30) .width(400) .css("border" , "1px solid black") .css("border-radius" , "10px") .css("background-color" , "#efef99") .css("color" , "#ff0000") .css("padding" , "20px") .empty(); $("#show").append("µÇ¼½á¹û£º" + data + "<br />"); $("#show").show(2000); }, // Ö¸¶¨·þÎñÆ÷ÏìӦΪhtml "html"); }); </script> </body> </html>

¡¡¡¡µÚÎå²½£º±àÒ룬Ȼºó·ÅÔÚweblogicÖв¿Ê𣬽ÓÏÂÀ´¾Í¿ÉÒÔÔËÐÐÁË¡£ÔÚä¯ÀÀÆ÷´°¿Ú£¬ÊäÈ룺:7001/Struts2Ajax/loginForm.jsp£¬ÎÒÃǾͿÉÒÔ¿´µ½Ò»¸öµÇ½¿ò¡£

¼¼Êõ·ÖÏí

È»ºó£¬°´ÕÕÎÒÃǵÄд·¨£¬ÏÈÀ´ÊäÈëÒ»¸ö´íÎóµÄÓû§ÃûºÍÃÜÂ룬ÎÒÃÇ¿´¿´»á·¢ÉúʲôÇé¿ö¡£

¼¼Êõ·ÖÏí

ÔÙ°´ÕÕActionÖÐÉèÖõÄÕ˺źÍÃÜÂ룬ÎÒÃÇÀ´¿´¿´»á·µ»Øʲô½á¹û£»

¼¼Êõ·ÖÏí

¡¡

1.±¾Õ¾×ñÑ­ÐÐÒµ¹æ·¶£¬ÈκÎתÔصĸå¼þ¶¼»áÃ÷È·±ê×¢×÷ÕߺÍÀ´Ô´£»2.±¾Õ¾µÄÔ­´´ÎÄÕ£¬ÇëתÔØʱÎñ±Ø×¢Ã÷ÎÄÕÂ×÷ÕߺÍÀ´Ô´£¬²»×ðÖØÔ­´´µÄÐÐΪÎÒÃǽ«×·¾¿ÔðÈΣ»3.×÷ÕßͶ¸å¿ÉÄܻᾭÎÒÃDZ༭Ð޸Ļò²¹³ä¡£

Ïà¹ØÎÄÕÂ
  • jquery ¶ÁÈ¡Ò³Ãæload get post ajax ËÄÖÖ·½Ê½´úÂëд·¨

    jquery ¶ÁÈ¡Ò³Ãæload get post ajax ËÄÖÖ·½Ê½´úÂëд·¨

    2017-03-30 17:01

  • ÀÏÓÍÌõµÄºǫ́¿ª·¢

    ÀÏÓÍÌõµÄºǫ́¿ª·¢

    2017-02-27 14:00

  • HackerNews

    HackerNews

    2017-02-26 10:00

  • AjaxÒì²½»ñÈ¡htmlÊý¾ÝÖаüº¬js·½·¨ÎÞЧµÄ½â¾ö·½·¨

    AjaxÒì²½»ñÈ¡htmlÊý¾ÝÖаüº¬js·½·¨ÎÞЧµÄ½â¾ö·½·¨

    2017-02-21 16:02

ÍøÓѵãÆÀ
½