简体   繁体   English

Java,Struts2,jQuery:验证登录字段而不重定向到另一个页面

[英]Java, Struts2, jQuery : Validate login fields without redirecting to another page

I've got a web app that I've built using Java, JDBC for database access, Struts2 to pass data to the views, and jQuery to aid in view manipulation. 我有一个我使用Java构建的Web应用程序,用于数据库访问的JDBC,用于将数据传递给视图的Struts2,以及用于帮助视图操作的jQuery。

When the app starts, it executes Home.action and displays the Home.jsp page which includes a "SIGN IN" link at the top. 当应用程序启动时,它会执行Home.action并显示Home.jsp页面,其中包含顶部的“SIGN IN”链接。 There are other views that include the "SIGN IN" link at the top as well. 还有其他视图也包括顶部的“登录”链接。

When a user clicks on "SIGN IN", jQuery causes a hidden pane to appear that provides text fields for username and password and a "Login" button. 当用户点击“登录”时,jQuery会显示一个隐藏窗格,提供用户名和密码的文本字段以及“登录”按钮。

Here's my dilemma... currently, clicking on the "Login" button executes Login.action and then redirects back to Home.action. 这是我的困境......目前,单击“登录”按钮执行Login.action,然后重定向回Home.action。 However, what I'd really like is for Login.action to perform the login, but remain on the same page. 但是,我真正喜欢的是Login.action执行登录,但保留在同一页面上。 In fact, I don't want the view to change or refresh at all; 事实上,我不希望视图改变或刷新; I simply want the login to happen and all else to remain the same. 我只想让登录发生,其他所有都保持不变。

I have two main reasons for this: 1) If the user enters invalid field data or the login is unsuccessful, I don't want to go anywhere; 我有两个主要原因:1)如果用户输入无效的字段数据或登录失败,我不想去任何地方; I just want to display the error to the user. 我只想向用户显示错误。 2) Since multiple pages offer the ability to login at the top, I don't want to arbitrarily redirect back to Home.action; 2)由于多个页面提供了登录顶部的能力,我不想随意重定向回Home.action; I want the user to stay where they are. 我希望用户留在原地。

So, my question to the community is this: can I do that with the technologies that I've already employed (and how?), or do I need to look into adding other technology (and which ones?)? 所以,我对社区的问题是:我可以用我已经使用的技术(以及如何使用)来实现这一点,或者我是否需要考虑添加其他技术(以及哪些技术?)?

Thanks very much! 非常感谢!

I will try to summarize as best as I can. 我会尽力总结一下。

Option 1 : Go with SPA (Singe-page Application). 选项1 :使用SPA(单页应用程序)。 You may add another JavaScript framework on to top of jQuery to make your life easier. 您可以在jQuery之上添加另一个JavaScript框架,以使您的生活更轻松。 More information on: http://en.wikipedia.org/wiki/Single-page_application 有关以下内容的更多信息: http//en.wikipedia.org/wiki/Single-page_application

Option 2: You can create your own JavaScript MVC Framework using jQuery (or whatever is available in the market). 选项2:您可以使用jQuery(或市场上可用的任何东西)创建自己的JavaScript MVC框架。

Notice that after deciding between Option1 and Option2 you will have two layers: Presentation Layer + Business Layer. 请注意,在决定Option1和Option2之后,您将有两个层:表示层+业务层。

Finally, you can make Ajax calls to your Web Service and control any redirect or display behaviour from the presentation layer. 最后,您可以对Web服务进行Ajax调用,并控制表示层中的任何重定向或显示行为。 In conclusion, your presentation is totally independent from your business layer. 总之,您的演示文稿完全独立于您的业务层。

Let's see if I can illustrate an example: 让我们看看我是否可以举例说明:

This would be part of our login.html 这将是我们login.html的一部分

<script type="text/javascript">
$(document).ready(function(){
    $("#singInButton").click(function{
    var parameters = {SignIn:{username:"userNameFromField", password:"passwordFromField"}};
    $.post("signin", parameters, function(data, status){
            if(data.message === "SUCCESS") {
                alert("Welcome back.");
            }else{
                alert("Invalid credentials. Please, try again.");
            }
        });
    });
});
</script>

Let's suppose this would generate a request like: {SignIn:{username:"userNameFromField", password:"passwordFromField"}} 假设这会产生如下请求:{SignIn:{username:“userNameFromField”,password:“passwordFromField”}}

On the server side it was verified that these are valid credentials. 在服务器端,已验证这些是有效凭据。 So, you would return something like (sorry, I'm not familiar with Struts, but I am sure you can figure it out): 所以,你会返回类似的内容(对不起,我对Struts不熟悉,但我相信你能搞清楚):

response.setContentType("application/json");
response.setResponseBody("SUCCESS");

Finally, the user interface would alert "Welcome back.". 最后,用户界面会提醒“欢迎回来”。 You do not have to dispatch your response, just return what you want in the correct format. 您无需发送响应,只需以正确的格式返回所需内容即可。 Of course, there are hundreds of frameworks to make life easier and convert json or xml objects into JavaObject so you do not have to parse strings manually. 当然,有数百个框架可以使生活更轻松,并将json或xml对象转换为JavaObject,因此您不必手动解析字符串。

I hope this example can clarify what I am trying to say. 我希望这个例子可以澄清我想说的话。

Cheers, 干杯,

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM