简体   繁体   English

struts2动作映射配置

[英]struts2 action mapping configuration

I'm trying a simple app in struts2 on gae but facing problems in submitting forms. 我正在尝试struts2中的简单应用程序,但在提交表单时遇到问题。

struts.xml conf 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. 我正在使用spring加载动作。

When I hit the url /auth/signup.html I get proper signup page. 当我点击url /auth/signup.html时,我会得到正确的注册页面。 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. 它让我回到同一个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. 现在主要的问题是:即使我在我的validate方法中添加fielderror而且我已经为我的表单使用了struts标记,我也看不到错误。

 @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 - 也是从这个页面,如果我再次点击/auth/signup.html它给出了这个错误 -

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. 我在conf中不需要'输入'来进行'注册'操作,因为我只会显示注册表单。 Also Except 'postSignup' method I've added the annotation @SkipValidation on each one. 除了'postSignup'方法之外,我在每个方法上都添加了注释@SkipValidation I'm new to struts2. 我是struts2的新手。 Migrating from struts1. 从struts1迁移。 Need help with this. 需要帮助。 Also how do I better organize my struts configurations? 另外,我如何更好地组织我的struts配置? Thanks 谢谢

Here is the jsp 这是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>

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. 如果再次点击auth页面仍然看到表单错误,则表示您正在使用Spring bean的默认“singleton”作用域。 Actions should be "prototype" scope, ie, 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. 您必须同时包含操作和JSP信息。

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? 你更喜欢什么?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM