文本儀一個實例講述了struts2+jquery實現ajax登陸的實現方法,具體步驟如下:
一、新建一個web項目,取名test。配置好struts2的環境,並導入Jquery的js文件到該項目。
二、在com.action包下,新建一個loginAction.java
loginAction.java的代碼如下
package com.action; import org.apache.struts2.convention.annotation.Action; import org.apache.struts2.convention.annotation.ParentPackage; import org.apache.struts2.convention.annotation.Result; import org.apache.struts2.convention.annotation.Results; import com.opensymphony.xwork2.ActionSupport; @Action("login") @ParentPackage(value = "json-default") @Results({ @Result(name = "success", type = "json", params = { "data", "flag" }), }) public class LoginAction extends ActionSupport { /** * */ private static final long serialVersionUID = 1751244794407005783L; private String flag; private String username; private String password; public String execute() { try { if (getUsername() == null || getUsername().trim().equals("")) { setFlag("用戶名不能為空"); return SUCCESS; } else if (getPassword() == null || getPassword().trim().equals("")) { setFlag("密碼不能為空"); return SUCCESS; } else if (getUsername().trim().equals("admin") && getPassword().equals("admin")) { setFlag("登陸成功"); return SUCCESS; } else { setFlag("用戶名或密碼錯誤"); return SUCCESS; } } catch (Exception e) { e.printStackTrace(); setFlag("登陸異常"); return SUCCESS; } } public String getFlag() { return flag; } public void setFlag(String flag) { this.flag = flag; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } }
三、在WebRoot目錄下新建index.jsp和success.jsp
index.jsp為登陸界面,success.jsp為登陸成功後跳轉的界面。
index.jsp內容如下:
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>" rel="external nofollow" > <title>My JSP 'index.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <script type="text/javascript" src="./resource/js/jquery.min.js"></script> </head> <script type="text/javascript"> $(document).ready(function() { $(".loginButton").click(function() { var name = $("#username").val();//獲取登陸用戶名 var password = $("#password").val(); //獲取登陸密碼 $.post("login", { username : name, password : password }, callback, "json"); }); function callback(data) { var value=data.flag; if(value=="登陸成功"){ location.href="./success.jsp" rel="external nofollow" ; }else{ alert(value); } }); </script> <body> <input name="username" id="username" type="text" /> <input name="password" id="password" type="password" /> <button class="loginButton" class="btn btn-inverse">登陸</button> </body> </html>
例子:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<script src="jquery.js"></script>
<script>
function sub(){
var value1 = $("#id1").val();
var value2 = $("#id2").val();
if(value1!="" && value2!=""){
$.ajax({
url:'a.action',
type:'post',
data:'value1='+value1+'&value2='+value2
//data:{value1:value1,value2:value2},
dataType:'text',
success:function(text){
alert(text);
}
})
}
}
</script>
</head>
<body>
<form id="form1" name="form1">
<input type="text" id="id1"/><br />
<input type="text" id="id2"/><br />
<input type="button" value="提交" onclick="sub();"/>
</form>
</body>
</html>
struts.xml:
<action name="a" class="com.test.a.action" method="test">
</action>
a.action:
private String value1;
private String value2;
private String text;
get()...set()...
public void test()throws Exception{
//do somethings
HttpServl......余下全文>>
和普通ajax上傳後台是一樣的!
只是從struts2中返回值到前台略又不同
給你個簡單的JQuery實現的ajax例子(需要引入JQuery核心jar,有不明白的可以百度Hi我):
①lib:引入struts2-json-plugin-2.2.3.1.jar
②struts2配置文件:要建一個包繼承json-default
(PS:①②是struts2返回json格式的值使用的)
③頁面js:
$(function() {
$("#tj").click(function() {
var params = $("#entAddForm").serialize();//傳遞的參數
$.ajax({
type: "POST",
url: "jsonAdvAction!flexTable.action", //你要提交的action
data: params,
dataType:"text",
success: function(json){ //返回json格式的值,裡面是解析
var obj = $.parseJSON(json);
var state_value = obj.gS_Result;
var state_null = obj.gS_Null;
if(state_value == "success"){
if(state_null=="nulls"){
alert("沒有查到你要的數據。。。");
}
parent.$("#flexTable").flexReload();
parent.$().closeIframe();
}else{
alert("有錯誤發生,json=" + json);
}
//parent.$("#flexTable").flexReload();
//parent.$().closeIframe();
},
error: function(json){
alert("json=" + json);
return false;
}
});
});
});
good luck!有問題可以追問或百度Hi我!~...余下全文>>