struts2 jquery Ajax form 提交

<%@ page language="java" import="java.util.*" pageEncoding="GBK"%>
<%
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%>">
    
    <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">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
  </head>
  <script type="text/javascript" src="<%=path %>/jquery-1.3.1.js"></script>
  <script type="text/javascript" src="<%=path %>/jquery.form.js"></script>
  <script type="text/javascript">
  $(function(){
	  $("#btn").click(function(){
		  alert(" 提交!");
		    $('#myForm1').ajaxForm(function() {
                login_Result();
             });
		    //return  false;不需要加
		    
	  })	
	  
	   $("#btn2").click(function(){
			 	var params=$('#myForm3').formSerialize(); //这里直接就序列化了表单里面的值;很方便   
	     	 	params = decodeURIComponent(params,true); 	     	 	
			 	$.ajax({   
	              	url :"/StudentDemo/cyzcFwb/studentLogin2!loginn.action",  //后台处理程序   
	           		type:'post',    //数据发送方式   
	             	dataType:'json',   //接受数据格式   
	            	data:params,   //要传递的数据;就是上面序列化的值   
	             	success:login_Result()
	     	 	});   
			 	return false;//防止刷新页面
	  	})	  
	  	 $("#btn3").click(function(){			  
			 	var queryString = $('#myForm4').formSerialize();
				$.post("/StudentDemo/cyzcFwb/studentLogin2!loginn.action", queryString);  
				return false;
			
	  	})	
	  
  })
  
  function login_Result(){
	  alert("保存,返回提示信息!");
  }
  </script>
  
  <body>

	<form action="/StudentDemo/cyzcFwb/studentLogin!loginn.action" id="myForm1" method="post">
		<input type="text"  name="student.username" id="username" ><br>
		<input type="submit"  value="提交2" id="btn" ><br>
	</form>	
	<br><br>
	<form action="/StudentDemo/cyzcFwb/studentLogin2!loginn.action" id="myForm3" method="post">
		<input type="text"  name="student.username" id="username" ><br>
		<input type="submit"  value="提交2" id="btn2" ><br>
	</form><br><br>	
	<form action="/StudentDemo/cyzcFwb/studentLogin2!loginn.action" id="myForm4" method="post">
		<input type="text"  name="student.username" id="username" ><br>
		<input type="submit"  value="提交3" id="btn3" ><br>
	</form>

  </body>
</html>

 

 

 

package user;

public class Login {
	
	Student student=new Student();	
	private String username;	
	private String messageType;	
	
	public String loginn(){	
		System.out.println(student.getUsername());
		messageType=student.getUsername()+"    哈哈";
		if(student.getUsername().equals("aa")){
			return "success";
		}
		return "success";		
	}
	

	public Student getStudent() {
		return student;
	}
	public void setStudent(Student student) {
		this.student = student;
	}
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}


	public String getMessageType() {
		return messageType;
	}


	public void setMessageType(String messageType) {
		this.messageType = messageType;
	}	

}

 

 

 

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
	<package name="cyzcFwb"  extends="struts-default,json-default"  namespace="/cyzcFwb">
		
		<action name="studentLogin" class="user.Login"   >
		 	<result name="success">/success.jsp</result>
        	<result name="error">/error.jsp</result>		
		</action>
		
		<action name="studentLogin2" class="user.Login"   >
		 	 <result type="json">
		        	<param name="root">messageType</param>
		      </result>       			
		</action>
	
	
	</package>
</struts>    

 

你可能感兴趣的:(JavaScript,jquery,Ajax,json,struts)