一、 通过herf来请求
二、通过获取表单来发送请求
三、通过ajax来请求(get,post)
首页建立前面四个请求的button:
处理方法如下:
/**第一种提交方式
* */
function submitForm1(){
window.location.href="../abc/href1?param=hrefMethod";
}
/**第二种提交方式
* */
function submitForm2(){
var form=document.forms[0];
form.method="post";
form.action="../abc/fromsub?param=formMethod";
form.submit();
}
/**
*第三种提交方式
*/
var xmlHttp;
//创建xmlHttp
function createXMLHttpRequest(){
if (window.XMLHttpRequest){// code for IE7+, Firefox, Chrome, Opera, Safari
xmlHttp=new XMLHttpRequest();
}else {// code for IE6, IE5
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
}
//Ajax使用get方式发送
function submitForm3(){
createXMLHttpRequest();
var url="../abc/ajaxget?";
url=url+"¶m=" + new Date().getTime();
xmlHttp.onreadystatechange=handleStateChange;
xmlHttp.open("GET",url,true);
xmlHttp.send(null);
}
//Ajax使用post方式发送
function submitForm4(){
createXMLHttpRequest();
var url="../abc/ajaxpost?param=" + new Date().getTime();
xmlHttp.open("POST",url,true);
xmlHttp.onreadystatechange=handleStateChange;
xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
xmlHttp.send("nihao");
}
function handleStateChange(){
if(xmlHttp.readyState==4){
//解析返回值
if(xmlHttp.status==200){
var responseText=document.createTextNode(xmlHttp.responseText);
alert("后台返回的返回值: "+xmlHttp.responseText);
}
}
}
处理controller如下:
package com;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping("/abc")
public class ReceiveController {
@RequestMapping("/login")
public String helloMvc1() {
return "login";
}
@RequestMapping("/dologin")
public String dologin(HttpServletRequest request,HttpServletResponse response) {
String username=request.getParameter("username");
String password=request.getParameter("password");
request.setAttribute("username", username);
System.out.println(username+password);
return "admin";
}
@RequestMapping("/href1")
public String herf(HttpServletRequest request,HttpServletResponse response) {
String param=request.getParameter("param");
System.out.println(param);
return "Test";
}
@RequestMapping("/fromsub")
public String fromsub(HttpServletRequest request,HttpServletResponse response) {
String param=request.getParameter("param");
System.out.println(param);
return "Test";
}
@RequestMapping("/ajaxget")
public String ajaxget(HttpServletRequest request,HttpServletResponse response) {
String param=request.getParameter("param");
System.out.println(param);
return "Test";
}
@RequestMapping("/ajaxpost")
public String ajaxpost(HttpServletRequest request,HttpServletResponse response) {
String param=request.getParameter("param");
System.out.println(param);
return "Test";
}
}