簡體   English   中英

Jersey Rest Web服務重定向到同一頁面

[英]Jersey Rest webservice redirecting to the same page

我有一個簡單的rest網絡服務,將用於加載頁面。 頁面加載后,將顯示同一頁面,並顯示確認消息或錯誤消息。

我正在使用下面的代碼來加載它....

jsp頁面:-

<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是程序名稱。

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

但我希望它成為

http://localhost:8080/RESTFileUpload/

我對MVC功能“澤西島”了解不多(或幾乎不了解),但是另一種選擇是僅使用重定向。 您可以簡單地使用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();

    }
}

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM