1

I have created Struts 2 database application. In this one using Query I made Ajax call to the Struts 2 action. The problem is whenever I hit the submit button, it will trigger the Ajax call, but Ajax call is not properly hit the server action class URL. Every time I got 404 exception in browser. How to resolve this problem?

this is my js file :

    $(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>

and this is my struts.xml file :

<?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 class:

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;
    }
    
}   }

Roman C
  • 49,761
  • 33
  • 66
  • 176
user3337270
  • 11
  • 1
  • 2

2 Answers2

0

There is no execute method in your LoginAction class, so AJAX call does not return any result. Try to add simple execute method:

public String execute() {
     return SUCCESS;
}
Kamil Chaber
  • 568
  • 1
  • 10
  • 26
0

404 is not an exception. It's an error code the Struts set to the response when no action found in the runtime configuration corresponding to the URL requested.

Because you didn't post a web.xml consider looking at Error 404 issues using Struts application.

Also check your library folder WEB-INF/lib for plugins you didn't use and remove them. Look at the issue at Struts2 - HTTP Status 404 - No result defined for action. You should post the stacktrace with exact error message however.

When calling an action from javascript code use s:url tag to build the action URL from the action mapping. See Jquery Error - While calling struts action.

The code you should change

$.ajax({
    type: "POST",
    url: '<s:url namespace="/" action="login"/>',
    data: jsonData,
    success: function(data){
        console.log(""+data);
    }
});

By the way if you are using struts2-jquery plugin there's a possibility to make ajax submit with <sj:submit> tag.

Roman C
  • 49,761
  • 33
  • 66
  • 176