servlet 重定向后 jQuery 移动页面未正确加载 [英] jQuery mobile pages not loading correctly after servlet redirect

查看:50
本文介绍了servlet 重定向后 jQuery 移动页面未正确加载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的移动网络应用程序中有一些基于 jQuery Mobile 的页面,这些页面无需身份验证即可访问,而有些页面仅在用户通过身份验证后才能访问.我的解决方案基于本教程:Java Servlet 过滤器示例教程.

I have some pages in my mobile web app based on jQuery Mobile that need to be accessible without authentication and some only after user has been authenticated. My solution is based on this tutorial: Java Servlet Filter Example Tutorial.

我已经完成了 @WebFilter,它检查用户尝试访问的页面以及他是否未通过身份验证(会话 未创建或没有属性 >usersession 中)他被重定向到 login.html 页面.

I have done @WebFilter which checks to which page user tries to access and if he isn't authenticated (session not created or there isn't attribute user inside the session) he's being redirected to login.html page.

这是来自我的 login.html 页面的相关代码:

This is relevant code from my login.html page:

<form action="LoginServlet" method="post">
...
input elements for username and password
...
</form>

表单将其数据提交到 LoginServlet.LoginServlet 检查凭据是否正确,如果正确,则将其重定向回所需页面.目前我在会话中存储所需的页面(最好将其存储在表单内的隐藏元素中,但我不知道什么时候我在写代码)

Form submitting its data to LoginServlet. LoginServlet checks that credentials are correct and if they're correct, redirect it back to the required page. Currently I store required page in the session (it's better to store this in a hidden element inside the form, but I didn't know it when I was writing the code)

我使用以下代码行从 LoginServlet 执行重定向:

I perform redirect from LoginServlet by using this line of code:

response.sendRedirect(redirectToPage);

我项目中的所有页面都是带有 javascript 的简单 *.html 页面,我使用 jQuery Mobile 制作了一个移动网络应用程序.

All pages in my project are simple *.html pages with javascript, I make a mobile web app by using jQuery Mobile.

重定向到所需页面后的问题:

My problem that after redirect to required page:

  1. 在地址栏中我看到了我的 servlet 的名称 LoginServlet 而在 Chrome 的控制台中我没有看到我的页面内容,只有我的 servlet 的名称 LoginServlet 其内容为空白.但是在浏览器中,我看到了所有静态内容,并且呈现正确.
  2. 最重要的问题是我加载的页面中的所有动态内容都不起作用.动态内容我的意思是一些 ajax 函数,它们应该在页面加载和呈现动态内容时调用:

  1. It the address bar I see name of the my servlet, LoginServlet while in a console of Chrome I didn't see content of my page, only the name of my servlet, LoginServlet and its content is a blank. But inside the browser I see all static content, it's rendered correctly.
  2. The most important problem that all my dynamic content in a loaded page doesn't work. Dynamic content I mean a few ajax function which should be invoked upon page loading and render dynamic content:

$(document).on('pagebeforeshow',function(){
            initPage();
});

如果我重新加载页面,一切都会开始正常工作.

If I reload page everything begins to work as it should.

我做错了什么?提前致谢.

What am I doing wrong? Thank you in advance.

推荐答案

对于这个问题,实际上有两种解决方案.第一个是在 javascript 中而不是在 servlet 中执行重定向,我认为这是更好的解决方案,有关更多详细信息,请参见此处:如何管理 jQuery Ajax 调用后的重定向请求如果您在提交 <form/> 后需要从 servlet 执行此操作,则第二种解决方案是添加 data-ajax="false"

标签:<form action="postthis" method="post" data-ajax="false">,这个解决方案取自这里:https://stackoverflow.com/a/8631895/947111

There are actually a two solutions for this problem. The first one is to perform redirect in javascript not in servlet, and I think that it's preferable solution, for more details look here: How to manage a redirect request after a jQuery Ajax call And second solution if you required to do it from servlet after submitting a <form/>, is to add data-ajax="false" into the <form/> tag: <form action="postthis" method="post" data-ajax="false">, this solution is taken from here: https://stackoverflow.com/a/8631895/947111

这篇关于servlet 重定向后 jQuery 移动页面未正确加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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