struts2 action mapping configuration

5.1k views Asked by At

I'm trying a simple app in struts2 on gae but facing problems in submitting forms.

struts.xml conf

<package name="auth" extends="struts-default" namespace="/auth">
    <default-interceptor-ref name="defaultStack"/>
    <action name="signup" class="signup" method="signup">
      <result name="success">/auth/signup.jsp</result>
    </action>
    <action name="postSignup" class="signup" method="create">
      <result name="input">/auth/signup.jsp</result>
    </action>
    <action name="signupConfirm" class="signup" method="signupConfirm"></action>

    <action name="resendCode" class="signup" method="resendCode"/>
</package>

I'm using spring to load actions.

When I hit the url /auth/signup.html I get proper signup page. Now after submitting the form if form validation fails(I'm doing this by a validate method in my action). It returns me back to the same jsp. Now the main problem: I can't see the errors even though I'm adding fielderror in my validate method and I've used struts tags for my form.

 @Override
  public void validate() {

    if(!StringUtil.isValidUserName(userForm.getUserName())){
      addFieldError("userForm.userName", "Invalid username format");
    }else {
      TutorUser user = this.userBc.getUserByUserName(userForm.getUserName());
      if(user!=null){
        addFieldError("userForm.userName", "Username already exist. Please choose a different one.");
      }
    }
    if(!StringUtil.isEmail(userForm.getEmail())){
      addFieldError("userForm.email", "Invalid email provided");
    }
    if(StringUtil.isBlank(userForm.getPassword1()) || StringUtil.isBlank(userForm.getPassword2()) ||
        !userForm.getPassword1().equals(userForm.getPassword2())) {
      addFieldError("userForm.password2", "Passwords do not match");
    }
  }

Also from this page if i hit simply /auth/signup.html again it gives this error -

Problem accessing /auth/signup.html. Reason:

    No result defined for action com.tutorial.action.auth.SignUpAction and result input

I don't need 'input' in my conf for 'signup' action as i will only be displaying signup form. Also Except 'postSignup' method I've added the annotation @SkipValidation on each one. I'm new to struts2. Migrating from struts1. Need help with this. Also how do I better organize my struts configurations? Thanks

Here is the jsp

  <s:form action="postSignup" cssClass="well" namespace="/auth" method="POST">
    <fieldset>
      <div class="control-group">
        <label class="control-label" for="userName">UserName</label>
        <div class="controls">
          <s:textfield cssClass="input-large" id="userName" name="userForm.userName"/>
          <span class="help-inline">Choose a username</span>
        </div>
      </div>
      <div class="control-group">
        <label class="control-label" for="password1">Password</label>
        <div class="controls">
          <s:password cssClass="input-large" id="password1" name="userForm.password1"/>
          <span class="help-inline">Enter your Password</span>
        </div>
      </div>
      <div class="control-group">
        <label class="control-label" for="password2">Confirm Password</label>
        <div class="controls">
          <s:password cssClass="input-large" name="userForm.password2" id="password2"/>
          <span class="help-inline">Confirm above password</span>
        </div>
      </div>
      <div class="control-group">
        <label class="control-label" for="email">Email Address</label>
        <div class="controls">
          <s:textfield cssClass="input-large" id="email" name="userForm.email"/>
          <span class="help-inline">Enter your Email Address</span>
        </div>
      </div>
      <div class="form-actions">
        <s:submit name="action" id="action" value="SignUp" cssClass="btn btn-primary"/>
      </div>

    </fieldset>
  </s:form>
1

There are 1 answers

2
Dave Newton On BEST ANSWER

If you hit the auth page again and still see form errors, it means you're using the default "singleton" scope for the Spring bean. Actions should be "prototype" scope, i.e., created per-request.

The issue with field errors is impossible to address since I cannot see anything related to it other than the addition of the field errors. You must include both action and JSP info.

Regarding "better organization" of config files, it's impossible to know what you mean without further info. Organize how? What's wrong with what you have? What would you prefer?