Struts2提交表单无法导航到新视图

时间:2018-06-29 01:23:04

标签: struts2 struts apache-tiles tiles-3

我在Struts22.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验证启动并检查所有带注释的字段(检查,这种情况正在发生),并且一切正常后,将流程发送到{{1} }(在图块中由/WEB-INF/content/enrollment/billing-info.jsp定义)...但是我所看到的几乎是带有错误标签的“计费信息”表格。见下图:

enter image description here

URL和标签也应该已更改。要将樱桃放在顶部,如果我再次单击enrollment.billing-info.tiles按钮,则会正确显示该表单。

有任何线索吗?

1 个答案:

答案 0 :(得分:1)

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

所以您可以在这里尝试:

 <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方法,才能传输信息。