2012-07-20 4 views
0

한 번에 X 레코드 만 표시하는 클라이언트 쪽 페이징 유형을 구현하려고 시도 중입니다. 클라이언트가 다음 X 레코드 등을 표시 할 때 더 많은 데이터를보고 싶을 때. 이렇게하려면 세션 변수를 사용하려고 시도하지만 값을 검사 할 때마다 비어 있습니다. 정말 그래서 어떤 도움을 주시면 감사하겠습니다 스프링 MVC에 대한 모든 것을 잘 모른다 : 여기Spring 3 MVC 세션 변수의 수명은 얼마나됩니까?

@Controller 
@RequestMapping(value = "/api/rest/da") 
@SessionAttributes({"sessionRowKey"}) 
public class DAController { 
    /** 
    * Default constructor for DAController 
    */ 
    public DAController() { 
    } 

    /** 
    * Initialize the SessionAttribute 'sessionRowKey' if it does not exist 
    */ 
    @ModelAttribute("sessionRowKey") 
    public String createSessionRowKey() 
    { 
     return ""; 
    } 

내가 확인 값이 비어 있으면 나는 그것이, 값 설정 초기화 바로 이것이다 :

@RequestMapping(value = "/getModelData/{namespace}/{type}/{rowkey:[^\\.]*\\.[^\\.]*}", method = RequestMethod.GET) 
public 
@ResponseBody 
Map<String, Map<String, String>> getModelData(String namespace, 
               String type, 
               String rowkey, 
               @ModelAttribute("sessionRowKey") String sessionRowKey, 
               HttpServletRequest request) 
{ 
    try 
    { 
     ModelType modelType = ModelType.fromString(type); 
     Model model; 
     if(modelType == ModelType.STATISTICAL) //page the data 
     { 
      //code abstracted 
      List<KeyValue> records = results_arr[30].list(); 

      if(sessionRowKey.equals("")) 
      { 
       model = modelReader.readModel(namespace, modelType, rowkey); 
       request.getSession().setAttribute("sessionRowKey", records.get(0).toString()); 
      } 
      else model = modelReader.readModel(namespace, modelType, sessionRowKey); 
     } 
     else 
     { 
      model = modelReader.readModel(namespace, modelType, rowkey); 
     } 
    } 
    catch (Exception e) 
    { 
     logger.log(Level.ERROR, "Error in DAController.getModelData: " + e.getMessage()); 
    } 
} 

세션 변수를 검사 할 때마다 항상 세션 변수가 얼마나 오래 지속됩니까?

+0

문제는 세션 변수의 수명과 관련이 없습니다. 일부 로깅을 추가하여 언제 어떤 메소드가 호출되는지 확인하십시오. 커프에서 createSessionRowKey()가 여러 번 호출되는지 궁금합니다. – DwB

+0

예, createSessionRowKey() 메소드를 제거하고 sessionRowKey.equals ("") 대신 (sessionRowKey == null)을 확인하십시오. –

+0

@DwB 좋은 생각입니다. 로그 문을 추가했고 메서드는 한 번만 실행되었습니다. –

답변

1

@ModelAttribute 주석 매개 변수 (sessionRowKey) 대신 HttpSession 매개 변수를 사용하고이 매개 변수를 사용하여 sessionRowKey를 가져옵니다. 예 :

... HttpSession httpSession, ... 

... 

String sessionRowKey = (String)httpSession.getAttribute("sessionRowKey"); 

... 

참고 : 위의 내용은 Java EE 5 이상을위한 것입니다. J2EE 1.4 이상에서는 HttpSession.getValue 및 HttpSession.setValue 메소드를 사용하십시오.

+0

고마워!, 나는이 기회를 포기하고 다시 게시, 나는 전에이 방법을 본 hadnt. –

+0

httpSession.setAttribute ("sessionRowKey", someValue)를 사용하여 세션 속성을 설정해야합니다. – DwB

+0

이것은 일을 끝내 셨습니다, 고마워요. –