我已经创建了Struts 2数据库应用程序。在这一个使用Query我做了Ajax调用Struts 2动作。问题是每当我点击提交按钮时,它都会触发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;
}
} }
答案 0 :(得分:0)
execute
课程中没有LoginAction
方法,因此AJAX
调用不会返回任何结果。尝试添加简单的execute
方法:
public String execute() {
return SUCCESS;
}
答案 1 :(得分:0)
听着,404也不例外。当在与所请求的URL对应的运行时配置中找不到任何操作时,Struts设置为响应的错误代码。
因为您没有发布web.xml
,请考虑查看Error 404 issues using Struts application。
同时检查您的库文件夹WEB-INF/lib
以查找未使用的插件并将其删除。请查看Struts2 - HTTP Status 404 - No result defined for action处的问题。您应该发布带有确切错误消息的堆栈跟踪。
从javascript代码调用操作时,使用s:url
标记从操作映射构建操作URL。请参阅Jquery Error - While calling struts action。
您应该更改的代码
$.ajax({
type: "POST",
url: '<s:url namespace="/" action="login"/>',
data: jsonData,
success: function(data){
console.log(""+data);
}
});
顺便说一句,如果你使用的是struts2-jquery插件,可以使用sj:submit
标签提交ajax。