文字化け対策する。
src/main/webapp/WEB-INF/web.xml を変更する。
すべてのリクエストに対してUTF-8エンコーディングを使用するように、フィルターを設定する。
最後の web-app タグの手前に以下の設定を追加する。
<filter> <filter-name>CharacterEncodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> <init-param> <param-name>forceEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>CharacterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
SpringMVCを使わないやりかた(テキストp.188)
テキストではshoeMessage.jspを使いまわしているが、そうすると修正前のものが残らないので、JSPやコントローラのメソッドを新しく作成する。
まずは requestparam.jsp を作成する。
<!DOCTYPE html> <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <html> <head> <meta charset="utf-8"> <title>Welcome</title> </head> <body> <h2>${message}</h2> <form action="/SpringMyApp/rp"> <input type="text" name="input1" /> <input type="submit"> </form> </body> </html>
新しく rp というURLを割り当てたので、それに対応するためにコントローラのメソッドを追加する。
MyAppController.java の最後に以下のコードを追加する。
@RequestMapping(value = "/rp", method = RequestMethod.GET) public String requestparam(Model model) { FormModel fm = new FormModel(); fm.setInput1("ここに書く"); model.addAttribute("formModel", fm); model.addAttribute("message", "何か書いてください。"); return "requestparam"; }
requestparam.jsp の form で post メソッドを指定する。
<!DOCTYPE html> <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <html> <head> <meta charset="utf-8"> <title>Welcome</title> </head> <body> <h2>${message}</h2> <form action="/SpringMyApp/rp" method="post"> <input type="text" name="input1" /> <input type="submit"> </form> </body> </html>
コントローラの引数を変更して HttpServletRequest にメッセージを設定する。
@RequestMapping(value = "/rp", method = RequestMethod.GET) public String requestparam(HttpServletRequest req, HttpServletResponse res) { req.getSession().setAttribute("message", "何か書いて"); return "requestparam"; }
クエリ送信をクリックすると POST method not supported のエラーになる。
コントローラにPOSTメソッドを受け付けるメソッドを追加する。
@RequestMapping(value = "/rp", method = RequestMethod.POST) public String rpform(HttpServletRequest req, HttpServletResponse res) { String param = req.getParameter("input1"); req.getSession().setAttribute("message", "typed:" + param); return "requestparam"; }
@RequestParam によるパラメータの受け渡し
@RequestParamアノテーションを使うと、自動的にパラメータを取得してくれる。
パラメータの名前はアノテーションの引数で指定する。
@RequestMapping(value = "/rp", method = RequestMethod.POST) public String rpform(@RequestParam("input1") String input1, HttpServletRequest req) { req.getSession().setAttribute("message", "typed:" + input1); return "requestparam"; }
ModelAndViewクラスを使用する方法
新規で mav.jsp を作成する。
<!DOCTYPE html> <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>${title}</title> <style type="text/css"> h1 { font-size:16pt; background-color: #ccccff; padding: 3px; } p { color: #000066; } </style> </head> <body> <h1>${title}</h1> <p>${message}</p> </body> </html>
コントローラにGETメソッドを受け取るためのメソッドを追加する。
@RequestMapping(value = "/mav", method = RequestMethod.GET) public ModelAndView mav() { ModelAndView modelAndView = new ModelAndView("mav"); modelAndView.addObject("title", "ModelAndView sample"); modelAndView.addObject("message", "これはModelAndViewのテストです。"); return modelAndView; }
ModelAndViewとフォーム送信
テキストp.196に従ってjspにフォームを追加する。
<body> <h1>${title}</h1> <p>${message}</p> <form method="post" action="/SpringMyApp/mav"> <input type="text" name="input1" /> <input type="submit" > </form> </body>
/mav に対するGETメソッドのためのメソッドを追加する。
@RequestMapping(value = "/mav", method = RequestMethod.GET) public ModelAndView mav() { ModelAndView modelAndView = new ModelAndView("mav"); modelAndView.addObject("title", "ModelAndView sample"); modelAndView.addObject("message", "これはModelAndViewのテストです。"); return modelAndView; }
/mav にアクセスすると入力フォームが表示される。
データを送信すると405エラーになる。POST not supported なので、POSTメソッドのためのコードをコントローラに追加する。
@RequestMapping(value = "/mav", method = RequestMethod.POST) public ModelAndView mavform(@RequestParam("input1") String input1) { ModelAndView modelAndView = new ModelAndView("mav"); modelAndView.addObject("title", "ModelAndView sample"); modelAndView.addObject("message", "typed: " + input1); return modelAndView; }
これで入力された文字列を表示できる。