2012-07-04 2 views
0

나는 jsp 파일에리스트를 전달하고자하는 편안한 방법을 사용하고있다.
어떻게 List를 jsp 파일로 전달할 수 있습니까?

@Path("myRest") 
public void handleRequestInternal() throws Exception { 
    try { 
     Request req = new Request(); 
     List<String> roles = manager2.getAllRoles(); 
     req.setAttribute("roles", roles); 
     java.net.URI location = new java.net.URI("../index.jsp"); 
     throw new WebApplicationException(Response.temporaryRedirect(location).build()); 
    } catch (URISyntaxException e) { 
     e.printStackTrace(); 
    } 
} 


내가 내 원하는 페이지로 이동하기 위해 webApplicationException를 사용
다음은 편안한 방법입니다.
을 (사실 나는 전달하거나 편안한 방법을 사용하는 경우, 리디렉션하는 다른 방법을 찾을 수 없습니다) 그리고 여기 내 JSP 파일입니다

<% if(request.getAttribute("roles") != null){%> 
<%  Object obj = (Object)request.getAttribute("roles"); 
    List<String> roles = (List<String>) obj;%> 
     <select name="role"> 
    <%int size = roles.size();%> 
    <%for(int i = 0; i < size; i++){ %> 
     <option value = "<%= roles.get(i)%>"><%= roles.get(i)%> 
    <%} %> 
    </select> 
<%}%> 


하지만합니다 (request.getAttribute에서 아무것도 얻을 "역할")
무엇이 문제입니까?

답변

1

난 당신이 더 나은 다음을 수행 생각 다음과 같이
이 편안한 방법을 쓰기 :

@Path("myRest") 
public void handleRequestInternal() throws Exception { 
    try { 
     List<String> roles = manager2.getAllRoles(); 
     String role = new String(); 
     for(int i = 0; i < roles.size(); i++){ 
      if(i != roles.size() - 1) 
       role += roles.get(i) + '-'; 
      else 
       role += roles.get(i); 
     } 
     java.net.URI location = new java.net.URI("../index.jsp?roles=" + role); 
     throw new WebApplicationException(Response.temporaryRedirect(location).build()); 
    } catch (URISyntaxException e) { 
     e.printStackTrace(); 
    } 
} 


그리고 당신의 JSP 파일에

:

<% if(request.getParameter("roles") != null){%> 
<!-- process request.getParameter("roles") into an arraylist and you are good to go --> 
<%}%> 
+0

아, 당신은 넣어 의미 URL에 내 변수. 그것은 보안 문제가 아니므로, 저와 잘 작동합니다. 고맙습니다. –

관련 문제