Hello,
I am using Spring MVC. i have some form elements and image upload. before submit button i have to validate the image
(type, Height & width and size).
only accept JPG,JPEG,PNG,GIF file formats
400*400 ---height * width of the image
10000---size of the file.
Using jquery& ajax how to validate the image file?.Please help me. very Urgent Please
the project Details are as follows,
I have a form with some input elements and image upload button. using Spring MVC i validated the form elements and inserted into sql Database. successfully i completed. The modified requirement is before click a submit button i have to validate the image
instead of spring Validator i want to use Jquery -Ajax.
But here i attach sample program which i was tried to call spring mvc. i couldn't get the result. please any one try to help me.
here is my Controller
package test.mydir;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;
public class TestBeanController extends SimpleFormController {
@Override
protected ModelAndView onSubmit(HttpServletRequest request,
HttpServletResponse response, Object command, BindException errors)
throws Exception {
TestBean testBean=(TestBean)command;
String name=testBean.getName();
System.out.print(name);
return new ModelAndView("success");
}
Here is My Service Class
package test.mydir;
import java.io.PrintWriter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.Logger;
import org.springframework.validation.BindException;
import org.springframework.web.servlet.mvc.SimpleFormController;
public class TestBeanService extends SimpleFormController {
private static final Logger log = Logger.getLogger(TestBeanService.class);
protected boolean isFormChangeRequest(HttpServletRequest request)
{
log.info("isfileupload method");
return isFileUploadRequest(request);
}
private boolean isFileUploadRequest(HttpServletRequest request) {
log.info("isfileupload method body");
return (!request.getParameter("name").equals(null));
}
protected void onFormChange(HttpServletRequest request,HttpServletResponse responses,
Object command,BindException bind){
TestBean testBean=(TestBean)command;
log.info("inside formchange");
if(isFileUploadRequest(request))
{
PrintWriter out=new PrintWriter(System.out);
out.print(testBean.getName());
System.out.print("testBean.getName");
}
}
}
Here is my Bean
package test.mydir;
public class TestBean {
private String name;
private String name1;
public String getName1() {
return name1;
}
public void setName1(String name1) {
this.name1 = name1;
}
public TestBean() {
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
here is my
Jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<script type="text/javascript" src="<%=request.getContextPath()%>jquery-1.6.2.js"></script>
<script type="text/javascript" src="<%=request.getContextPath()%>/js/jquery.form.js"></script>
<script>
function call(){
var queryString = $('#myForm').formSerialize();
$.post('testBeanService.html', queryString,
function(data){
alert(data);
});
}
</script>
</head>
<body>
<form:form id="myForm" method="post" commandName="event">
Name: <form:input path="name" id="name" onchange="call();" />
<form:input path="name1" id="name1" />
<input type="submit" value="Submit Comment" />
</form:form>
</body>
</html>
Here is my DispatcherServlet
<?xml version="1.0" encoding="UTF-8"?>
<beans>
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass"><value>org.springframework.web.servlet.view.JstlView</value></property>
<property name="prefix" value="/jsp/"/>
<property name="suffix" value=".jsp"></property>
</bean>
<bean name="/testBeanService.html" class="test.mydir.TestBeanService"/>
<bean name="/eventCreation.html" class="test.mydir.TestBeanController">
<property name="commandName" value="event"/>
<property name="commandClass" value="test.mydir.TestBean"/>
<property name="formView" value="test"/>
<property name="successView" value="success"/>
</bean>
</beans>
Thanks