±êÇ©£º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ÖÐÉèÖõÄÕ˺źÍÃÜÂ룬ÎÒÃÇÀ´¿´¿´»á·µ»Øʲô½á¹û£»
¡¡