问题
here's what i did i have created a jsp page cardholder
<%@ 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>
here is my xml validation file
<!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>
when i run this page i am getting error messages over the fields but i want to display them beside each field. please help.
this is my action class
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;
}
this is sample code generated after validation
<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>
i want the error display message to be on the same row as input row not in a new row.
回答1:
you are saying i can be able to do it with CSS alone can you please give me an example.
Almost everything can be done with CSS alone. In this case, however, it's not clean and has drawbacks (mainly due to the colspan
), so two better alternatives are:
use the simple theme and write the mark-up by yourself, like this:
<s:textfield id="foo" key="foo" /> <s:fielderror fieldName="foo" />
create a custom theme, forking the XHTML one and changing only the things you want to differ, like fieldError placement.
However, since you asked, and as a pure academic exercise (don't try this at home), here it is:
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>
来源:https://stackoverflow.com/questions/32847576/how-to-display-actionerror-messages-beside-the-field-in-struts-while-using-xml-v