Jersey Rest webservice重定向到同一页面

我有一个简单的restWeb服务,将用于加载页面。 加载页面后,将显示相同的页面,并显示确认消息或错误消息。

我使用下面的代码加载它….

jsp页面: –







Select a file :


调节器

 @Path("/file") public class FileUploadService { @POST @Path("/upload") @Consumes(MediaType.MULTIPART_FORM_DATA) public Viewable uploadFile( @Context HttpServletRequest request,@Context HttpServletResponse response, @FormDataParam("file") InputStream uploadedInputStream, @FormDataParam("file") FormDataContentDisposition fileDetail, @FormDataParam("username") String username, @FormDataParam("password") String password) throws NoSuchAlgorithmException, IOException, URISyntaxException { response.setHeader("Location", "/"); return new Viewable("/index.jsp",null); 

web.xml中

  jersey-serlvet /rest/*  

单击表单后,文件将被加载并返回到index.jsp页面,但页面的位置设置为。 RESTFileUpload是程序名称。

 http://localhost:8080/RESTFileUpload/rest/ 

但我希望它是

 http://localhost:8080/RESTFileUpload/ 

我不太了解(或者说真的有关)MVC MVfunction,但另一种选择是使用重定向。 您只需使用Response.seeOther(URI) 。 这将发送带有Location标题的“303 See Other” 。 浏览器应该发送此页面的另一个请求。 该方法可能看起来像

 public Response getRedirect(@Context ServletContext context) { UriBuilder builder = UriBuilder.fromPath(context.getContextPath()); builder.path("index.jsp"); return Response.seeOther(builder.build()).build(); } 

这将重定向到/contextPath/index.jsp (或者换句话说,位于webapp根目录中的index.jsp路径)

顺便说一句,如果你完全熟悉Javascript / jQuery,还有其他文件上传选项不涉及重定向。


UPDATE

只是为了表明这很好用

 @Path("/redirect") public class RedirectResource { @GET public Response getRedirect(@Context ServletContext context) { UriBuilder builder = UriBuilder.fromPath(context.getContextPath()); builder.path("index.html"); return Response.seeOther(builder.build()).build(); } } 

在此处输入图像描述