0

我正在使用一种宁静的方法,我想将一个列表传递给一个 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来访问我想要的页面。(事实上​​,在使用 restful 方法时,我没有找到其他转发或重定向的方法)
这是我的 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("roles")
中一无所获, 这是什么问题?

4

1 回答 1

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 -->
<%}%>
于 2012-07-07T07:13:42.923 回答