Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | 7 |
8 | 9 | 10 | 11 | 12 | 13 | 14 |
15 | 16 | 17 | 18 | 19 | 20 | 21 |
22 | 23 | 24 | 25 | 26 | 27 | 28 |
29 | 30 | 31 |
Tags
- JSP
- 메소드
- 전화걸기
- 생성자
- 클래스
- mybatis
- 국제화
- 이클립스
- Java
- Eclips
- layout
- struts2
- 메서드
- Menu
- 배열
- AWT
- paint
- 에러페이지
- 안드로이드
- 오버로딩
- 기본
- HTML
- oracle
- Graphic
- Android
- OGNL
- 예외처리
- Spring
- JavaScript
- 어노테이션
Archives
- Today
- Total
note
뷰에 모델 데이터 전달하기 본문
@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>
'JSP > Spring' 카테고리의 다른 글
Spring 요청 URI 매칭 (0) | 2012.03.05 |
---|---|
Spring 유효성 체크 (0) | 2012.03.05 |
Spring 컨트롤러 메서드의 리턴 타입 (0) | 2012.03.05 |
@RequestHeader 어노테이션을 이용한 헤더 매핑 (0) | 2012.03.05 |
@CookieValue 어노테이션을 이용한 쿠키 매핑 (0) | 2012.03.05 |