formsjspstruts

Registration action returns the requested resource is not available


I've a form with the following code:

<form action="doRegister" class="form-signup" >

    <h2 class="form-signup-heading">Please sign up</h2>
    <input type="email" class="form-control" placeholder="Email address" required autofocus>
    <input type="password" class="form-control" placeholder="Password" required>
    <input type="password" class="form-control" placeholder="Password control" required>
    <input type="text" class="form-control" placeholder="Name" required>
    <input type="text" class="form-control" placeholder="Surname" required>
    <input type="date" class="form-control" placeholder="Born date" required>
    
    <button class="btn btn-lg btn-primary btn-block" type="submit">Sign up</button>
</form

I've two classes: UserRegisterForm and UserRegistrationAction

UserRegisterForm:

package com.github.jcpp.jathenaeum.action;

import org.apache.struts.action.ActionForm;

public class UserRegisterForm extends ActionForm{

    private static final long serialVersionUID = 1;
    
    /*ATTRIBUTES of the form fields*/
    
    /*METHODS Get and Set*/
    
    public UserRegisterForm() {
        super();
    }
}

UserRegistrationAction:

package com.github.jcpp.jathenaeum.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.github.jcpp.jathenaeum.Utente;
import com.github.jcpp.jathenaeum.db.dao.UtenteDAO;
import com.github.jcpp.jathenaeum.exceptions.RegistrationException;

public class UserRegistrationAction extends Action{

    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        
        //boolean action_perform = false;
        String action_target = null;
        Random rnd = new Random();
        UtenteDAO userDao = new UtenteDAO();
        
        Utente user = new Utente();
        //ActionMessages errors_mesg = new ActionMessages();
        UserRegisterForm uf = (UserRegisterForm) form;
        if(form != null){
            user.setEmail(uf.getEmail());
            user.setPassword(uf.getPassword());
            user.setNome(uf.getName());
            user.setCognome(uf.getSurname());
            user.setDataNascita(uf.getBornDate());
            user.setNumeroTessera(rnd.nextInt(999999)+1);
            
            
            try{
                if(userDao.register(user)){
                    action_target = "success";
                }
                
            }catch(Exception e){
                action_target = "failed";
                throw new RegistrationException();
            }
        }

        return mapping.findForward(action_target);
    }

in my struts-config.xml I've:

<form-beans>
        <form-bean name="registerform" type="com.github.jcpp.jathenaeum.action.UserRegisterForm"/>
</form-beans>

<action-mappings>
        <action path="/index" type="org.apache.struts.actions.ForwardAction" parameter="page.index" />
        <action path="/signin" type="org.apache.struts.actions.ForwardAction" parameter="page.signin" />
        <action path="/signup" type="org.apache.struts.actions.ForwardAction" parameter="page.signup" />
        <action
                path="/doRegister"
                type="com.github.jcpp.jathenaeum.action.UserRegistrationAction"
                name="registerform"
                scope="request"
                validate="true"
                input="signup">
                <forward name="input" path="/index.jsp"/>
                <forward name="success" path="/welcome.jsp"/>
                <forward name="failure" path="/index.jsp"/>
            </action>
    </action-mappings>

My errors report is:

Why do I've this problem?


Solution

  • The problem was the action parameter in the form: it must be doRegister.do in my case, and in struts-config.xml the action path is equal to /doRegister. So, see the following code:

    <form action="doRegister.do" >
    [...]
    </form>
    

    and in the struts-config.xml

    <action
                    path="/doRegister" <!-- LOOK HERE -->
                    type="com.github.jcpp.jathenaeum.action.UserRegistrationAction"
                    name="registerform"
                    scope="request"
                    validate="true"
                    input="/signup.jsp">
                   <!-- <forward name="input" path="/index.jsp"/>
                    <forward name="success" path="/signin.jsp"/>
                    <forward name="failed" path="/signup.jsp"/> -->
                </action>