I have a java web application project with Spring framework in netbeans and I’m trying to use Spring validation.
What my application does is raise a support ticket.
I have read several tutorials and I’ve gotten the form is not sent until the fields are filled. The problem is that I can not get to show error messages which I defined in the class “Validator”.
That is, if required fields are not filled the form is not sent, but the error message does not appear anywhere.
Can someone tell me what I’m doing wrong?
These are my codes:
SupportTicket Class:
package controller;
public class SupportTicket
{
private String SubmissionStatus;
private String ReportNumber;
private String SupportType;
private String WithContract;
private String Priority;
private String SupportTicketID;
private String CustomerName;
private String SenderName;
private String SenderLastName;
private String ContactMail;
private String ContactPhone;
private String Description;
public String submitTicket()
{
//Do something
}
//setters and getters for private members
public String getContactMail() {
return ContactMail;
}
public void setContactMail(String ContactMail) {
this.ContactMail = ContactMail;
}
public String getContactPhone() {
return ContactPhone;
}
public void setContactPhone(String ContactPhone) {
this.ContactPhone = ContactPhone;
}
public String getCustomerName() {
return CustomerName;
}
public void setCustomerName(String CustomerName) {
this.CustomerName = CustomerName;
}
public String getSenderName() {
return SenderName;
}
public void setSenderName(String SenderName) {
this.SenderName = SenderName;
}
public String getDescription() {
return Description;
}
public void setDescription(String Description) {
this.Description = Description;
}
public String getSubmissionStatus() {
return SubmissionStatus;
}
public void setSubmissionStatus(String SubmissionStatus) {
this.SubmissionStatus = SubmissionStatus;
}
public String getReportNumber() {
return ReportNumber;
}
public void setReportNumber(String ReportNumber) {
this.ReportNumber = ReportNumber;
}
public String getSupportTicketID() {
return SupportTicketID;
}
public void setSupportTicketID(String SupportTicketID) {
this.SupportTicketID = SupportTicketID;
}
public String getPriority() {
return Priority;
}
public void setPriority(String Priority) {
this.Priority = Priority;
}
public String getSupportType() {
return SupportType;
}
public void setSupportType(String SupportType) {
this.SupportType = SupportType;
}
public String getWithContract() {
return WithContract;
}
public void setWithContract(String WithContract) {
this.WithContract = WithContract;
}
public String getSenderLastName() {
return SenderLastName;
}
public void setSenderLastName(String SenderLastName) {
this.SenderLastName = SenderLastName;
}
}
SupportTicketValidator class:
package controller;
import org.springframework.validation.Errors;
import org.springframework.validation.ValidationUtils;
import org.springframework.validation.Validator;
public class SupportTicketValidator implements Validator
{
@Override
public boolean supports(Class aClass)
{
return SupportTicket.class.equals(aClass);
}
@Override
public void validate(Object target, Errors errors)
{
ValidationUtils.rejectIfEmptyOrWhitespace(errors, "SenderName", "SenderName.required", "Sender Name is required");
ValidationUtils.rejectIfEmptyOrWhitespace(errors, "SenderLastName", "SenderLastName.required", "Sender Last Name is required");
ValidationUtils.rejectIfEmptyOrWhitespace(errors, "ContactMail", "ContactMail.required", "E-mail is required");
ValidationUtils.rejectIfEmptyOrWhitespace(errors, "CustomerName", "CustomerName.required", "Customer Name is required");
ValidationUtils.rejectIfEmptyOrWhitespace(errors, "ContactPhone", "ContactPhone.required", "Phone is required");
}
}
SupportTicketController Class:
package controller;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;
import service.SupportTicketService;
public class SupportTicketController extends SimpleFormController
{
public SupportTicketController()
{
setCommandClass(SupportTicket.class);
setCommandName("supportTicket");
setSuccessView("resultView");
setFormView("inputView");
}
@Override
protected ModelAndView onSubmit(Object command) throws Exception
{
SupportTicket supportTicket = (SupportTicket)command;
ModelAndView mv = new ModelAndView(getSuccessView());
mv.addObject("SubmissionStatus", supportTicket.submitTicket());
mv.addObject("ReportNumber", supportTicket.getReportNumber());
mv.addObject("CustomerName", supportTicket.getCustomerName());
mv.addObject("SupportType", supportTicket.getSupportType());
mv.addObject("WithContract", supportTicket.getWithContract());
mv.addObject("SenderName", supportTicket.getSenderName());
mv.addObject("SenderLastName", supportTicket.getSenderLastName());
mv.addObject("ContactMail", supportTicket.getContactMail());
mv.addObject("ContactPhone", supportTicket.getContactPhone());
mv.addObject("Description", supportTicket.getDescription());
mv.addObject("Priority", supportTicket.getPriority());
return mv;
}
}
Dispatcher-Servlet.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
<bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"/>
<bean id="supportTicketValidator" class="controller.SupportTicketValidator" />
<bean class="controller.SupportTicketController" p:supportTicketService-ref="supportTicketService" p:validator-ref="supportTicketValidator"/>
<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="index.htm">indexController</prop>
</props>
</property>
</bean>
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/WEB-INF/jsp/"
p:suffix=".jsp" />
<bean name="indexController"
class="org.springframework.web.servlet.mvc.ParameterizableViewController"
p:viewName="index" />
</beans>
InputView.jsp:
<%@taglib uri="http://www.springframework.org/tags" prefix="spring" %>
<%@page contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<style type="text/css">
h1 {
color: #3990bd;
font-size: 22px;
line-height: 22px;
font-family: Arial, Helvetica, sans-serif;
font-weight: normal;
text-decoration: none;
font-family: Arial, Helvetica, sans-serif;
}
p {font-family:Arial, Helvetica, sans-serif ; font-size: 12px;color:#666; font-weight:bold}
.blue {font-family:Arial, Helvetica, sans-serif ; font-size: 12px;color: #3990bd}
.error {font-family:Arial, Helvetica, sans-serif; font-size: 9px; color:#F00}
</style>
</head>
<body>
<h1>Please give us some information:</h1>
<p> </p>
<spring:nestedPath path="supportTicket">
<form id="inputForm" action="" method="post">
<form:errors path="*" cssClass="error"/>
<table width="640" border="0" cellpadding="5" cellspacing="5">
<tr>
<td><p>Name:</p></td>
<td><spring:bind path="SenderName">
<input type="text" name="${status.expression}" class="blue" value="${status.value}" size="50">
</spring:bind>
<form:errors path="SenderName" cssClass="error"/>
</td>
</tr>
<tr>
<td><p>Last Name</p></td>
<td><spring:bind path="SenderLastName">
<input type="text" name="${status.expression}" class="blue" value="${status.value}" size="50">
</spring:bind>
</td>
</tr>
<tr>
<td><p>Email:</p></td>
<td><spring:bind path="ContactMail">
<input type="text" name="${status.expression}" class="blue" value="${status.value}" size="50">
</spring:bind>
</td>
</tr>
<tr>
<td><p>Customer name:</p></td>
<td></p>
<spring:bind path="CustomerName">
<input type="text" name="${status.expression}" class="blue" value="${status.value}" size="50">
</spring:bind>
</td>
</tr>
<tr>
<td><p>Phone:</p></td>
<td><spring:bind path="ContactPhone">
<input type="text" name="${status.expression}" class="blue" value="${status.value}" size="50">
</spring:bind>
</td>
</tr>
<tr>
<td><p>Support needed:</p></td>
<td><spring:bind path="SupportType">
<select name="${status.expression}" class="blue" value="${status.value}">
<option>App error</option>
<option>New app</option>
<option>Wrong result</option>
<option>Other</option>
</select>
</spring:bind>
</td>
</tr>
<tr>
<td><p>Do you have a support contract?</p></td>
<td><spring:bind path="WithContract">
<select name="${status.expression}" class="blue" value="${status.value}">
<option>No</option>
<option>Yes</option>
</select>
</spring:bind>
</td>
</tr>
<tr>
<td><p>Priority:</p></td>
<td><spring:bind path="Priority">
<select name="${status.expression}" class="blue" value="${status.value}">
<option>Low</option>
<option>Medium</option>
<option>High</option>
</select>
</spring:bind>
</td>
</tr>
<tr>
<td><p>Requirement description:</p></td>
<td> <spring:bind path="Description">
<textarea name="${status.expression}" class="blue" value="${status.value}" rows="5" cols="52">
</textarea>
</spring:bind>
</td>
</tr>
<tr>
<td> </td>
<td><div align="right">
<input type="submit" class="blue" value="Submit Ticket">
</div>
</td>
</tr>
</table>
<p><br>
</p>
</form>
</spring:nestedPath>
</body>
</html>
As I mentioned, the validation works (I think) because the form is not submited until i fill the required fields, but the error messages are not shown with that < form:errors > tags.
Can you give me an advice to solve my problem?
PART 1 OF 2
I have moved my app to Spring 3 style and i have been reading a lot about this new style.
Now my app looks a bit different.
The objective of the application is to integrate a Salesforce.com org with Java and allow users to create records of an object that acts as a support ticket.
The user is presented with a form in which it must fill some fields to describe his request and contact information, once the form is validated, the information is sent to the Salesforce org and the record is created.
In summary, the application is used to create records of a type called “Ticket_Soporte__c” in a Salesforce org, but can be easily adapted to other needs.
This is the JSP that shows the input form, it uses jquery mb Tooltips. The input is validated in both sides (client with JS and server with Spring validation utils) to ensure the data integrity if JS is not available or something like that. This form also allows file upload to a server, if a file is uploaded then an attachment will be created in salesforce and it will be linked to the ticket record as a “Note and Attachment” under a related list.
The mail is validated on blur using regular expressions.
tickets.jsp
The next JSP is shown when the ticket is registered succesfully in Salesforce:
sucess.jsp
And this one is shown when something goes wrong in ticket registration:
error.jsp
I have created packages in order to mantain sources under control.
“config” package is used to define constants. I have a class named Configuracion where i declare those constants:
Configuracion.java
the “controller” package contains two classes (SupportTicket and SupportTicketController).
SupportTicket.java:
SupportTicketController.java: