-1

I am trying to learn Struts2, I have used a view page that hits action , the action class is using a bean where getter setter methods are written and the action is also using a dao where the connection is written.I want to mention that I am using jboss v5.0 and eclipse, I have added all the jar files.

Now , when I am trying to run this application the welcome jsp page is hitting properly then on clicking submit button the below error is showing:

There is no Action mapped for namespace /controller and action name

I am placing my code and directory structure. I have tried the using namespace="/" <result name="SUCCESS">/success.jsp</result>, still its is not working


passing
+JAX-WS Web Services
+Deployment Descriptor:Passing
-Java Resources
 -src
  -controller
   -TestAction.java
    -TestAction
     -execute(HttpServletRequest request , HttpServletResponse response) : String

   -model
    -TestBean.java
     -TestBean
      -age
      -ocation
      -name
      -getAge() :String
      -getName() :String
      -getLocation() :String
      -setAge(String) : void
      -setName(String) : void
      -getlocation(String) : void
    -UserDao.java
     - UserDao
      -Logincheck() :Connection

registration.jsp


web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>Passing</display-name>

  <filter>
  <filter-name>struts2</filter-name>
  <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
  </filter>

  <filter-mapping>
  <filter-name>struts2</filter-name>
  <url-pattern>/*</url-pattern>
  </filter-mapping>

  <welcome-file-list>
    <welcome-file>registration.jsp</welcome-file>
  </welcome-file-list>
</web-app>


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>
        <package name="default" extends="sturts-default">
            <action name="TestAction" class="controller.TestAction">
                <result name="SUCCESS">/success.jsp</result>
            </action>
        </package>




  </struts>


success.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">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<center> Welcome, Data successfully inserted</center>
</body>
</html>


registration.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">
<html>
<head>
<%@ taglib uri="/struts-tags" prefix="s"/%>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<s:form action="TestAction" method="post" >
<s:textfield name="Name" label="Name"/>
<s:textfield name="Age" label="Age"/>
<s:textfield name="Location" label="Location"/>
<s:submit label="Submit"></s:submit>

</s:form>

</body>
</html>
      


TestAction.java

package controller;

import java.sql.Connection;
import java.sql.PreparedStatement;

import com.opensymphony.xwork2.ActionSupport;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import model.TestBean;
import model.UserDao;

import org.apache.struts2.ServletActionContext;


public class TestAction extends ActionSupport{



    public String execute(HttpServletRequest request , HttpServletResponse response)   throws Exception
    {
        String Name = request.getParameter("Name");
        String Age = request.getParameter("Age");
        String Location = request.getParameter("Location");

        TestBean bean = new TestBean();

        bean.setName(Name);
        bean.setAge(Age);
        bean.setLocation(Location);

        UserDao obj = new UserDao();
        Connection x= obj.Logincheck();

        if(x!=null)
        {
        PreparedStatement ps= x.prepareStatement("insert into Registration values(?,?,?)");
        ps.setString(1, bean.getName());
        ps.setString(2, bean.getAge());
        ps.setString(3, bean.getLocation());
        int i = ps.executeUpdate();
        System.out.println("The value of i =" +i);
        if(i>0)
        {
        return SUCCESS;
        }
        }
        return null;

    }
}


TestBean.java

package model;

public class TestBean {
    String name;
    String age;
    String location;

    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getAge() {
        return age;
    }
    public void setAge(String age) {
        this.age = age;
    }
    public String getLocation() {
        return location;
    }
    public void setLocation(String location) {
        this.location = location;
    }

}


UserDao.java

package model;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;

public class UserDao {
 
 public Connection Logincheck()
 {
  
  Connection con=null;
  
  try
  {
   try
   {
    Class.forName("oracle.jdbc.driver.OracleDriver");
   }
   catch(ClassNotFoundException e)
   {
    e.printStackTrace();
   }
   
   con= DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE", "username", "password");
   if(con!=null)
   {
    return con;
   }
   
  }
  catch(Exception e)
  {
   e.printStackTrace();
  }
  return null;
  
 }

}

that's all.

Roman C
  • 49,761
  • 33
  • 66
  • 176
Anna
  • 31
  • 8
  • Unrelated, but the `execute` method is doing too much, and operating at different levels of abstraction. This isn't a huge deal, but IMO it's a bad habit to get in to. – Dave Newton Dec 10 '14 at 11:27
  • Please clarify your specific problem or add additional details to **highlight exactly what you need**. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. – Roman C Dec 10 '14 at 12:06

2 Answers2

0

Following are points which you should notice,

  1. If you are using TestBean class as data carrier, you have to implement ModelDriven Interface.
  2. TestAction.java should present in "Controller" package.
  3. If you want HttpServletRequest and HttpServletResponse in your execute() method, then the simplest way you can get that is to implement ServletRequestAware and ServletResponseAware interfaces and implement respective methods.
  4. And most important if your success.jsp is in Web Content folder, then you dont need to specify

     <result name="SUCCESS">/success.jsp</result>
    

Just make it like

 <result name="SUCCESS">success.jsp</result>

And if you have any folder suppose, "Folder1" in Web Content, Then specify like

 <result name="SUCCESS">Folder1/success.jsp</result>
Aditya Ekbote
  • 1,493
  • 3
  • 15
  • 29
  • No need to use ModelDriven, no need to use HttpServletRequest and HttpServletResponse, and ServletResponseAware and ServletRequestAware are not providing the two aboves, but wrappers – Andrea Ligios Dec 10 '14 at 10:12
  • Thanks Aditya EKbote your last point worked for my application.I also want to mention that the other points you said was not required for my application. – Anna Dec 12 '14 at 08:18
  • Yes. I know that. You passed HttpServletRequest and HttpServletResponse to execute method. That also can work. And I mentioned my last point as important. If this worked for you, can you vote up and accept the answer? – Aditya Ekbote Dec 12 '14 at 08:22
0
  1. This error

    There is no Action mapped for namespace /controller and action name

    generally means Struts can't find the action specified in a namespace specified (/controller in this case). BTW in your case you've not specified /controller anywhere and the action name is missing in the error message, so this error could be probably generated by the bunch of other errors you have in your action / JSP / configuration.

  2. you also have two typos, preventing anything to work:

    • sturts-default instead of struts-default:

      <package name="default" extends="struts-default">
      
    • "SUCCESS" instead of "success", that is the result mapped to the constant SUCCESS that you are returning from the Action method.

          <result name="success">/success.jsp</result>
      
  3. If using Struts version newer than or equals to 2.1.3, you need the new filter, StrutsPrepareAndExecuteFilter instead of the deprecated FilterDispatcher;

  4. The action method must be a public String something() with no parameters. This:

    public String execute(HttpServletRequest request , HttpServletResponse response)   throws Exception
    

    is simply wrong, and should be:

    public String execute(){}
    
  5. Here

    String Name = request.getParameter("Name");
    String Age = request.getParameter("Age");
    String Location = request.getParameter("Location");
    
    TestBean bean = new TestBean();
    
    bean.setName(Name);
    bean.setAge(Age);
    bean.setLocation(Location);
    

    you are reinventing the wheel really badly. Attributes must be private, at class level, with getters and setters, and they'll be populated automatically, with no need to access the request. In your case:

    private TestBean bean;
    /* GETTER AND SETTER */
    
    public String execute(){
        System.out.println(bean.getName());
        // ...
    }
    

Unrelated, but Java naming conventions use lower-case names for local variables and instance properties.

  1. In JSP page, you need to access the getter with a starting lowercase character:

    <s:textfield name="Name" label="Name"/>
    

    must be

    <s:textfield name="name" label="Name"/>
    

    and so the other fields. Also if you point to an object like TestBean, you need to do like this:

    <s:textfield name="bean.name" label="Name"/>
    

This are the main problems... BTW consider start coding after having read a book, or some good tutorial. Starting in the dark like this is not the right way, IMHO.

Community
  • 1
  • 1
Andrea Ligios
  • 49,480
  • 26
  • 114
  • 243
  • Upped for the normal reason :/ – Dave Newton Dec 10 '14 at 11:24
  • Ok, but even if you post the canonical, perfect answer to her exact problem (basically only in the title, because the question has an entire software in it), it won't work the same, because she have *at least* other 6 problems :| This is not a good question for SO, and I generally skip Q like this with a sort of RTFM. In this case I've tried to help OP (though I didn't noticed there were so much errors when I've begun to write the answer :) getting a downvote. What about the question? Or the other answer saying it's mandatory to use ModelDriven? There's someone spamming downvotes, not answers – Andrea Ligios Dec 10 '14 at 13:53
  • None, except the extra complexity that makes it an absolute bad idea suggesting its usage, especially to OP that seems unfamiliar with even the basic concepts of the framework – Andrea Ligios Dec 10 '14 at 19:45
  • my question is how to remove the "There is no Action mapped for namespace /controller and action name" problem. and if I go wrong anywhere in the code please correct me – Anna Dec 11 '14 at 09:58
  • Thanks Andrea Ligios all your point worked.Now my application is working fine.Thanks for all the mentioned points and rectifying my faults. – Anna Dec 12 '14 at 08:13
  • You're welcome. Please consider accepting the answer if it helped. So @RomanC, as you can see, this answer wasn't this bad: exactly as I thought, that error was caused by *the other errors*, and fixing them made it disappear... – Andrea Ligios Dec 12 '14 at 09:27
  • I don't know and nobody knows exactly the cause, so this question and answer is *not* useful on SO. – Roman C Dec 12 '14 at 09:43
  • Yes but at least it was useful for her, definitely not a `misunderstanding of S2 configuration. It's also not helpful for OP`. BTW I'll hardly answer this kind of questions again, we agree on that – Andrea Ligios Dec 12 '14 at 09:57
  • Yes this post is a comment on all errors in the code posted by OP. It is not answering anything. It's like a code review for finding bugs in the code, that isn't working. Nothing more, no point, no answer, and lack of a solution. Only "if" and "or" that makes a post too vague. I know if this answer is accepted then you will talk in another way. However the way are you talking to me is rude. I don't know who will agree with you and what? – Roman C Dec 12 '14 at 19:34
  • I am really sorry if i wasted any of your precious time.I posted this problem as I was unable to find out the solution by myself as I am just learning struts2 and tried to run an application based on it.And after this application my concept and logic became clear.I am really gratefull to all for helping me... – Anna Dec 15 '14 at 03:57