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>