note

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

JSP/Spring

뷰에 모델 데이터 전달하기

투한 2012. 3. 5. 12:42



















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














dispatcher-servlet.xml
<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context-3.0.xsd">


	
	<!-- 뷰에 모델 데이터 전달하기 -->
	<bean class="madvirus.spring.chap06.controller.GameSearchController" />
	<bean id="searchService" class="madvirus.spring.chap06.service.SearchService" />
	
	
	<!-- View 글로벌 설정 -->
	<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/WEB-INF/view/" />
		<property name="suffix" value=".jsp" />
	</bean>
</beans>





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<SearchType> referenceSearchTypeList(){
		List<SearchType> options = new ArrayList<SearchType>();
		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" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>게임 검새ㅐㄱ 메인</title>
</head>
<body>
인기 키워드 : <c:forEach var="popularQuery" items="${popularQueryList}">${popularQuery}</c:forEach>
<form action="game.do">
<select name="type">
	<c:forEach var="searchType" items="${searchTypeList}">
	<option value="${searchType.code}">${searchType.text}</option>
	</c:forEach>
</select>
<input type="text" name="query" value=""/>
<input type="submit" value="검색"/>
</form>
</body>
</html>


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







search/game.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>게임 검색 결과</title>
</head>
<body>
	인기 키워드 :
	<c:forEach var="popularQuery" items="${popularQueryList}">${popularQuery}</c:forEach>
	<form action="game.do">
		<select name="type">
			<c:forEach var="searchType" items="${searchTypeList}">
				<option value="${searchType.code}"
					<c:if test=${command.type==searchType.code}">selected</c:if>>
					${searchType.text}</option>
			</c:forEach>
		</select> 
		<input type="text" name="query" value="${command.query}" /> 
		<input type="submit" value="검색" />
	</form>
	검색 결과 :${search.Result}
</body>
</html>