I'm using Struts 2 for developing a web application. I have action class implementing ActionSupport
. My problem is that when I enter some text error messages are not getting cleared as well as form is also not getting submitted.
Here's what I have:
CustomerAction.java
:
public class CustomerAction extends ActionSupport
{
private Customer customer = new Customer();
private String customerName;
private String emailID;
public String getCustomerName() {
return customerName;
}
public void setCustomerName(String customerName) {
this.customerName = customerName;
}
public Customer getCustomer() {
return customer;
}
/**
* @param customer
*/
public void setCustomer(Customer customer) {
this.customer = customer;
}
public String getEmailID() {
return emailID;
}
public void setEmailID(String emailID) {
this.emailID = emailID;
}
public void validate()
{
if (customerName == null)
{
addFieldError("customer.customerName","Required");
}
if (emailID == null || emailID.trim().equals(""))
{
addFieldError("customer.emailID","Email is Required");
}
}
Struts.xml
:
<action name="addCustomer" class="com.yell.hibu.action.CustomerAction" method="execute">
<interceptor-ref name="params">
<param name="excludeParams">dojo\..*,^struts\..*</param>
</interceptor-ref>
<interceptor-ref name="validation">
<param name="excludeMethods">input,back,cancel,browse</param></interceptor-ref>
<interceptor-ref name="workflow">
<param name="excludeMethods">input,back,cancel,browse</param>
</interceptor-ref>
<result name="input">/registration.jsp</result>
<result name="success">/success.jsp</result>
</action>
</package>
</struts>
regisration.jsp
:
<%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Customer Registration Form</title>
<s:head/>
</head>
<body>
<br>
<br>
<center>
<table width="450" align="center">
<tr>
<td align="center" height="40"><font
style="color: #003300; font-family: fantasy !important;"><strong
style="color: #003300 !important;">Registration Form</strong></font></td></tr>
<tr>
<td width="1000" align="center"><br> <font color="#003300"><s:actionerror/>
<s:form action="addCustomer" id="register-form" method="post" theme="xhtml" enctype="multipart/form-data">
<s:textfield name="customer.customerID" label="Customer ID" size="15" />
<s:textfield name="customer.customerName" label="Customer Name:" maxlength="10"/>
</s:form>
</body>
</head>
</html>
To get the form submitted you need to pass the validate
method not adding there errors in it. if you adding errors or errors are added via xml validation then by default framework will return the input
result. it is /registration.jsp
in your action. The execute
method not executed then. You get that errors added because the action fields are not initialized. In the JSP you are referencing object customer
and using OGNL notation the values would be set by the params
interceptor inside it. So, to validate this values you should write
public void validate() {
if (customer.getCustomerName() == null || customer.getCustomerName().trim().equals("")) {
addFieldError("customer.customerName", "Required");
}
}
If you don't not use the customer
object in the JSP then you should use OGNL notation for the field names that references the action attributes.
then validate
public void validate() {
if (customerName == null || customerName.trim().equals("")) {
addFieldError("customerName", "Required");
}
// the same for email ID
}
thought you've got the idea.