【发布时间】:2015-08-24 04:19:25
【问题描述】:
我只是想发送一个新的 .jsp 页面,由客户端使用 URIBuilder 呈现。所以我有一个 main.js 向调用 logIn() 的服务器发送一个 POST。现在我只想将一个新的 .jsp 文件发送到客户端进行渲染。到目前为止没有任何反应,我尝试使用不同的文件路径 - 在我只是使用“Feed.jsp”作为文件路径之前。
我觉得还有更多我没有理解的内容。
这是我的 main.js 文件。它通过 logIn() 方法成功地向服务器发送 POST。这个 main.js 被我的 index.jsp 文件成功使用了。
var rootURL = "http://localhost:8080/messenger/webapi";
$(function() {
$('#btnRegister').click(function() {
var username = $('#username').val();
registerProfile();
});
$('#btnSignIn').click(function() {
logIn();
});
function logIn() {
var profileJSON = formToJSON();
$.ajax({
type: 'POST',
contentType: 'application/json',
url: rootURL + "/profiles/logIn",
dataType: "json",
data: profileJSON,
success: (function(data){
alert("Success!");
})
});
}
function registerProfile() {
var profileJSON = formToJSON();
$.ajax({
type : 'POST',
contentType: 'application/json',
url: rootURL + "/profiles",
dataType: "json",
data: profileJSON,
success: (function() {
alert("Resgistered");
})
});
}
function formToJSON() {
return JSON.stringify({
"profileName": $('#username').val(),
"password" : $('#password').val(),
});
}
});
这是我在 ProfileResource.java 中调用的方法 LogIn()。它可以成功调用帖子,但由于某种原因,当我包含 UriBuilder 时,它什么也不做。
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@POST
@Path("/logIn")
public Response logIn( @Context ServletContext context) {
UriBuilder uriBuilder = UriBuilder.fromUri(URI.create(context.getContextPath()));
uriBuilder.path("Deployed Resources/webapp/Feed.jsp");
URI uri = uriBuilder.build();
return Response.seeOther(uri).build();
}
}
所以基本上我的 index.jsp 会呈现给客户端。我的“btnResgister”按钮可以满足它的需要,我的“btnSignIn”只是没有做任何事情,尽管我知道它可以很好地访问“配置文件/登录”资源。
更新
我已经使用用户 peeskillets UriUtils 类实现了这样的登录方法:
@POST
@Path("/logIn")
public Response logIn( @Context ServletContext context, @Context UriInfo uriInfo) {
URI contextPath = UriUtils.getFullServletContextPath(context, uriInfo);
UriBuilder uriBuilder = UriBuilder.fromUri(contextPath);
uriBuilder.path("Feed.jsp");
return Response.seeOther(uriBuilder.build()).build();
}
但POST 仍未完成。我想知道这是否与 ServletContext 或 UriInfo 参数有关...这些参数是在我 POST 时自动发送的,还是我必须使用 .js 从客户端发送更多信息?
这也是我的文件结构:
【问题讨论】:
标签: java rest jsp jersey jax-rs