Why am I getting error: No result defined for action [] and result input?

Here is my Struts file in which I mapped all action files

struts.xml:

<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
    "http://struts.apache.org/dtds/struts-2.3.dtd">

<struts>
    <package name="default" extends="struts-default">
         <action name="AddTaskAction" class="actions.AddTaskAction" method="post">
            <result name="success">/success.jsp</result>
            <result name="error">/error.jsp</result>
        </action>
        <action name="UpdateTaskAction"
            class="actions.UpdateTaskAction" method="post">
            <result name="success">/displayTask.jsp</result>
            <result name="error">/error.jsp</result>
        </action>
        <action name="DeleteTaskAction"
            class="actions.DeleteTaskAction" method="post">
            <result name="success">/displayTask.jsp</result>
            <result name="error">/error.jsp</result>
        </action>
    </package>
</struts>

This is Action class file here I written about the data insertion into the database using Hibernate's transaction, session

AddTaskAction.java:

package actions;

import org.hibernate.Session;
import org.hibernate.Transaction;

import com.opensymphony.xwork2.ActionSupport;
import helper.FactoryProvider;
import model.Task;

public class AddTaskAction extends ActionSupport {
    private Task t = new Task();

    public String post() {
        Session session = FactoryProvider.getFactory().openSession();
        Transaction tx = null;

        try {
            tx = session.beginTransaction();
            session.persist(t);
            tx.commit();
            addActionMessage("Task added successfully!");
            return SUCCESS;
        } catch (Exception e) {
            if (tx != null && tx.isActive()) {
                tx.rollback();
            }
            addActionError("Error adding task: " + e.getMessage());
            return ERROR;
        } finally {
            session.close();
        }
    }

    public Task getT() {
        return t;
    }

    public void setT(Task t) {
        this.t = t;
    }
}

In this below JSP page I written a code to take the data from the user and it will redirect the data into my action file to insert into databse

addTask.jsp:

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Add Task</title>
    </head>
<body>
<div class="form-container">
        <form action="AddTaskAction" method="post">
            <div class="form-element">
                <label for="name" class="form-label">Task Name</label>
                <input type="text" id="name" name="t.name" required>
            </div>
            <div class="form-element">
                <label for="description" class="form-label">Description</label>
                <input type="text" id="description" name="t.description" required>
            </div>
            <div class="form-element">
                <label for="due_date" class="form-label">Due Date</label>
                <input type="text" id="due_date" name="t.due_date" required>
            </div>
            <div class="form-element">
                <label for="level" class="form-label">Priority Level</label>
                <input type="text" id="level" name="t.level" required>
            </div>
            <div class="form-element">
                <label for="status" class="form-label">Status</label>
                <input type="text" id="status" name="t.status" >
            </div>
            <div class="form-submit">
                <button type="submit">ADD</button>
            </div>
        </form>
    </div>
</body>
</html> 

I am trying to insert data into the database using Struts2 but in result I am getting above error frequently

Upvotes: 0

Views: 50

Answers (1)

Roman C
Roman C

Reputation: 1

There should be INPUT result configured.

<action name="AddTaskAction" class="actions.AddTaskAction" method="post">
  <result name="success">/success.jsp</result>
  <result name="input">/addTask.jsp</result>
  <result name="error">/error.jsp</result>
</action>

INPUT result is returned by the workflow interceptor if your action has errors. These errors might be encountered by interceptors from the stack before your action is executed. For example, it could be validation errors when the validation is performed because you have a validation interceptor on the stack.

Upvotes: 0

Related Questions