Search code examples
javahibernatejspstruts2action-mapping

who's can help me with struts2 and hibernate to solve a particular exception: HTTP 404 - There is no Action mapped for namespace


I'm learning how to use struts2 and hibernate, but i can´t resolve this exception "There is no Action mapped for namespace [/] and action name [registrar_barrios] associated with context path [/ConsultarPatente].". I know it's asked many time, but it's common for several case, as well here we go:

The next is built on eclipse.

1.- Project explorer https://www.dropbox.com/s/4gg52h64e4hz64s/package.png?dl=0

2.- Codes (src and WebContent folers). https://www.dropbox.com/s/x3q9qtq050a8anv/Files.rar?dl=0

3.- I thinks AgregarBarrioAction and registrar_barrios are not working.

4.- Database.

CREATE TABLE `barrio_table` (
  `barrioId` int(11) NOT NULL auto_increment,
  `Nombre` varchar(24) collate utf8_bin NOT NULL,
  PRIMARY KEY  (`barrioId`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 COLLATE=utf8_bin AUTO_INCREMENT=4 ;

5.- AgregarBarrioAction code.

package com.java.rmo.actions;

import java.util.ArrayList;
import java.util.List;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Actions;
import org.apache.struts2.convention.annotation.InterceptorRef;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;

import com.java.rmo.dao.BarrioDAO;
import com.java.rmo.dao.BarrioDAOImpl;
import com.java.rmo.model.Barrio;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;


@Namespace(value="/")
public class AgregarBarrioAction extends ActionSupport
implements ModelDriven<Barrio>
{
    private static final long serialVersionUID = -6659925652584240539L;


    private Barrio barrio = new Barrio();
    private List<Barrio> barrioList = new ArrayList<Barrio>();
    private BarrioDAO barrioDAO = new BarrioDAOImpl();


    @Override
    public Barrio getModel()
    {
        return barrio;
    }

    @Action
    (
        value="agregarBarrio",
        results={@Result(location="/registrar_barrios.jsp", type="redirect")}   
    )
    public String add()
    {
        barrioDAO.saveBarrio(barrio);
        return SUCCESS;
    }

    @Action
    (
        value="consultarBarrio",
        results={@Result(location="/registrar_barrios.jsp")}
    )
    public String list()
    {
        setBarrioList(barrioDAO.listBarrio());
        return SUCCESS;
    }

    public Barrio getBarrio() 
    {
        return barrio;
    }

    public void setBarrio(Barrio barrio)
    {
        this.barrio = barrio;
    }

    public List<Barrio> getBarrioList() 
    {
        return barrioList;
    }

    public void setBarrioList(List<Barrio> barrioList) 
    {
        this.barrioList = barrioList;
    }


}

6.- registrar_barrios.jsp code

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>

<%@taglib uri="/struts-tags" prefix="s"%>
<!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>
<s:form action="agregarBarrio">
    <s:textfield name="nombre" label="Nombre" />
    <s:submit />
</s:form>

<s:if test="consultarBarrio.size() > 0">
    <div class="content">
    <table class="barrioTable" cellpadding="5px">
        <tr class="even">
            <th>Nombre</th>
        </tr>
        <s:iterator value="consultarBarrio" status="barrio">
            <tr
                class="<s:if test="#barrio.odd == true ">odd</s:if><s:else>even</s:else>">
                <td><s:property value="nombre" /></td>
            </tr>
        </s:iterator>
    </table>
    </div>
</s:if>
</body>
</html>

Give me a help, i appreciate to you. Good luck!!!


Solution

  • The error

    There is no Action mapped for namespace [/] and action name [registrar_barrios] associated with context path [/ConsultarPatente].

    is telling you that your action is not accessible. And it is not a surprise, since you don't have any @Action(name="registrar_barrios").

    Your actions are instead agregarBarrio and consultarBarrio:

    @Action(value="agregarBarrio", 
        results={@Result(location="/registrar_barrios.jsp", type="redirect")}   
    )
    
    
    @Action(value="consultarBarrio",
        results={@Result(location="/registrar_barrios.jsp")}
    )
    

    The first result is also using redirect out of turn. Remove it.