如何在使用xml验证时在struts中的字段旁边显示actionerror消息

时间:2015-09-29 15:08:29

标签: jsp struts2 xml-validation model-driven

这就是我所做的 我创建了一个jsp页面      持卡人

      <%@ page contentType="text/html; charset=UTF-8"%>
        <%@ taglib prefix="s" uri="/struts-tags"%>
      <!DOCTYPE HTML>
         <%
     String client = (String) request.getAttribute("clientId");
      %>
       <html>
       <head>
     <style type="text/css">
      h3 {
       font-family: Calibri;
       font-size: 22pt;
      font-style: normal;
font-weight: bold;
color: SlateBlue;
text-align: center;
text-decoration: underline
 }

 table {
font-family: Calibri;
color: white;
font-size: 11pt;
font-style: normal;
text-align:;
background-color: SlateBlue;
border-collapse: collapse;
border: 2px solid navy;
text-align:;
}

   table.inner {
border: 0px
 }

  table {
margin: 0 auto;
  }
   </style>
  <script type="text/javascript">
  function clear()
  {
  document.getElementsByName('custIdNum')[0].value = "";
  document.getElementsByName('passport')[0].value = "";
  document.getElementsByName('country')[0].value = "";
  document.getElementsByName('firstName')[0].value = "";
  document.getElementsByName('middleName')[0].value = "";
  document.getElementsByName('lastName')[0].value = "";
  document.getElementsByName('dob')[0].value = "";
  document.getElementsByName('officeNo')[0].value = "";
    document.getElementsByName('mobileNo')[0].value = "";
  document.getElementsByName('homeNo')[0].value = "";
  document.getElementsByName('email')[0].value = "";
  document.getElementsByName('postalId')[0].value = "";
  document.getElementsByName('address')[0].value = "";
  document.getElementsByName('state')[0].value = "";
  document.getElementsByName('city')[0].value = "";
  document.getElementsByName('clientNo')[0].value = "";
 }
 function changeStyle() {
    var v1 ="<%=client%>";
    if (!(v1 == "null")) {
        document.getElementsByName('clientNo')[0].value = v1;
        alert("client " + v1 + " registered successfully");
    }
  }
  </script>
  </head>

<body onload="changeStyle();">
<h3>Card Holder Details</h3>   
  <s:actionerror/>


<s:form action="registerClient">
    <s:div class="table">
        <tr><td><s:textfield name="clientNo" label="CLIENT ID" /></td></tr>
        <s:textfield name="custIdNum" label="CUSTOMER ID NUMBER" />
        <s:textfield name="passport" label="PASSPORT NUMBER" />
        <s:select name="country" label="NATIONALITY"
            list="{'BOTSWANA','NIGERIA','KENYA','S AFRICA','ZIMBABWE'}" />
        <s:textfield name="firstName" label="FIRST NAME" />
        <s:textfield name="middleName" label="MIDDLE NAME" />
        <s:textfield name="lastName" label="LAST NAME" />
        <s:textfield name="dob" label="BIRTHDAY" />
        <s:radio list="{'male','female'}" name="gender" label="GENDER"></s:radio>
        <s:textfield name="officeNo" label="OFFICE NUMBER" />
        <s:textfield name="mobileNo" label="MOBILE NUMBER" />
        <s:textfield name="homeNo" label="HOME NUMBER" />
        <s:textfield name="email" label="EMAIL ID" />
        <s:textfield name="postalId" label="POSTAL CODE" />
        <s:textfield name="city" label="CITY" />
        <s:textarea name="address" label="ADDRESS" />
        <s:textfield name="state" label="STATE" />
        <tr>
            <td colspan="2"><s:submit value="submit" theme="simple"/></td>
            <td><s:submit type="button" onclick="clear();return false;"
                    value="Reset" /></td>
        </tr>
    </s:div>
</s:form>

 </body>
 </html>

这是我的xml验证文件

      <!DOCTYPE validators PUBLIC "-//OpenSymphony Group//XWork Validator       1.0.2//EN"
      "http://struts.apache.org/dtds/xwork-validator-1.0.2.dtd">
      <validators>
<field name="custIdNum">
    <field-validator type="requiredstring">
        <message>Customer ID is required.</message>
    </field-validator>
</field>    
<field name="passport">
    <field-validator type="requiredstring">
        <message>Passport Number is required.</message>
    </field-validator>
</field>
<field name="firstName">
    <field-validator type="requiredstring">
        <message>Please Enter First Name.</message>
    </field-validator>
</field>    
<field name="lastName">
    <field-validator type="requiredstring">
        <message>Please Enter Last Name.</message>
    </field-validator>
</field>
<field name="gender">
    <field-validator type="requiredstring">
        <message>Please select a gender</message>
    </field-validator>
</field>
<field name="officeNo">
    <field-validator type="requiredstring">
        <message>Please Enter Office number.</message>
    </field-validator>
</field>
 <field name="mobileNo">
    <field-validator type="requiredstring">
        <message>Please Enter personal mobile number</message>
    </field-validator>
</field>
<field name="postalId">
    <field-validator type="requiredstring">
        <message>Please Enter Postal code</message>
    </field-validator>
</field>
<field name="email">
    <field-validator type="email">
        <message>email required</message>
    </field-validator>
</field>

 </validators>

当我运行此页面时,我收到字段上的错误消息,但我想在每个字段旁边显示它们。 请帮忙。

这是我的动作类

package com.struts.lifewardscard;
 import java.sql.CallableStatement;
 import java.sql.Connection;
  import java.sql.ResultSet;
 import java.sql.Statement;
import java.util.ArrayList;
import java.util.Map;

 import javax.servlet.http.HttpSession;
 import javax.servlet.http.HttpServletRequest;

 import org.apache.struts2.ServletActionContext;
 import org.apache.struts2.interceptor.ServletRequestAware;
 import org.apache.struts2.interceptor.SessionAware;

  import oracle.jdbc.driver.OracleTypes;

  import com.struts.lifewardscard.CardHolderForm;
 import com.opensymphony.xwork2.ActionSupport;
 import com.opensymphony.xwork2.ModelDriven;

 public class CardHolderRegistration extends ActionSupport implements   ModelDriven{

CardHolderForm cardform=new CardHolderForm();
public String execute() throws Exception{
    String clientId=null;
    HttpServletRequest request = ServletActionContext.getRequest();
    clientId=saveCardHolderDetails();       
    request.setAttribute("clientId", clientId);
    return "success";

}

@Override
public Object getModel() {
    // TODO Auto-generated method stub
    return cardform;
}

这是验证后生成的示例代码

  <tr errorFor="registerClient_custIdNum">
  <td align="center" valign="top" colspan="2"><span    class="errorMessage">Customer ID is required.</span></td>
  </tr>
  <tr>
<td class="tdLabel"><label for="registerClient_custIdNum"   class="errorLabel">CUSTOMER ID NUMBER:</label></td>
 <td
 ><input type="text" name="custIdNum" value=""   id="registerClient_custIdNum"/></td>
 </tr>

我希望错误显示消息与输入行位于同一行 不在新的一行。

1 个答案:

答案 0 :(得分:1)

  

你说我能用CSS做到这一点你能不能给我一个例子。

几乎所有事情都可以通过CSS完成。然而,在这种情况下,它不干净并且有缺点(主要是由于colspan),所以有两个更好的选择:

  • 使用简单主题并自行编写标记,如下所示:

    <s:textfield id="foo" key="foo" />
    <s:fielderror   fieldName="foo" />
    
  • 创建自定义主题,分支XHTML并仅更改您想要区分的内容,例如fieldError放置。

然而,既然你问过,并且作为纯粹的学术练习不在家尝试这个),这里是:

table {
  margin: 10px;
  border: 1px solid silver;
}
tr {
  background: #eee;
}
tr[errorFor] {
  color: red;
}
tr[errorFor]+tr {
  color: forestGreen;
}
#example1 tr[errorFor] {
  display: inline-block;
}
#example1 tr[errorFor]+tr {
  display: inline-block;
}
#example2 tr[errorFor] {
  float: right;
}
#example2 tr[errorFor]+tr {
  float: right;
}
<table>
  <tr errorFor="registerClient_custIdNum">
    <td align="center" valign="top" colspan="2"><span class="errorMessage">Standard error label</span>
    </td>
  </tr>
  <tr>
    <td class="tdLabel">
      <label class="errorLabel">Label</label>
    </td>
    <td>
      <input type="text" />
    </td>
  </tr>
  <tr>
    <td class="tdLabel">
      <label>foo</label>
    </td>
    <td>
      bar
    </td>
  </tr>
</table>

<table id="example1">
  <tr errorFor="registerClient_custIdNum">
    <td align="center" valign="top" colspan="2"><span class="errorMessage">Inline-block error label </span>
    </td>
  </tr>
  <tr>
    <td class="tdLabel">
      <label class="errorLabel">Label</label>
    </td>
    <td>
      <input type="text" />
    </td>
  </tr>
  <tr>
    <td class="tdLabel">
      <label>foo</label>
    </td>
    <td>
      bar
    </td>
  </tr>
</table>

<table id="example2">
  <tr errorFor="registerClient_custIdNum">
    <td align="center" valign="top" colspan="2"><span class="errorMessage">Floating error label</span>
    </td>
  </tr>
  <tr>
    <td class="tdLabel">
      <label class="errorLabel">Label</label>
    </td>
    <td>
      <input type="text" />
    </td>
  </tr>
  <tr>
    <td class="tdLabel">
      <label>foo</label>
    </td>
    <td>
      bar
    </td>
  </tr>
</table>