程序猿全敏
								2015-12-31 11:22:58
							
							
								原
							jquery ajax无刷新请求Struts2验证用户名密码数据库是否存在
通过ajax请求验证后台数据是否存在.
首先导入struts2的核心包。
后台Action代码
import com.opensymphony.xwork2.ActionSupport;
public class CodeCheckAction extends ActionSupport {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private String code;
	private String result;
	
	public String execute() throws Exception {
		System.out.println("code的值为"+code);
		String msg = "F";
		
		if("abc".equals(code)){
			msg = "T";
		}
		
		result="{\"retCode\":\""+msg+"\"}";
		
		return SUCCESS;
	}
	public String getCode() {
		return code;
	}
	public void setCode(String code) {
		this.code = code;
	}
	public String getResult() {
		return result;
	}
	public void setResult(String result) {
		this.result = result;
	}
	
}
struts.xml代码
<action name="userAction" class="action.UserAction"> <result name="index">/index.jsp</result> </action>
然后jsp js代码
$(function(){
			
			$("#btn").click(function(i,e){
				var code_input = $("#code");
				var code = code_input.val()+"";
				$.getJSON("test?code="+code,function(root,error){
					var retcode = $.parseJSON(root);
					if(retcode.retCode != "T"){
						//code_input.val("");
						document.getElementById("msg").innerHTML="<font color='red'>不可用</font>";
					}else{
						document.getElementById("msg").innerHTML="<font color='green'>可用</font>";
					}
				});
			});
		});
这是body代码
<form id="form"> <input id="code" name="code" type="text" /><span id="msg"></span><br/> <button id="btn" type="button">测试</button> </form>
然后运行
输入一个错误的,前台显示
后台控制台接收数据
现在输入一个正确的code,前台显示
后台接收数据
猜你喜欢
- struts2利用ajax返回json数据实现局部刷新的简单实例
- Struts2验证文本框信息
- struts2开发用户登陆时输入图片验证码实现验证的简单实例
- 手把手教你们通过jquery ajax调用查询java struts2后端数据+js拼接字符串
- Jquery结合Struts2实现无刷新
- Struts2+JQuery+Json实例
- struts2实现分页
- Struts2国际化支持(中英文选择)
- struts2的简单demo实例源代码下载
- Struts2用户登录入门DEMO实例
- struts2标签库详解
- 【猪猪-后端】中英日三版Struts2多语言国际化,一套模版根据语言切换不同CSS,开发必备Demo,部署即可查看
请下载代码后再发表评论
    
											  
													 文件名:testAjax2.rar,文件大小:4227.293K
											  
											  
												  下载
											  
											  
											  
												
										  
								- /- /testAjax2- /testAjax2/.classpath
- /testAjax2/.mymetadata
- /testAjax2/.project
- /testAjax2/.settings- /testAjax2/.settings/.jsdtscope
- /testAjax2/.settings/com.genuitec.eclipse.core.prefs
- /testAjax2/.settings/org.eclipse.jdt.core.prefs
- /testAjax2/.settings/org.eclipse.wst.common.component
- /testAjax2/.settings/org.eclipse.wst.common.project.facet.core.xml
- /testAjax2/.settings/org.eclipse.wst.jsdt.ui.superType.container
- /testAjax2/.settings/org.eclipse.wst.jsdt.ui.superType.name
 
 
 
- /testAjax2
 相关代码
相关代码
				 最近下载
最近下载
				
                Sleachp     LV8
                2023年5月5日
            
            
        
                1973356987     LV13
                2021年6月17日
            
            
        
                itscod     LV6
                2019年6月16日
            
            
        
                sky19961212     LV18
                2019年6月2日
            
            
        
                咕噜流氓兔     LV5
                2018年10月11日
            
            
        
                z924931408     LV25
                2018年5月23日
            
            
        
                812992358     LV11
                2018年5月10日
            
            
        
                1335203     LV10
                2018年3月14日
            
            
        
                mkl_mzq     LV10
                2017年9月1日
            
            
        
                zl0919     LV18
                2017年8月2日
            
            
         最近浏览
最近浏览
				
                uni-code_0123     LV1
                2023年11月29日
            
            
        
                Sleachp     LV8
                2023年5月5日
            
            
        
                zxc131313     LV12
                2023年4月8日
            
            
        
                Tg171017     LV12
                2023年1月9日
            
            
        
                微信网友_5998053264134144     LV1
                2022年6月11日
            
            
        
                852545556     LV7
                2022年5月3日
            
            
        
                单身汪    
                2021年6月29日
            
            
                    暂无贡献等级
            
        
                1973356987     LV13
                2021年6月17日
            
            
        
                talete     LV3
                2020年11月20日
            
            
        
                殷章宁     LV2
                2020年8月14日
            
            
        




 
                 
     
                