トッカンソフトウェア

Spring MVC コントローラ

今回はコントローラをやります。


以前のサンプルの説明

				
package spring.test;

import static org.springframework.web.bind.annotation.RequestMethod.GET;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class TestController {

	@RequestMapping(value = "/", method = GET)
	public String show() {
		return "test";
	}
}


			
クラスの頭に@Controllerを付け、Spring定義ファイルのcomponent-scanでパッケージを指定すると、そのクラスはコントローラとして動作します。
コントローラはブラウザなどクライアントからのリクエストを受付け、サービスを呼び出し、結果をクライアントに返します。

コントローラは複数作成されますが、どのコントローラが呼ばれるかは@RequestMappingで指定した条件と一致したものになります。
@RequestMappingが指定されたメソッドの戻り値がJspファイルまたはダウンロードデータになります。

@RequestMappingについて

@RequestMappingはクラスまたはメソッドの頭に付けます。@RequestMappingには色々な条件を付けられますが、今回はvalue、method、paramasをやります。

value

URLの指定になります。以下はそれぞれ
http://localhost:8080/springMVC/
http://localhost:8080/springMVC/test/
http://localhost:8080/springMVC/test/test.html
でアクセスされます。
				
	@RequestMapping(value = "/", method = GET)
	public String show() {
		System.out.println("スラッシュのみ");
		return "test";
	}

	@RequestMapping(value = "/test/", method = GET)
	public String showGet() {
		System.out.println("ディレクトリ指定");
		return "test";
	}

	@RequestMapping(value = "/test/test.html", method = GET)
	public String showHtml() {
		System.out.println("html指定");
		return "test";
	}
	
			

method

クライアントからGET、POSTのどちらで呼ばれたの指定です。
下の例ではクラスでURLを指定し、メソッドでGET、POSTを指定しています。
				
package spring.test;

import static org.springframework.web.bind.annotation.RequestMethod.GET;
import static org.springframework.web.bind.annotation.RequestMethod.POST;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping(value = "/test")
public class TestController {

	@RequestMapping(method = GET)
	public String showGet() {
		System.out.println("GETで呼ばれた");
		return "test";
	}

	@RequestMapping(method = POST)
	public String showPost() {
		System.out.println("POSTで呼ばれた");
		return "test";
	}

}


			

params

GET、POSTに渡すパラメータでも振り分けられます。

以下のURLでアクセスすると以下のメソッドが呼び出されます(今回はGETで例を書きますが、POSTでも動作します)。
URL 呼び出されるメソッド
http://localhost:8080/springMVC/test?xxx showGet0()
http://localhost:8080/springMVC/test?xxx=123
http://localhost:8080/springMVC/test?args=abc showGet1()
http://localhost:8080/springMVC/test?args=123 showGet2()
http://localhost:8080/springMVC/test showGet3()
				
package spring.test;

import static org.springframework.web.bind.annotation.RequestMethod.GET;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping(value = "/test", method = GET)
public class TestController {

	@RequestMapping(params = "xxx")
	public String showGet0() {
		System.out.println("GET0が呼ばれた");
		return "test";
	}

	@RequestMapping(params = "args=abc")
	public String showGet1() {
		System.out.println("GET1が呼ばれた");
		return "test";
	}

	@RequestMapping(params = "args=123")
	public String showGet2() {
		System.out.println("GET2が呼ばれた");
		return "test";
	}

	@RequestMapping()
	public String showGet3() {
		System.out.println("GET3が呼ばれた");
		return "test";
	}
}


			

値の受け取り

コントローラはクライアントから色々な方式で値を受け取ることができます。

URLの指定位置を値として取得(@PathVariable)

				
package spring.test;

import static org.springframework.web.bind.annotation.RequestMethod.GET;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping(method = GET)
public class TestController {

	@RequestMapping(value = "/test/{arg}")
	public String showArg(@PathVariable("arg") String id) {
		System.out.println("引数:" + id);
		return "test";
	}

	@RequestMapping(value = "/test/{arg1}/{arg2}")
	public String showArg2(@PathVariable("arg1") String id1, @PathVariable("arg2") String id2) {
		System.out.println("引数:" + id1 + "," + id2);
		return "test";
	}
}
			
URLの指定位置を値として受け取ることが出来ます。{・・・}で位置を指定し、@PathVariableアノテーションで値を受け取ります。

上の例では http//localhost:8080/springMVC/test/abc でアクセスしたときにshowArgメソッドが呼ばれ、引数idに abc がセットされます。
http//localhost:8080/springMVC/test/abc/def でアクセスしたときにshowArg2メソッドが呼ばれ、引数id1に abc、引数id2に def がセットされます。

パラメータの取得(@RequestParam)

				
package spring.test;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
public class TestController {

	@RequestMapping(value = "/test")
	public String showArg(@RequestParam("arg1") String id1,
			@RequestParam(value = "arg2", required = false, defaultValue = "aaa") String id2) {
		System.out.println("引数:" + id1 + "," + id2);
		return "test";
	}

}


			
パラメータ名を指定して受け取ります。Getの場合、URLパラメータから受け取り、Postの場合、フォームの項目名から受け取ります。

arg1のようにパラメータだけ指定することやarg2のように必須/任意やデフォルト値を設定することもできます。arg1のようにパラメータだけ
指定した場合は、必須になるので、指定されない可能性がある場合、arg2のように任意かデフォルト値をセットさせて下さい。

HttpServletRequestの使用

				
package spring.test;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class TestController {

	@RequestMapping(value = "/test")
	public String showArg(HttpServletRequest request) {
		System.out.println("引数:" + request.getParameter("arg"));
		return "test";
	}
}


			

通常のServletと同じようにHttpServletRequestからパラメータを取得することもできます。


ページのトップへ戻る