Ajax2Strut2jQuery


package cn.com.oneslife;

import java.util.HashMap;
import java.util.Map;

import org.apache.struts2.interceptor.SessionAware;

public class AjaxAction implements SessionAware{

private static final long serialVersionUID = 555146237439856288L;

protected static final String SUCCESS = "success";

protected Map<String,Object> session = null;

protected Boolean success = null;

protected Map<String,String> errors = null;

@Override
public void setSession(Map<String, Object> session) {
this.session = session;
}

public Map<String, String> getErrors() {
return errors;
}

public void addErrors(String key,String message) {
if(null == this.errors){
this.errors = new HashMap<String, String>();
this.errors.put(key, message);
}else{
this.errors.put(key, message);
}
}

public Boolean getSuccess() {
return success;
}

public void setSuccess(Boolean success) {
this.success = success;
}
}


$(document).ready(function(){
$('#btnSubmit').click(function(){
options = {
url:'login.action',
type:'POST',
dataType:'json',
success:parentJSON
};
function parentJSON(response){
$('form').find('#Error').remove();

if(!response.success){
for(var error in response.errors){
addError(error,response.errors[error]);
}
}
}
$('form').ajaxSubmit(options);
});

$('#btnReset').click(function(){
$(':input:not(:button)').val('');
$('form').find('#Error').remove();
});

function addError(name,value){
var selector = '#' + name.replace('.','\\\.');
$(selector).after("<div id='Error' style='color:red;font-size:12px'>" + value + '</div>');
}
});

<%@ page language="java" pageEncoding="utf-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript" src="scripts/jQuery/jquery-1.3.2.min.js"></script>
<script type="text/javascript" src="scripts/jQuery/jquery.form.js"></script>
<script type="text/javascript" src="scripts/util/md5.js"></script>
<script type="text/javascript" src="scripts/index.js"></script>
<title>LOGIN</title>
</head>
<body>
<s:form id="form" action="login" method="post">
<table align="center" style="margin-top: 200px;">
<thead>
<tr>
<th>LOGIN</th>
</tr>
</thead>
<tbody>
<tr>
<td><s:label value="用户"/></td>
<td><s:textfield id="account.name" name="account.name" /></td>
</tr>
<tr>
<td><s:label value="密码"/></td>
<td><s:password id="account.password" name="account.password" /></td>
</tr>
<tr>
<td colspan="2">
<input id="btnSubmit" type="button" value="登陆" />
<input id="btnReset" type="button" value="重置" />
</td>
</tr>
</tbody>
</table>
</s:form>
</body>
</html>

<?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>
<constant name="struts.ui.theme" value="simple"></constant>
<package name="struts" extends="json-default">
<global-results>
<result type="json">
<param name="ignoreHierarchy">false</param>
</result>
</global-results>
<action name="login" class="cn.com.oneslife.LoginAction" />
</package>
</struts>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值