◆ 무한한 가능성/& JSP

[JSP] select option 연도선택(년도선택, 연도출력, 연도설정) 동적으로 받아오기, JAVA 사용

치로로 2013. 6. 4. 13:38

[JSP] JAVA: select option 연도선택(년도선택, 연도출력, 연도설정) 동적으로 받아오기

 - get a year-value automatically.


/////////////////////////////////////////////////////////////////////////////

// - ※ 본 소스는 개인적인 용도로 작성 된 것이므로, 오류가 있을 수 있으니...

// - ...본 포스팅을 읽으시는 분들께서는 이 점 숙지 하시기 바랍니다. 감사합니다.

/////////////////////////////////////////////////////////////////////////////



JAVA를 이용하여  JSP에서 사용하는

Select 박스의 Option 에 연도선택을 동적으로 받아오자.




1. 연도선택 받아오기 - JAVA(JSP 페이지 내의)


<%@ page import="java.util.*" %>

 

<%

int year, yearMin, yearMax;

int yearResult, yearCount;

int month;

int date;

List<String> yearList = new ArrayList<String>();

Calendar now = Calendar.getInstance();


year = now.get(Calendar.YEAR);

month = now.get(Calendar.MONTH)+1;

date = now.get(Calendar.DATE);


yearMin = 2010; // 최소 연도는 '2010'

yearMax = now.get(Calendar.YEAR)+1; // 최대 연도는 '현재연도 + 1'

yearCount = yearMax - yearMin;


// 연도선택을 위한 연도출력을 동적으로 변경

/*

System.out.println("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++");

System.out.println("now.get(Calendar.YEAR): " + now.get(Calendar.YEAR));

System.out.println("yearMin: " + yearMin + ", yearMax: " + yearMax + ", yearCount: " + yearCount);

System.out.println("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++");


 yearResult = yearMin;

for(int i=0; yearResult<=yearMax; i++) { // yearList에 뿌려줄 연도 범위 구해서 넣기

System.out.println("String.valueOf(yearResult): " + String.valueOf(yearResult));

yearList.add(String.valueOf(yearResult++));

}

*/

%>




2-1. 정적으로 하드코딩된 연도선택 - JSP


<select id="Sel-01-Year" title="연도 선택">

<option value="">연도선택</option>

<option value="2010" <% if(year == 2010){%>selected="selected"<%} %> >2010</option>

<option value="2011" <% if(year == 2011){%>selected="selected"<%} %> >2011</option>

<option value="2012" <% if(year == 2012){%>selected="selected"<%} %> >2012</option>

<option value="2013" <% if(year == 2013){%>selected="selected"<%} %> >2013</option>

<option value="2014" <% if(year == 2014){%>selected="selected"<%} %> >2014</option>

</select>





2-2. 동적으로 받아오는 연도선택 - JSP


<select id="Sel-01-Year" title="연도 선택">

<option value="">연도선택</option>

<%-- // 연도선택을 위한 연도출력을 동적으로 변경 --%>

<%yearResult = yearMin;

for(int i=0; yearResult<=yearMax; i++) { %>

<option value=<%=yearResult%> <% if(year == yearResult){%>selected="selected"<%} %> ><%=yearResult%></option>

<%String.valueOf(yearResult++); } %>

</select>