note

뷰에 모델 데이터 전달하기 본문

JSP/Spring

뷰에 모델 데이터 전달하기

투한 2012. 3. 5. 12:42



















@ModelAttrubyte()
메소드 위에 명시만으로 JSP 에서 호출가능














dispatcher-servlet.xml





	
	
	
	
	
	
	
	
		
		
	






madvirus.spring.chap06.service/SearchCommand.java
package madvirus.spring.chap06.service;

public class SearchCommand {
	private String type;
	private String query;
	private int page;
	
	
	public String getType() {
		return type;
	}
	public void setType(String type) {
		this.type = type;
	}
	public String getQuery() {
		return query;
	}
	public void setQuery(String query) {
		this.query = query;
	}
	public int getPage() {
		return page;
	}
	public void setPage(int page) {
		this.page = page;
	}
}





madvirus.spring.chap06.service/SearchResult.java
package madvirus.spring.chap06.service;

public class SearchResult {

}





madvirus.spring.chap06.service/SearchService.java
package madvirus.spring.chap06.service;

public class SearchService {

	
	public SearchResult search(SearchCommand command){
		return new SearchResult();
	}
}





madvirus.spring.chap06.service/SearchType.java
package madvirus.spring.chap06.service;

public class SearchType {

	private int code;
	private String text;
	
	
	public SearchType(int code, String text) {
		super();
		this.code = code;
		this.text = text;
	}

	public int getCode() {
		return code;
	}
	public void setCode(int code) {
		this.code = code;
	}
	public String getText() {
		return text;
	}
	public void setText(String text) {
		this.text = text;
	}
}





madvirus.spring.chap06.controller/GameSearchController.java
package madvirus.spring.chap06.controller;

import java.util.ArrayList;
import java.util.List;


import madvirus.spring.chap06.service.SearchCommand;
import madvirus.spring.chap06.service.SearchResult;
import madvirus.spring.chap06.service.SearchService;
import madvirus.spring.chap06.service.SearchType;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;


@Controller
public class GameSearchController {
	@Autowired
	private SearchService searchService;

	@ModelAttribute("searchTypeList")
	public List referenceSearchTypeList(){
		List options = new ArrayList();
		options.add(new SearchType(1, "전체"));
		options.add(new SearchType(2, "아이템"));
		options.add(new SearchType(3, "캐릭터"));
		return options;
	}

	@ModelAttribute("popularQueryList")
	public String[] getpopularQueryList(){
		return new String[] {"게임","창천2","위메이드"};
	}

	@RequestMapping("/search/main.do")
	public String main() {
		return "search/main";
	}

	@RequestMapping("/search/game.do")
	public ModelAndView search(@ModelAttribute("command") SearchCommand command){
		ModelAndView  mav = new ModelAndView("search/game");

		System.out.println("검색어 = "+command.getQuery().toUpperCase());

		SearchResult result = searchService.search(command);

		mav.addObject("searchResult",result);
		return mav;
	}
	@ExceptionHandler(NullPointerException.class)
	public String handleNullPointException(NullPointerException ex){
		return "error/nullException";
	}
	public void setSearchService(SearchService searchService){
		this.searchService = searchService;
	}
}





search/main.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>




게임 검새ㅐㄱ 메인


인기 키워드 : ${popularQuery}


String 배열로 부터 index에 접근하여 데이터 빼기







search/game.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>




게임 검색 결과


	인기 키워드 :
	${popularQuery}
	
검색 결과 :${search.Result}