我已经创建了 Struts 2 数据库应用程序。在这一示例中,我使用 Query 对 Struts 2 操作进行了 Ajax 调用。问题是每当我点击提交按钮时,它都会触发 Ajax 调用,但 Ajax 调用没有正确命中服务器操作类 URL。每次我在浏览器中遇到 404 异常时。如何解决这个问题?
这是我的js文件:
$(document).ready(function(e){
$("#loginPage_emailid").blur(function () {
console.log("OnBlur() EmailId");
var emailId=$("#loginPage_emailid").val();
console.log("emailId ------- "+emailId);
if(emailId != null && emailId.length != 0 && typeof emailId != 'undefined' && emailId != "InvalidEmailId"){
FormValidator.prototype.validateEmailId(emailId);
}
else{
$("#loginPage_emailid").val("InvalidEmailId");
return false;
}
});
$("#loginPage_emailid").click(function () {
var emailId=$("#loginPage_emailid").val();
if( emailId == "InvalidEmailId"){
$("#loginPage_emailid").val("EmailID");
}
});
$("#loginPage_password").blur(function () {
var password=$("#loginPage_password").val();
if( password != "InvalidPassword"){
$("#loginPage_password").val("Password");
}
});
$("#loginPage_password").blur(function () {
var password=$("#loginPage_password").val();
console.log("password ------- "+password);
if(password != null && password.length != 0 && typeof password != 'undefined' && password != "InvalidPassword"){
FormValidator.prototype.validatePassword(password);
}
else{
$("#loginPage_password").val("InvalidPassword");
return false;
}
});
$("#loginPage_form").submit(function () {
console.log("Submit !!!!");
if(!FormValidator.prototype.isValidEmailId || !FormValidator.prototype.isValidPassword){
console.log("@#$%^&#$%^&@#%*@#$%&*#^&*#$%&*($%&*(");
if(!FormValidator.prototype.isValidEmailId){
$("#loginPage_emailid").val("InvalidEmailId");
}
if(!FormValidator.prototype.isValidPassword){
$("#loginPage_password").val("InvalidPassword");
}
return false;
}
else{
console.log("Success full submit");
var jsonData={};
jsonData.emailId=FormValidator.prototype.emailId;
jsonData.password=FormValidator.prototype.password;
$.ajax({
type: "POST",
url: "login.action",
data: jsonData,
success: function(data){
console.log(""+data);
}
});
return false;
}
});
});
function FormValidator(){
var isValidEmailId=false;
var isValidPassword=false;
var emailId=null;
var password=null;
}
FormValidator.prototype.validateEmailId=function(emailId){
console.log("Email Id ===== "+emailId);
var reg = /^\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/;
if (reg.test(emailId)){
FormValidator.prototype.isValidEmailId=true;
FormValidator.prototype.emailId=emailId;
}
};
FormValidator.prototype.validatePassword=function(password){
console.log("Password ===== "+password);
FormValidator.prototype.isValidPassword=true;
FormValidator.prototype.password=password;
};
loginpage.jsp
:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<!--[if lt IE 7 ]> <html lang="en" class="ie6 ielt8"> <![endif]-->
<!--[if IE 7 ]> <html lang="en" class="ie7 ielt8"> <![endif]-->
<!--[if IE 8 ]> <html lang="en" class="ie8"> <![endif]-->
<!--[if (gte IE 9)|!(IE)]><!-->
<html lang="en"><!--<![endif]--><head>
<meta charset="utf-8">
<title>Login Form</title>
<link rel="stylesheet" type="text/css" href="css/style.css">
<script type="text/javascript" src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
<script type="text/javascript" src="js/formvalidator.js"></script>
</head>
<body>
<div class="container">
<section id="content">
<!-- <s:form id="loginPage_form">
<div>
<s:textfield name ="emailid" label="EmailID" placeholder="EmailID" required="" id="loginPage_emailid" />
</div>
<div>
<s:password type="password" name ="password" label="Password" placeholder="Password" required="" id="loginPage_password" />
</div>
<div>
<s:submit value="Log in" />
</div>
</s:form> -->
<form id="loginPage_form" >
<h1>Login Form</h1>
<div>
<input type="text" placeholder="EmailID" required="" id="loginPage_emailid">
</div>
<div>
<input type="password" placeholder="Password" required="" id="loginPage_password">
</div>
<div>
<input type="submit" value="Log in"> <a href="#" style="display: none;">Lost your password?</a> <a href="#" style="display: none;">Register</a>
</div>
</form>
<div id="loginPage_errormessage" style="display: none;margin: 0px 0px;position: absolute;left: 47%;top: 79%;color: orangered;">emailid or password is invalid</div>
<!-- form -->
<div class="button" style="display: none;">
<a href="#">Download source file</a>
</div>
<!-- button --> </section>
<!-- content -->
</div>
<!-- container -->
</body></html>
这是我的
struts.xml
文件:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<constant name="struts.enable.DynamicMethodInvocation" value="false" />
<constant name="struts.devMode" value="false" />
<constant name="struts.convention.default.parent.package" value="default"/>
<package name="default" extends="struts-default" namespace="/">
<action name="login" class="com.jamcracker.view.LoginAction" method="execute">
<result name="success">/jsp/dummy.jsp</result>
</action>
<!-- <action name="index">
<result>loginpage.jsp</result>
</action> -->
<!-- <action name="add"
class="com.jamcracker.view.ContactAction" method="add">
<result name="success" type="chain">index</result>
<result name="input" type="chain">index</result>
</action>
<action name="delete"
class="com.jamcracker.view.ContactAction" method="delete">
<result name="success" type="chain">index</result>
</action>
<action name="index"
class="com.jamcracker.view.ContactAction">
<result name="success">index.jsp</result>
</action> -->
</package>
</struts>
LoginAction.java
班级:
package com.jamcracker.view;
import com.opensymphony.xwork2.ActionSupport;
public class LoginAction extends ActionSupport{
private static final long serialVersionUID = 1L;
private String emailId;
private String password;
public String getEmailId() {
return emailId;
public void setEmailId(String emailId) {
this.emailId = emailId;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
} }
您的
execute
类中没有 LoginAction
方法,因此 AJAX
调用不会返回任何结果。尝试添加简单的execute
方法:
public String execute() {
return SUCCESS;
}
404 也不例外。这是一个错误代码,当在运行时配置中找不到与请求的 URL 相对应的操作时,Struts 设置为响应。
因为您没有发布
web.xml
,请考虑查看 使用 Struts 应用程序的错误 404 问题。
另请检查您的库文件夹
WEB-INF/lib
是否有未使用的插件并将其删除。查看问题Struts2 - HTTP 状态 404 - 没有为操作定义结果。不过,您应该发布带有确切错误消息的堆栈跟踪。
从 javascript 代码调用操作时,使用
s:url
标签从操作映射构建操作 URL。请参阅Jquery 错误 - 调用 struts 操作时。
您应该更改的代码
$.ajax({
type: "POST",
url: '<s:url namespace="/" action="login"/>',
data: jsonData,
success: function(data){
console.log(""+data);
}
});
顺便说一句,如果您使用 struts2-jquery 插件,则可以使用
<sj:submit>
标签进行 ajax 提交。