使用JAX-RS UriBuilder渲染新的.jsp页面 [英] Rendering a new .jsp page using JAX-RS UriBuilder

查看:80
本文介绍了使用JAX-RS UriBuilder渲染新的.jsp页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是在尝试发送一个新的.jsp页面,以供客户端使用URIBuilder呈现.因此,我有一个main.js发送POST到服务器,该服务器调用logIn().现在,我只希望它向客户端发送一个新的.jsp文件进行渲染.到目前为止,什么都没有发生,在使用"Feed.jsp"作为文件路径之前,我尝试过使用其他文件路径.

I am simply trying to send a new .jsp page to be rendered by the client using the URIBuilder. So I have a main.js send a POST to the server which calls logIn(). For now I just want this to send a new .jsp file to the client to be rendered. Nothing happens thus far, I have tried using different file paths - before I just used "Feed.jsp" as the file path.

我觉得还有更多我不愿意接受的事情.

I feel like there is more to this that I am not picking up on.

这是我的main.js文件.它通过logIn()方法成功将POST发送到服务器.我的index.jsp文件成功使用了这个main.js.

Here is my main.js file. It sends a POST to server successfully via the logIn() method. This main.js is used successfully by my index.jsp file.

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时,它什么也不做.

Here is my is the method LogIn() called in my ProfileResource.java. It successfully can call the post but for some reason when I include the UriBuilder it does nothing.

@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"按钮却什么也没做.

So basically my index.jsp gets rendered to the client. My "btnResgister" button does what it needs, my "btnSignIn" just doesn't do anything although I know it accesses the "profiles/login" resource fine.

更新 我已经使用用户peeskillets UriUtils类实现了这样的登录方法:

Update I have implemented my login method like this using user peeskillets UriUtils class:

@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从客户端发送更多信息?

But the POST is still not being completed. I am wondering if this has to do with the ServletContext or UriInfo parameters... Are these parameters automatically sent when I POST or do I have to send more info from the client using .js?

这也是我的文件结构:

推荐答案

此答案的致歉.看来ServletContext.getContextPath()仅返回相对路径,而不是完整uri.在这种情况下,当Jersey找到相对URI时,它将从应用程序的基本URI构造完整的URI.因此,您将始终获得一个包含Jersey根路径的路径(该路径永远不会导致jsp页面).

Apologies for this answer. It seems the ServletContext.getContextPath() only returns the relative path and not the full uri. In which case, when Jersey finds a relative URI, it will construct a full URI from the base URI of the application. So you will always get a path containing the Jersey root path (which will never lead to the jsp page).

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

I don't know how else to get the the complete context path (full URI), from Jersey except to do some string manipulation. You can use

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);
        }
    }
}

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

Then use path with a UriBuilder the path of the jsp page, relative to the servlet context path. For example, if your jsp page is at the root of the context path (i.e at the root of the webapp folder, or in most cases same location as index.jsp), you can do

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

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

I will make a correction to the first linked post :-)

这篇关于使用JAX-RS UriBuilder渲染新的.jsp页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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