SpringMVC 参数传递

使用@RequestParam 注解获取GET请求或POST请求提交的参数;

获取Cookie的值:使用@CookieValue :

根据不同的Web请求方法,映射到不同的处理方法:
使用登陆页面作示例,定义两个方法分辨对使用GET请求和使用POST请求访问login.htm时的响应。可以使用处理GET请求的方法显示视图,使用POST请求的方法处理业务逻辑;

@Controller
public class LoginController {
@RequestMapping(value = "/login", method = RequestMethod.GET)
public String login() {
return "login";
}
@RequestMapping(value = "/login", method = RequestMethod.POST)
public String login2(HttpServletRequest request) {
String username = request.getParameter("username").trim();
System.out.println(username);
return "login2";
}
}

@PathVariable 取 URL 路径中的某个值为变量,如下

@RequestMapping("/index/{username}")

public String index(@PathVariable("username") String username)
{
   System.out.print(username);
   return "index";
}

  

在@RequestMapping中定义访问页面的URL模版,使用{}传入页面参数,使用@PathVariable 获取传入参数,即可通过地址:http://localhost:8080/crm/index/tanqimin.htm 访问;

上一篇:第三章、vue基础精讲


下一篇:【C++自我精讲】基础系列六 PIMPL模式