0
<?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:context="http://www.springframework.org/schema/context"    
xsi:schemaLocation="http://www.springframework.org/schema/beans    
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd    
http://www.springframework.org/schema/context    
http://www.springframework.org/schema/context/spring-context-3.0.xsd">    

<context:component-scan base-package="com.springcurd.*"></context:component-
scan>  

<bean 
class="org.springframework.web.servlet.view.InternalResourceViewResolver">  
<property name="prefix" value="/WEB-INF/jsp/"></property>  
<property name="suffix" value=".jsp"></property>   
</bean>  

<bean id="ds" 
class="org.springframework.jdbc.datasource.DriverManagerDataSource">  
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver">
</property>  
<property name="url" value="jdbc:oracle:thin:@localhost:1521:xe"></property>  

<property name="username" value="system"></property>  
<property name="password" value="tiger"></property>  
</bean>  

<bean id="jt" class="org.springframework.jdbc.core.JdbcTemplate">  
<property name="dataSource" ref="ds"></property>  
</bean>  

<bean id="dao" class="com.springcurd.dao.EmpDao">  
<property name="template" ref="jt"></property>  
</bean>  
<bean id="employeeValidator" 
class="com.springcurd.validator.EmployeeValidator">
</bean>

</beans>  

function validateForm() {
    var x = document.forms["emp"]["name"].value;
    var y = document.forms["emp"]["sal"].value;
    var z = document.forms["emp"]["designation"].value;

    if (x == "") {
        alert("Please enter Name");
        return false;
    }else if(!/^[a-zA-Z]*$/g.test(x)){
        alert("Only characters are allowed");
        return false;

    }else if(y==""){
        alert("Please enter Salary");
        return false;
    }else if (/[^0-9.]/g.test(y) ){
        alert("Only numbers are allowed");
        return false;

    }else if(z==""){
        alert("Please enter your Designation");
        return false;
    }else if (!/^[a-zA-Z]*$/g.test(z)) {
        alert("Only characters are allowed");
        return false;
    }
    }
    function nameValidate(){
    var a = document.forms["emp"]["name"].value;
    if(a == ""){
        alert("Please enter name");
    }else if (!/^[a-zA-Z]*$/g.test(a)){
        alert("Only characters are allowed");
    }else
        alert(a+" "+"Name entered successfully");
     return true;
    }
    function salValidate(){
    var b = document.forms["emp"]["sal"].value;
    if(b == "") {   
        alert("Please enter Salary");
    }else if (/[^0-9.]/g.test(b) ){
        alert("Only numbers are allowed");
    }else
        alert("Salary entered successfully");
    return true;
    }
    function desigValidate(){
     var c = document.forms["emp"]["designation"].value;
     if(c == ""){
         alert("Please enter designation");
     }else if (!/^[a-zA-Z]*$/g.test(c)){ 
            alert("Only characters are allowed");
     }else
         alert("designation entered successfully");
    return true;
    }
    function validate(){
    var d=document.getElementById("dropDown").value;
    alert("you selected"+" "+d);
    }
     <!DOCTYPE html>
     <html>
     <h1>Add New Employee</h1>

    <head>
    <script type="text/javascript" src="empForm.js">

    </script>

    </head>
    <body>
     <form name="emp" action="save.spring" onsubmit="return validateForm()" 
    method="POST" >

    <table >    
         <tr>    
          <td>Name : </td>   
          <td><input type="text"  name="name" onblur="nameValidate()"/></td>  
         </tr>    
         <tr>    
          <td>Salary :</td>    
          <td><input  type="text" name="sal" id="s" onblur="salValidate()"/>
    </td>  
         </tr>   
         <tr>    
          <td>Designation :</td>    
          <td><input  type="text" name="designation" onblur="desigValidate()"/>
    </td>  
         </tr>   
         <tr>    
          <td> </td>    
          <td><br><input type="submit" value="Save" /></td>    
         </tr>    
        </table> 

    </form>
    <select id="dropDown" onchange="validate()">
    <option value="Hyd">Hyd</option>
    <option value="Bnglr">Bnglr</option>
    <option value="Chennai">Chennai</option>
    </select>
    <body>
    </html>

my JS file not accessing with html.i used my JS file name in /springcurd/WebContent/WEB-INF/js/empForm.js path and HTML page in /springcurd/WebContent/WEB-INF/jsp/empform.jsp path.can ayone tell me what is the problem.

alessandrio
  • 4,282
  • 2
  • 29
  • 40
johnny
  • 51
  • 1
  • 12

3 Answers3

0

I think your script tag should be like this.You have to point to the proper directory to get the js file

<script type="text/javascript" src="springcurd/js/empForm.js">
0

Because you are using the Spring framework your file structure should look like this as a best practice:

springcurd
|
├── resources
|   └── empForm.js
|
└── WEB-INF
    └── jsp
        └── empform.jsp

In your HTML code you should add the script like this:

<spring:url value="/resources/empForm.js" var="empFormJS" />
<script src="${empFormJS}"></script>

In the XML file which configures your bean, you should add these two lines at the end of the file before the </beans> tag.

<mvc:resources mapping="/resources/**" location="/resources/" />
<mvc:annotation-driven />

To see the changes please perform Project -> Clean and re-deploy your application.


Update

The XML file looks like the MVC definitions are missing. Would you, please, try to replace that code in the XML file and let me know if it works for you?

<?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:context="http://www.springframework.org/schema/context"  
    xmlns:mvc = "http://www.springframework.org/schema/mvc"  
    xsi:schemaLocation="http://www.springframework.org/schema/beans    
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
        http://www.springframework.org/schema/context    
        http://www.springframework.org/schema/context/spring-context-3.0.xsd">

And at the end of the file change it like that:

    <mvc:resources mapping = "/resources/**" location="/resources/" />
    <mvc:annotation-driven/>

</beans>

If that still doesn't help try to register this bean additionally:

<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping" />
LoHer
  • 172
  • 1
  • 13
  • in web-inf i took two folders one for jsp and one for js . i change my js file into jsp folder but no use. – johnny May 26 '17 at 05:30
  • Can you please paste the URL, with which you are accessing the content of the empform.jsp? That would help me to figure the path out, because the path to the js file is normally relative to the requested URL. – LoHer May 26 '17 at 07:11
  • Thank you, I've updated the answer. Does it help you now? – LoHer May 26 '17 at 09:02
  • when i try to add those two lines in xml file it shows error like "The prefix "mvc" for element "mvc:resources" is not bound." – johnny May 26 '17 at 09:17
  • Would you mind to add the full content of the XML file to your original post? – LoHer May 26 '17 at 09:22
  • Could you try if it works when you just add it like this: `` – LoHer May 26 '17 at 10:40
  • I've added a new suggestion as an update to my answer above. The XML file looks like the MVC definitions are missing. Would you, please, try to extend the XML file and let me know if it works for you? – LoHer May 27 '17 at 09:41
0

Path should be

    <script type="text/javascript" src="../js/empForm.js">

Try to force cache refresh your browser (Ctrl+F5) after applying changes.

yellie
  • 46
  • 5
  • still not getting out put . when i run in browser consol showing the error like "Uncaught ReferenceError: nameValidate is not defined at HTMLInputElement.onblur (empform.spring:17)" – johnny May 26 '17 at 05:59
  • Are your functions (e.g. `nameValidate`) inside `$(document).ready()` or something? If so html will not be able to find your functions. Try putting them outside `$(document).ready()`. – yellie May 26 '17 at 06:19
  • can you please explain my total code is mentioned above. – johnny May 26 '17 at 06:28
  • I believe that your js file can be found by your html but not the functions Instead of putting `onblur` on your html, try adding id to your elements then use addEventListener e.g. `var s = document.getElementById("s"); s.addEventListener("blur", salValidate());` – yellie May 26 '17 at 06:39