basic/spring

spring framework 03 [실습] 파라미터 수집, view로 데이터 보내기, 의존성주입

못지(Motji) 2021. 8. 2. 23:15

❕  Controller의 파라미터 수집

넘어오는 파라미터의 이름과 매개변수의 이름값이 동일해야 바인딩이 잘됨

원하는 데이터 타입으로 지정

DTO, 배열, List 등 으로도 받을 수 있음

@RequestMapping("hello.ex")
	public String hello(String id, int pw) {
    	// .../hello.ex?id=java&pw=1234 요청
		System.out.println("id : " + id);
		System.out.println("pw : " + (pw+1));
		
		return "/WEB-INF/views/spring02/hello.jsp";
	}

💻 console 출력

❕ DTO 타입으로 파라미터 수집

@RequestMapping("hello1.ex")
	public String hello1(TestDTO dto) {
    	// .../hello1.ex?id=jsp&pw=0707 요청
		System.out.println("dto.id : " + dto.getId());
		System.out.println("dto.pw : " + dto.getPw());
		
		return "/WEB-INF/views/spring02/hello.jsp";
	}

💻 console 출력

❕ 배열 타입으로 파라미터 수집

@RequestMapping("hello11.ex")
	public String hello1(String[] ids) {
    	// .../hello11.ex?ids=jsp&ids=0707 요청
		System.out.println(ids);
		System.out.println("ids : " + Arrays.toString(ids));
		
		return "/WEB-INF/views/spring02/hello.jsp";
	}

💻 console 출력

❕ Controller 메소드에서 Model 전달자로 view에 데이터 전달하기

@RequestMapping("hello2.ex")
	public String hello2(String id, int pw, Model model) {
    	// .../hello2.ex?id=hulk&pw=9981
		System.out.println(id);
		System.out.println(pw);
		model.addAttribute("id", id);
		model.addAttribute("pw", pw);
		
		SampleDTO dto = new SampleDTO();
		dto.setId(id);
		dto.setPw(pw);
		System.out.println(dto.getId());
		model.addAttribute("dto",dto);

		return "/WEB-INF/views/spring02/hello.jsp";
	}

❕ jsp 페이지 코드

<h1> hello page </h1>
<h3> id : ${id}</h3>
<h3> pw : ${pw}</h3>
<h3> dto.id : ${dto.id}</h3>
<h3> dto.pw : ${dto.pw}</h3>

 

💻 웹문서 출력

@Autowired 의존성 자동 주입 어노테이션 활용해보기

▹xml 설정파일에 <bean> 태그로 객체 생성할것 작성

<bean id="tvBean" class="test.spring.bean.TvBean"/>
<bean id="day" class="java.util.Date" />

▹객체 생성할 변수위에 @Autowired 작성

@Autowired
TvBean tvBean = null;
@Autowired
Date day = null;

@RequestMapping("hello4.ex")
    public String hello4() {
    System.out.println(tvBean);
    System.out.println(day);
    tvBean.func();

    return "/WEB-INF/views/spring02/hello.jsp";
}

💻 console출력

<property> 태그 사용하여 bean 태그 사용할때 값 넣어주기

▹xml 설정파일에 태그로 객체 생성할것 작성하면서 값도 넣어주기

 

<bean id="day" class="java.util.Date" />
<bean id="tvBean" class="test.spring.bean.TvBean" >
    <property name="power" value="false" />
    <property name="color" value="black" />
    <property name="ch" value="22" />
    <property name="ref" ref="day" />
</bean>

▹Controller 메소드 작성

@Autowired
TvBean tvBean = null;
    
@RequestMapping("hello5.ex")
public void hello5() {
    System.out.println(tvBean.getPower());
    System.out.println(tvBean.getColor());
    System.out.println(tvBean.getCh());
    System.out.println(tvBean.getRef());
}

💻 console 출력

▹ 요청 url .../hello5.ex

<constructor-arg> 태그 사용하여 매개변수 있는 생성자 호출해보기

▹xml 설정파일에 태그작성

<bean id="day" class="java.util.Date" />
<bean id="tvBean" class="test.spring.bean.TvBean">
    <constructor-arg index="0" value="true" />
    <constructor-arg index="1" value="12" />
    <constructor-arg index="2" value="yellow" />
    <constructor-arg index="3" ref="day" />
</bean>

▹Controller 메소드 작성

@Autowired
TvBean tvBean = null;

@RequestMapping("hello6.ex")
public void hello6() {
    System.out.println("power : " + tvBean.getPower());
    System.out.println("color : " + tvBean.getColor());
    System.out.println("ch : " + tvBean.getCh());
    System.out.println("ref : " + tvBean.getRef());
}

💻 console 출력

▹ 요청 url .../hello6.ex

@RequestMapping 어노테이션 사용해보기

▹ Controller 메소드 코드

// value로 경로지정, 여러경로 지정하고 싶으면 배열에 담으면됨 {}사용
// params 넘어와야할 파라미터 값 설정, id/pw 값 안넘어오면 error 뜸
// method 전달방식 설정
@RequestMapping(value="hello7.ex", params = {"id", "pw"}, method=RequestMethod.GET)
public String hello5(TestDTO dto, Model modle) {
    modle.addAttribute("dto", dto);

    return "/WEB-INF/views/spring02/hello2.jsp";
}

▹ view page 태그

<h3> id : ${dto.id}</h3>
<h3> pw : ${dto.pw}</h3>

💻 웹문서 출력

▹ 요청 url : .../hello7.ex?id=java&pw=1234

@RequestParam 어노테이션 사용해보기

// 경로 매핑
@RequestMapping("hello8.ex")
// value는 넘어올 파라미터 이름지정, defaultValue는 값 안넘어올시 자동으로 들어갈 데이터
// required 필수로 넘어와야하는 파라미터 값
public void hello6(
    @RequestParam(value="hoho", defaultValue="java") String id, 
    @RequestParam(required = true) String pw) {
    
    System.out.println("id : " + id);
    System.out.println("pw : " + pw);
}

💻 console 출력

▹ ../hello8.ex?pw=0505 요청

▹ pw값 안넘어오면 error

@ResponseBody 어노테이션 사용해보기

@ResponseBody
@RequestMapping("hello9.ex")
public String hello7() {

    return "end!@!@!@";
}

💻 웹문서 출력