@RequestAttribute
获取HTTP的请求(request)对象属性值,用来传递给控制器的参数。
创建Hello控制器
package com.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class Hello {
@RequestMapping("/show")
public String show(@RequestAttribute("name")String userName) {
System.out.println("userName="+userName);
return "success";
}
}
创建index.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>index</title>
</head>
<body>
<%
request.setAttribute("name", "baixue");
request.getRequestDispatcher("/show").forward(request,response);
%>
</body>
</html>
启动 Tomcat访问 index.jsp
更多推荐
Spring MVC @RequestAttribute注解
发布评论