小编典典
你可以 …
重定向,(使用Response.seeOther(URI),将任何需要的值作为查询参数传递。例如
@POST
@Path("logIn")
public Response login(@Context ServletContext context) {
UriBuilder uriBuilder = UriBuilder.fromUri(URI.create(context.getContextPath()));
uriBuilder.path(.. ..);
uriBuilder.queryParam("key1", "value1");
uriBuilder.queryParam("key1", "value2");
URI uri = uriBuilder.build();
return Response.seeOther(uri).build();
}
或者你可以..
使用Jersey的JSP
MVC功能,并在此处清楚地演示。例如
@POST
@Path("logIn")
public Viewable login() {
Map model = new HashMap<>();
model.put("key1", "value1");
model.put("key2", "value2");
return new Viewable("/feed", model);
}
feed.jsp
${it.key1}
${it.key2}
另外: 您是否 真的 要在URI路径中传递密码?会带来巨大的安全风险。最好在请求正文中传递它。
更新
现在,我考虑了一下,您应该始终按照POST / REDIRECT /
GET模式从登录POST
重定向。如果要在整个方法中使用JSP
MVC,则可以让控制器返回Viewable登录页面的GET(在GET上),并在成功返回POST的情况下,重定向到Feed控制器,否则重定向回同一登录页面(得到)。有几种不同的解决方案。
例如
@Path("/login")
public class LoginController {
@GET
public Viewable loginPage() {
...
return new Viewable("/login", model); // to login.jsp
}
@POST
public Response loginPost(Form form, @Context UriInfo uriInfo) {
...
UriBuilder builder = uriInfo.getBaseUriBuilder();
if (success) {
builder.path("/feed"); // to FeedController GET
} else {
builder.path("/login"); // to LoginController GET
}
return Response.seeOther(builder.build()).build();
}
}
@Path("/feed")
public class FeedController {
@GET
public Viewable getFeed() {
...
return new Viewable("/feed", model); // to feed.jsp
}
}
2020-11-26