【问题标题】:Rendering a new .jsp page using JAX-RS UriBuilder使用 JAX-RS UriBuilder 呈现新的 .jsp 页面
【发布时间】: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


    【解决方案1】:

    为this answer 道歉。 ServletContext.getContextPath() 似乎只返回相对路径而不是完整的 uri。在这种情况下,当 Jersey 找到一个相对 URI 时,它将从应用程序的基本 URI 构造一个完整的 URI。所以你总是会得到一个包含 Jersey 根路径的路径(永远不会通向 jsp 页面)。

    除了进行一些字符串操作外,我不知道如何从 Jersey 获取完整的上下文路径(完整 URI)。你可以使用

    public class UriUtils {
    
        public static URI getFullServletContextPath(ServletContext context, 
                                                    UriInfo uriInfo) {
    
            URI requestUri = uriInfo.getRequestUri();
    
            String contextPath = context.getContextPath();
    
            if (contextPath.trim().length() == 0) {
                String fullContextPath = requestUri.getScheme() 
                        + "://" + requestUri.getRawAuthority();
                return URI.create(fullContextPath);
            } else {
                int contextPathSize = contextPath.length();
                String requestString = requestUri.toASCIIString();
                String fullContextPath = requestString.substring(
                        0, requestString.indexOf(contextPath) + contextPathSize);
                return URI.create(fullContextPath);
            }
        }
    }
    

    然后使用path 和UriBuilder 相对于servlet 上下文路径的jsp 页面路径。例如,如果您的 jsp 页面位于上下文路径的根目录(即 webapp 文件夹的根目录,或者在大多数情况下与 index.jsp 相同的位置),您可以这样做

    @POST
    public Response post(@Context ServletContext servletContext, 
                         @Context UriInfo uriInfo) {
    
        URI contextPath = UriUtils.getFullServletContextPath(servletContext, uriInfo);
    
        UriBuilder uriBuilder = UriBuilder.fromUri(contextPath);
        uriBuilder.path("feed.jsp");
        return Response.seeOther(uriBuilder.build()).build();
    }
    

    我将对第一个链接的帖子进行更正:-)

    【讨论】:

    • 嘿,再次感谢您的回答。如果您可以看一下,我已尝试在 UPDATE 中使用您的类和方法。
    • 打印出 uri(在附加 Feed.jsp 之后)。如果它与指向索引页面的 url 相同,那么它没有理由不工作。告诉我
    • 好的,所以它输出:localhost:8080/messenger/Feed.jsp。这不是我想要的吗?我仍然没有收到说 POST 成功的消息客户端
    • 您不应该收到消息。它应该重定向到 Feed 页面。
    • 在 main.js 中确定我做了它,所以它是为了在 POST 成功时产生警报。它不会产生此警报,也不会重定向
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多