使用restful方法重定向到页面?

Mat*_* Kh 10 java rest redirect

我创建了一个页面,要求用户填写一些表单字段,当他提交时,表单将被发送到Restful方法,您可以在下面看到:

@POST
@Path("addUser")
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
public void addUser(@FormParam("username") String username,
        @FormParam("password") String password,
        @FormParam("id") String id,
        @FormParam("group_name") String groupName,
        @FormParam("authority_name") String authorityName,
        @FormParam("authority_id") String authorityId
        )
{
    //Something will be done here
}
Run Code Online (Sandbox Code Playgroud)


如何在此函数结束时将用户重定向到(比方说)index.jsp?

小智 14

像这样更改你的代码,addUser()应该返回一个响应对象

public Response addUser(@FormParam("username") String username,
        @FormParam("password") String password,
        @FormParam("id") String id,
        @FormParam("group_name") String groupName,
        @FormParam("authority_name") String authorityName,
        @FormParam("authority_id") String authorityId
        )
{
    //Something will be done here

    java.net.URI location = new java.net.URI("../index.jsp?msg=A_User_Added");
    return Response.temporaryRedirect(location).build()

}
Run Code Online (Sandbox Code Playgroud)


ser*_*lva 10

使用javax.ws.rs.core.UriBuilder该URI创建URI ,映射您要保留的参数和其他数据.然后使用Response.temporaryRedirect返回重定向到客户端并将其传递给您构建的URI.