Announcement Announcement Module
Collapse
No announcement yet.
[Java, Spring 3] Beginner Problem Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • [Java, Spring 3] Beginner Problem

    Good morning,

    I try to create my first web site using Spring 3. But there is problem how to set common properties to run simple web application.

    I use Eclipse 3.4.1, Tomcat 6.0, Java version 1.6.

    In a web folder of Tomcat, there is a folder webapps, which is set to be WorkSpace for Eclipse. I've made new Java Project named "Spring3". Project Spring3 includes folder WEB-INF, which includes "lib" folder, "classes" folder and Spring3-servlet.xml and web.xml.

    lib
    I added all jar from dist folder and commons-logging and "Builded path" adding these jar.

    web.xml
    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    
    <web-app xmlns="http://java.sun.com/xml/ns/j2ee"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
             http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
             version="2.4">
    
    	<display-name>Spring 3</display-name>
    	<servlet>
    		<servlet-name>Spring3</servlet-name>
    		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    		<load-on-startup>1</load-on-startup>
    	</servlet>
    	
    	<servlet-mapping>
    		<servlet-name>Spring3</servlet-name>
    		<url-pattern>*.htm</url-pattern>
    	</servlet-mapping>
    	
    	<welcome-file-list>
    		<welcome-file>index.htm</welcome-file>
    	</welcome-file-list>
    	
    </web-app>
    Spring3-servlet.xml
    Code:
    <beans xmlns="http://www.springframework.org/schema/beans"  
    	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"  
    	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">  
        
    	<bean id="Spring3Bean" class="classes.Spring3" />
    	  
    </beans>
    "Classes" folder contents 2 classes: Spring3.java and Spring3Test.java

    Spring3.java
    Code:
    package classes;
    
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    
    @Controller
    public class Spring3 {
    
    	@RequestMapping("/hello.htm")
    	public void sayHola()
    	{
    		System.out.println("Hola spring 3.0!");
    	}
    	
    }
    Spring3Test.java
    Code:
    package classes;
    import org.springframework.beans.factory.xml.XmlBeanFactory;  
    import org.springframework.core.io.ClassPathResource;  
    
    public class Spring3Test {  
        
    	public static void main(String[] args) {  
              XmlBeanFactory beanFactory = new XmlBeanFactory(new ClassPathResource(  
              "Spring3-servlet.xml"));  
              
              Spring3 simpleSpringBean = 
            	  (Spring3) beanFactory.getBean("Spring3Bean");  
              simpleSpringBean.sayHola();
         }  
    }

    I've run my code as Java application in the Eclipse.
    Problem is, that my Web browser returns this error message: Cannot find class [classes.Spring3] for bean with name 'Spring3Bean' defined in ServletContext resource [/WEB-INF/Spring3-servlet.xml];

  • #2
    I think Spring3-servlet.xml should be something like

    Code:
    <?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: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:annotation-config/>
    
    	<bean id="Spring3Bean" class="classes.Spring3" />
         
    </beans>
    http://static.springsource.org/sprin...tml/beans.html 3.9 Annotation-based container configuration

    Comment

    Working...
    X