Changing from one namespace to another when clicking a submit button in Struts 2

739 views Asked by At

I'm new to Struts 2 framework, I have a problem changing from one namespace to another when clicking a submit button, I get this error.

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>

<package name="module-login" namespace="/login" extends="struts-default">

    <action name="verifier" class="presentation.LoginAction" method="verifierLogin">
        <result name="model.Client">/vuesJSP/client/client.jsp</result>
        <result name="model.Administrateur">/vuesJSP/admin/ajouterClient.jsp</result>
        <result name="error">/vuesJSP/login/login.jsp</result>
        <result name="input">/vuesJSP/login/login.jsp</result>
    </action>

</package> 

<package name="module-Admin" namespace="/admin" extends="struts-default">

    <action name="ajouter" class="presentation.AdminAction" method="ajouterClient">
    <result name="success">/vuesJSP/admin/ajouterClient.jsp</result>
    <result name="error">/vuesJSP/admin/ajouterClient.jsp</result>
    <result name="input">/vuesJSP/admin/ajouterClient.jsp</result>
    </action>

    <action name="modifier" class="presentation.AdminAction" method="modifierClient">
    </action>

    <action name="supprimer" class="presentation.AdminAction" method="supprimerClient">
    </action>

    <action name="lister" class="presentation.AdminAction" method="listerClients">
    </action>

</package>

form.jsp

<s:form method="post">

<s:textfield name="nom" label="Nom"></s:textfield>
<s:textfield name="prenom" label="prenom"></s:textfield>
<s:textfield name="cin" label="cin"></s:textfield>
<s:textfield name="login" label="login"></s:textfield>
<s:textfield name="password" label="password"></s:textfield>

<s:submit value="Ajouter Client" action="admin/ajouter"></s:submit>
<s:submit value="Lister les clients" action="admin/lister"></s:submit>

</s:form>

actual error when clicking on "ajouter client" button

Error HTTP 404 - There is no Action mapped for namespace /login and action name admin/ajouter.

i want to change the url when clicking "ajouter client" button from "http://localhost:8080/GestionCommandes/login/verifier.action" to "http://localhost:8080/GestionCommandes/admin/ajouter.action"

is there any way so i can achieve that ?

1

There are 1 answers

0
Roman C On BEST ANSWER

In the struts form tag you should use namespace and action attribute. Don't use slashes in action name unless it's configured in struts.xml.

<s:form namespace="/admin" action="ajouter" method="post">

  <s:textfield name="nom" label="Nom"></s:textfield>
  <s:textfield name="prenom" label="prenom"></s:textfield>
  <s:textfield name="cin" label="cin"></s:textfield>
  <s:textfield name="login" label="login"></s:textfield>
  <s:textfield name="password" label="password"></s:textfield>

  <s:submit value="Ajouter Client"></s:submit>
  <s:submit value="Lister les clients" action="lister"></s:submit>
</s:form>

Note, to use action attribute in struts submit tag you should enable the action prefix for the parameter with action name. See this answer for detailed explanation.