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:    </label><input type="text"
placeholder="Username" name="username"> <br> <br>
<label>Password:     </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/
我对 Jersey 的 MVC 功能知之甚少(或者真的一无所知),但另一种选择是仅使用重定向。您可以简单地使用 Response.seeOther(URI)
. This will send out a "303 See Other" 和 Location
header。浏览器应该为这个页面发送另一个请求。该方法可能类似于
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,还有其他 file upload options 不涉及重定向。
更新
只是为了证明这工作正常
@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();
}
}
我有一个简单的 rest 网络服务,将用于加载页面。加载页面后,将显示同一页面,并显示确认消息或错误消息。
我使用下面的代码加载它....
jsp 页面:-
<form action="rest/file/upload" method="post"
enctype="multipart/form-data">
<br> <label>Username:    </label><input type="text"
placeholder="Username" name="username"> <br> <br>
<label>Password:     </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/
我对 Jersey 的 MVC 功能知之甚少(或者真的一无所知),但另一种选择是仅使用重定向。您可以简单地使用 Response.seeOther(URI)
. This will send out a "303 See Other" 和 Location
header。浏览器应该为这个页面发送另一个请求。该方法可能类似于
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,还有其他 file upload options 不涉及重定向。
更新
只是为了证明这工作正常
@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();
}
}