//검색리스트 세션저장
HttpSession session = request.getSession();
if(session.getAttribute("result_".concat(fileName)) != null){
request.setAttribute("plantList", session.getAttribute("result_".concat(fileName)));
}
else{
어쩌고 저쩌고 소스
// 검색결과 세션저장...
session.setAttribute("result_".concat(fileName),articleList);
}
해결
지금 하려는게
1. 사용자가 식물 이미지 검색을 한 후 검색결과 리스트 10개를 보여준다.
2. 검색결과 리스트의 상세 이미지를 보고 난 후에 다시 뒤로 가기 버튼을 누른다.
3. 뒤로 가기를 하면 이전에 검색했던 검색결과 리스트 10개를 보여준다.
인데.
아래 소스는 검색결과까지는 가지고 다시 검색하는 소스코드 부분에 왔다.
그런데,검색결과 리스트가 문자열 형태라 잘라서 배열에 넣어서 arraylist에 넣었다.
이 새로 만든 arraylist가 검색이 안된다.
어째서 일까.
ArrayList<plant> articleList = new ArrayList<plant>();
ArrayList<String> articleList2 = new ArrayList<String>();
String plantList = request.getParameter("getplantList");///////////////////**********
System.out.println("plantList************ "+ plantList);
if(plantList != null){
// 앞뒤 [ ] 잘라야하나 어차피 articleList2출력해보면 또 붙어있음.
//2개씩있음 잘라야 하나봄
String plantList_split = plantList.replaceAll("\\[" , "");
System.out.println("plantList_split-------------"+plantList_split);
String plantList_split2 = plantList_split.replaceAll("\\]", "");
System.out.println("plantList_split2-------------"+plantList_split2);
String[] array = plantList_split2.split(",");
//String[] array = plantList.split(",");
for(int i=0; i < array.length; i++){
System.out.format("array[%d] = %s%n", i,array[i]);
}
Collections.addAll(articleList2, array);
for (int i = 0; i < array.length; i++) {
System.out.format("articleList2[%d] = %s%n", i, articleList2.get(i));
}
//새로 검색하지 않고 검색리스트 보기가 가능해지면 ?
request.setAttribute("plantList", articleList2);
System.out.println("13****articleList2*****" + articleList2);
//return "plantlistdata.jsp";
}
/*
ArrayList<plant> articleList = new ArrayList<plant>(); // articleList 밑에 선언되어 있는거 주석하고 여기다 씀
// ArrayList<plant> articleList2 = new ArrayList<plant>();
// ArrayList<String> articleList2 = new ArrayList<String>();
//<String>형으로 임시로 만듦.
String plantList = request.getParameter("getplantList");//처음 검색결과 값 다시 받은 것(string형)
// plant plant_plantList = plant.class.cast(plantList);
System.out.println("plantList************ "+ plantList);
// System.out.println("plant_plantList************ "+ plant_plantList);
if(plantList != null){ //이전에 검색한 결과가 있으면
// 앞뒤 [ ] 잘라야하나?
// 어차피 articleList2출력해보면 또 붙어있음.
//2개씩있음. 잘라야 하나봄
String plantList_split = plantList.replaceAll("\\[" , "");
System.out.println("plantList_split-------------"+plantList_split);
String plantList_split2 = plantList_split.replaceAll("\\]", "");
System.out.println("plantList_split2-------------"+plantList_split2);
// 문자열의 앞뒤에 대괄호 지움.
String[] array = plantList_split2.split(",");
//String[] array = plantList.split(",");
for(int i=0; i < array.length; i++){
System.out.format("array[%d] = %s%n", i,array[i]);
System.out.println(array[i]);
} //잘 잘라졌나 확인.
//Collections.addAll(articleList, array); //array를 arraylist로 변환?
//java.util.Collections도 안됨. // 그냥 add안됨.
/*for (int i = 0; i < array.length; i++) {
System.out.format("articleList2[%d] = %s%n", i, array[i]);
}*/ // 잘 됐나 확인.
//새로 검색하지 않고 검색리스트 보기가 가능해지면 ?
request.setAttribute("plantList", array); //다시 검색할 값을 보낸다. plantlistdata.jsp에
System.out.println("13****articleList2*****" + articleList);
//return "plantlistdata.jsp";
//1.어떻게 하면 articleList2를 가지고 검색이 될까
//2. articleList2가 ArrayList<plant> 가 아니라서 검색이 안되는걸까?
//3.밑에 검색 소스를 안타게 하던지..바로 return되어야 할 것 같은데,,
//4.articleList2는 왜 <plant>가 아닌 <String>형태여야 하는가. - 뭘알아봐야 답을 찾지?
//5.새로 검색을 하면 새로운 값이 들어가게?
//6. 같은 값을 보내면 다시 검색이 되는 건 맞나?
}
*/
//1.어떻게 하면 articleList2를 가지고 검색이 될까
//2. articleList2가 ArrayList<plant> 가 아니라서 검색이 안되는걸까?
//3.밑에 검색 소스를 안타게 하던지..바로 return되어야 할 것 같은데,,
//4.articleList2는 왜 <plant>가 아닌 <String>형태여야 하는가.
*addAll() 메소드의 매개변수의 Collection<? extends E> 중 '? extends E'가 의미하는 것은
요소 클래스형 E를 상속받는 모든 클래스라는 의미다.
예를 들어 String의 경우 String을 상속받는 모든 클래스로 이뤄진 Collection객제를 매개변수로 받을 수 있다.
-> 그렇다면 array를...제네릭형으로 만들면 된다는 것인가?
http://ojava.tistory.com/32
배열이 List로 대변되는 Generic과 어떤 점에서 다른가?
1. 배열은 공변 , 제네릭은 불변
- 배열을 사용하여 에러가 발생하는 경우는 런타임 시 그 내용을 알 수 있고, Generic을 사용해 에러가 발생하는 경우는 컴파일 시 내용을 알 수 있다.
2. 배열은 구체적이고 Generic은 비구체적이다.
- 구체화와 비구체화의 차이는 컴파일 시보다 런타임시에 어떤 것이 더 많은 정보를 가지느냐는 문제아다. 따라서 런타임 시 더 많은 정보를 가지는 배열이 구체적 , 컴파일 시 더 많은 정보를 가지는 Generic은 비구체적
===> 배열과 Generic은 위의 두가지의 차이점을 가지기 때문에 혼용해서 사용되지 않습니다.
new List<E>[] , new List<String>[] 과 같이 둘을 혼용한 것을 '제네릭 배열'아라고 부르며,
이런 선언을 하게 되면 '제네릭 배열 생성 에러'가 발생한다.
생성되지 않는 이유는 배열을 런타임시타입안전을 보장하고 컴파일시에는 보장하지않지만
제네릭타입은 컴파이시 타입안전을 보장하고 런타임시에 보장하지 않는 정 반대의 성질을 지녔기 때문에,
이러한 타입 안전상의 문제 때문에 제네릭과 배열을 결합한 형태를 사용하지 않는다.
(그러나 언바운드 와일드 카드 (<?>))은 제네릭 형태중에서도 구체화 타입이므로 배열 생성이 가능하다.