「Spring MVC」@RequestParamアノテーションを使用するサンプル

1.Javaコード
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
public class RequestParamExampleController {

@RequestMapping(“/user")
public String userInfo(Model model,
@RequestParam(value = “name", defaultValue = “Guest") String name) { model.addAttribute(“name", name);
if(“demo".equals(name)) {
model.addAttribute(“email", “demo@sample.com");
} else{
model.addAttribute(“email", “Not set");
}
return “userInfo";
}

}
2.JSPコード
/WEB-INF/pages/userInfo.jsp
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8″>
<title>User Info</title>
</head>
<body>
<h2>${name}</h2>
Email: ${email}
<br>
</body>
</html>
URLの確認
http://localhost:8080/HelloSpringMVC/user?name=demo

Java

Posted by arkgame