Jersey Rest webservice重定向到同一页面 [英] Jersey Rest webservice redirecting to the same page

查看:166
本文介绍了Jersey Rest webservice重定向到同一页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a simple rest webservice that will be used to load a page. Once the page is loaded the same page will be displayed with a confirmation msg or error msg being displayed.

我正在使用以下代码加载它....

Im using the using the below code to load it ....

jsp页面: -

jsp page:-

<form action="rest/file/upload" method="post"
    enctype="multipart/form-data">

    <br> <label>Username: &nbsp&nbsp&nbsp</label><input type="text"
        placeholder="Username" name="username"> <br> <br>
    <label>Password:&nbsp&nbsp&nbsp&nbsp&nbsp</label><input type="text"
        placeholder="Password" name="password"> <br> <br>

    <hr>
    <p>
        Select a file : <input type="file" name="file" size="45" />
    </p>
    <br> <input id="submit" type="submit" value="Upload" />
    <c:out value="${obj}" />
    <!-- ${obj} -->
</form>

控制器

@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

<servlet-mapping>
        <servlet-name>jersey-serlvet</servlet-name>
        <url-pattern>/rest/*</url-pattern>
    </servlet-mapping>

一旦我点击表格,文件就会被加载并返回到index.jsp页面,但是页面的位置设置为。 RESTFileUpload是程序名称。

Once I click the form the file is loaded and it takes back to the index.jsp page, but the location of the page is set to. RESTFileUpload is the program name.

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

但我希望它是

http://localhost:8080/RESTFileUpload/


推荐答案

我不太了解关于MVC功能泽西的(或者说真的任何东西),但另一种选择是使用重定向。你可以简单地使用 Response.seeOther(URI) 。这将使用<$发送303 See Other c $ c>位置标题。浏览器应该发送此页面的另一个请求。该方法可能类似于

I don't know much (or really anything) about the MVC features Jersey, but another option is to just use a redirect. You can simply use Response.seeOther(URI). This will send out a "303 See Other" with the Location header. The browser should send another request for this page. The method might look something like

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 路径)

This will redirect to /contextPath/index.jsp (or in other words the index.jsp path located in the webapp root)

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

As an aside, if you are familiar with Javascript/jQuery at all, there are other file upload options that don't involve the redirect.

只是为了表明这个工作正常

Just to show that this works fine

@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();

    }
}

这篇关于Jersey Rest webservice重定向到同一页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆