代码之家  ›  专栏  ›  技术社区  ›  x80486

Struts2提交表单不导航到新视图

  •  0
  • x80486  · 技术社区  · 6 年前

    我在用 Struts2 (版本 2.5.14.1 )与 Spring Tiles 插件。

    这是我的(相关的) struts.xml 配置:

    <package name="enrollment" namespace="/enrollment" extends="struts-bean-validation">
      <result-types>
        <result-type name="tiles" class="org.apache.struts2.views.tiles.TilesResult" />
      </result-types>
    
      <action name="start" class="io.shido.struts.action.enrollment.StartAction">
        <result name="success" type="tiles">enrollment.start.tiles</result>
      </action>
      <action name="personal-info" class="io.shido.struts.action.enrollment.PersonalInfoAction">
        <result name="input" type="tiles">enrollment.personal-info.tiles</result>
        <result name="success" type="tiles">enrollment.billing-info.tiles</result>
      </action>
      <action name="billing-info" class="io.shido.struts.action.enrollment.BillingInfoAction">
        <result name="input" type="tiles">enrollment.billing-info.tiles</result>
        <result name="success" type="tiles">enrollment.finish.tiles</result>
      </action>
      <action name="finish" class="io.shido.struts.action.enrollment.FinishAction">
        <result name="success" type="tiles">enrollment.finish.tiles</result>
      </action>
    </package>
    

    …包含以下表单之一的jsp文件:

    <%@ taglib prefix="s" uri="/struts-tags" %>
    
    <link rel="stylesheet" href="<s:url value='/resources/styles/enrollment.styles.css' />">
    
    <s:form cssClass="form-input-info" action="personal-info" method="post">
      <legend><s:text name="legend.text" /></legend>
      <hr>
      <s:textfield cssClass="form-control" key="textfield.first-name" name="personalInfo.firstName" />
      <s:textfield cssClass="form-control" key="textfield.last-name" name="personalInfo.lastName" />
      <s:textfield cssClass="form-control" key="textfield.email" name="personalInfo.email" />
      <s:textfield cssClass="form-control" key="textfield.phone-number" name="personalInfo.phoneNumber" />
      <s:textfield cssClass="form-control" key="textfield.age" name="personalInfo.age" />
      <s:submit class="btn btn-primary btn-lg btn-block" key="submit.continue" />
    </s:form>
    

    …最后是(超级简单) Action 班级:

    package io.shido.struts.action.enrollment;
    
    // imports
    
    public final class PersonalInfoAction extends ActionSupport {
      private static final long serialVersionUID = 3560814234131884357L;
    
      private final Logger logger = LogManager.getLogger(this.getClass());
    
      private PersonalInfoHandler handler;
    
      @Valid
      private PersonalInfo personalInfo;
    
      @Override
      public String execute() {
        // If personalInfo is not null is guaranteed to be valid due to Bean Validation contraints
        if (null != personalInfo) {
          logger.debug("Processing personal info...");
          handler.save(personalInfo);
          return Action.SUCCESS;
        }
        logger.info("Collecting personal info...");
        return Action.INPUT;
      }
    
      public PersonalInfoHandler getHandler() { return handler; }
    
      public void setHandler(final PersonalInfoHandler handler) { this.handler = handler; }
    
      public PersonalInfo getPersonalInfo() { return personalInfo; }
    
      public void setPersonalInfo(final PersonalInfo personalInfo) {
        this.personalInfo = personalInfo;
        this.personalInfo.normalize();
      }
    }
    

    问题是,每当我提交该表单时,我都希望bean验证能够跳出并检查所有带注释的字段(检查,这正在发生),当一切正常时,将流发送到 /WEB-INF/content/enrollment/billing-info.jsp (在瓷砖中定义为 enrollment.billing-info.tiles )……但我只看到(几乎)标签错误的“账单信息”表格。见下图:

    enter image description here

    url和标签都应该已经更改了。把樱桃放在上面,如果我点击 Continue 再按一次,表格就会正确显示。

    有线索吗?

    1 回复  |  直到 6 年前
        1
  •  1
  •   beendr    6 年前

    我没有直接的答案,但是您可以尝试使用重定向操作,而不是多次使用tiles。

    所以你可以在这里试试:

     <action name="personal-info" class="io.shido.struts.action.enrollment.PersonalInfoAction">
        <result name="input" type="tiles">enrollment.personal-info.tiles</result>
        <result name="success" type="redirectAction">
            <param name="actionName">billing-info</param>
        </result>
     </action>
    

    如果需要传输某种ID来显示数据,可以在struts.xml中使用其他参数,如下所示:

    <!-- rest as above -->
    <result>
    <param name="param1">${value1}</param>
    </result> 
    

    这个 param1 必须在两个操作中都有有效的getter/setter方法才能传输信息。

    推荐文章