1.
package com.yk.springmvc.handlers; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; @RequestMapping("/springmvc") @Controller public class SpringMVCTest { private static final String SUCCESS = "success"; /** * 使用method属性指定请求方式 * @return */ @RequestMapping(value="/testMethod",method=RequestMethod.POST) public String testMethod(){ System.out.println("SpringMVCTest.testMethod()"); return SUCCESS; } @RequestMapping("/testRequestMapping") public String testRequestMapping(){ System.out.println("SpringMVCTest.testRequestMapping()"); // return "success"; 下面会有很多,所以定义一个常量 return SUCCESS; } }
2.测试POST请求(默认是GET请求)
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <!-- 测试post请求 --> <form action="springmvc/testMethod" method="POST"> <input type="submit" value="submit测试POST请求"/> </form> <!-- <a href="springmvc/testMethod">TesttestMethod</a> --> <br /> <br /> <br /> <a href="springmvc/testRequestMapping">Test--RequestMapping</a> <br /> <br /> <br /> <a href="helloworld">Hello World</a> </body> </html>