「Spring」HttpServletRequestでフォームの値を取り出す
書式
request.getParameter(フォームの変数名);
request.getSession().setAttribute(セッション変数名,保存データ);
使用例
1.JSPコード
<h2>${SESS_USER}</h2>
<form action="/create" method="post">
<input type="text" name="username" />
//処理コード
<h2>${SESS_USER}</h2>
<form action="/create" method="post">
<input type="text" name="username" />
//処理コード
<h2>${SESS_USER}</h2> <form action="/create" method="post"> <input type="text" name="username" /> //処理コード
2.Javaコード
@RequestMapping(value = "/create",method = RequestMethod.POST)
public String create(HttpServletRequest request,
HttpServletResponse response) {
// usernameを受け取る
String cft = request.getParameter("username");
// セッションSESS_USERに文字列を保管
request.getSession.setAttribute("SESS_USER","ユーザ名:"+cft);
return "create";
}
@RequestMapping(value = "/create",method = RequestMethod.POST)
public String create(HttpServletRequest request,
HttpServletResponse response) {
// usernameを受け取る
String cft = request.getParameter("username");
// セッションSESS_USERに文字列を保管
request.getSession.setAttribute("SESS_USER","ユーザ名:"+cft);
return "create";
}
@RequestMapping(value = "/create",method = RequestMethod.POST) public String create(HttpServletRequest request, HttpServletResponse response) { // usernameを受け取る String cft = request.getParameter("username"); // セッションSESS_USERに文字列を保管 request.getSession.setAttribute("SESS_USER","ユーザ名:"+cft); return "create"; }