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 session = null;
	
	protected Boolean success = null;
	
	protected Map errors = null;

	@Override
	public void setSession(Map session) {
		this.session = session;
	}

	public Map getErrors() {
		return errors;
	}

	public void addErrors(String key,String message) {
		if(null == this.errors){
			this.errors = new HashMap();
			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("
" + value + '
'); } });

<%@ page language="java" pageEncoding="utf-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>








LOGIN



LOGIN




	
	
		
			
				false
			
		
		
	

你可能感兴趣的:(Struts,Java,Apache,json,JavaScript)