【问题标题】:404 exception in browser when making jQuery Ajax call from JSP page to Struts 2 action从 JSP 页面向 Struts 2 操作进行 jQuery Ajax 调用时,浏览器中出现 404 异常
【发布时间】:2014-02-21 13:03:06
【问题描述】:

我已经创建了 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;
    }
    
}   }

【问题讨论】:

  • 请删除与问题无关的代码。

标签: java jquery ajax jsp struts2


【解决方案1】:

您的LoginAction 类中没有execute 方法,因此AJAX 调用不会返回任何结果。尝试添加简单的execute 方法:

public String execute() {
     return SUCCESS;
}

【讨论】:

    【解决方案2】:

    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 插件,则可以使用 &lt;sj:submit&gt; 标签进行 ajax 提交。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多