「Spring」HttpServletRequestでフォームの値を取り出す

書式
request.getParameter(フォームの変数名);
request.getSession().setAttribute(セッション変数名,保存データ);

使用例
1.JSPコード

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<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コード

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
@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";		   
}

 

SpringMVC

Posted by arkgame