2012-07-04 30 views
0

我正在使用一個restful方法,我想將一個列表傳遞給一個jsp文件。
這裏是RESTful方法:
如何將一個列表轉發給一個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

我想你最好做到以下幾點:
寫您的RESTful方法如下:

@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中。好吧,因爲這不是安全的事情,那對我來說工作得很好。謝謝。 –

相關問題