0

please help me with not difficult problem. I want just try something with @RequestMapping annotation in my project. I created WebController.java, which has

  package com.mycompany.controller.controller;

    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    import org.springframework.web.servlet.ModelAndView;

    @Controller
    @RequestMapping("/welcome")
    public class WebController{

        @RequestMapping(method = RequestMethod.GET)
        public ModelAndView helloWorld(){

            ModelAndView model = new ModelAndView("HelloWorldPage");
            model.addObject("msg", "hello world");

            return model;
        }
    }

and HelloWorldPage.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
${msg }
</body>
</html>

springmvc-servlet.xml (other controllers and web app works fine)

<?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:aop="http://www.springframework.org/schema/aop"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:jee="http://www.springframework.org/schema/jee"
    xmlns:lang="http://www.springframework.org/schema/lang"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:util="http://www.springframework.org/schema/util"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd
        http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">

     <context:annotation-config />

    <context:component-scan base-package="com.mycompany.controller" />

    <bean name="/countrylist.html" class="com.mycompany.controller.controller.CountryController"/>
    <bean name="/citylist.html" class="com.mycompany.controller.controller.CityController"/>
    <bean name="/successcitysearch.html" class="com.mycompany.controller.controller.CitiesOfCountryController"/>

    <bean name="/addcountry.html" class="com.mycompany.controller.controller.AddCountryController">
    <property name="commandClass" value="com.mycompany.model.domain.Country"/>
    <property name="formView" value="addcountry"/>
    <property name="successView" value="countrylist.html"/>
    </bean>

    <bean name="/addcity.html" class="com.mycompany.controller.controller.AddCityController">
    <property name="commandClass" value="com.mycompany.model.domain.City"/>
    <property name="formView" value="addcity"/>
    <property name="successView" value="citylist.html"/>
    </bean>
    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
        <property name="prefix" value="/jsp/"/>
        <property name="suffix" value=".jsp"/>
    </bean>



    <bean id="propertyConfigurer"
        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
        p:location="/WEB-INF/jdbc.properties" />

    <bean id="dataSource"
        class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"
        p:driverClassName="${jdbc.driverClassName}"
        p:url="${jdbc.databaseurl}" p:username="${jdbc.username}"
        p:password="${jdbc.password}" />


    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="configLocation">
            <value>/WEB-INF/hibernate.cfg.xml</value>
        </property>
        <property name="configurationClass">
            <value>org.hibernate.cfg.AnnotationConfiguration</value>
        </property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">${jdbc.dialect}</prop>
                <prop key="hibernate.show_sql">true</prop>
            </props>
        </property>
    </bean>

    <tx:annotation-driven />
    <bean id="transactionManager"
        class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>

</beans>

web.xml

<?xml version="1.0" encoding="UTF-8"?>

<web-app version="2.4"
         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" >

  <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/springmvc-servlet.xml</param-value>
    </context-param>
     <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
  <servlet>
    <servlet-name>springmvc</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>


  <servlet-mapping>
    <servlet-name>springmvc</servlet-name>
    <url-pattern>*.html</url-pattern>
  </servlet-mapping>

  <welcome-file-list>
    <welcome-file>
      index.jsp
    </welcome-file>
    <welcome-file>index.html</welcome-file>
  </welcome-file-list>

</web-app>

So when I try write link localhost/MyCountryProject/welcome or localhost/MyCountryProject/welcome.html It gives me 404 error resource not available. Please help me, thanks!

Ajinkya
  • 22,324
  • 33
  • 110
  • 161
Jumper
  • 133
  • 2
  • 15

2 Answers2

0

To use spring-mvc with annotations you will need to tell spring in your configuration.

<mvc:annotation-driven />

What does <mvc:annotation-driven /> do?

Also make sure your file paths are correct.

One thing that would make searching for errors easier is to log debug information. The logs will tell you exactly what happened and how.

Community
  • 1
  • 1
Bart
  • 17,070
  • 5
  • 61
  • 80
  • Thank you man, hope you r right, but i cant check it because of this errors Error occured processing XML 'org/springframework/format/support/ FormattingConversionServiceFactoryBean'. See Error Log for more details – Jumper Jun 19 '13 at 11:25
  • What's the actual exception from the log? – Bart Jun 19 '13 at 11:37
  • Also make sure the `mvc` namespace is present in the xml. – Bart Jun 19 '13 at 11:39
  • java.lang.NoClassDefFoundError: org/springframework/format/support/FormattingConversionServiceFactoryBean at org.springframework.web.servlet.config.AnnotationDrivenBeanDefinitionParser.getConversionService(AnnotationDrivenBeanDefinitionParser.java:145) at org.springframework.web.servlet.config.AnnotationDrivenBeanDefinitionParser.parse(AnnotationDrivenBeanDefinitionParser.java:105).... – Jumper Jun 19 '13 at 11:39
  • Did you include the spring-context library in both development and production environments? – Bart Jun 19 '13 at 11:40
  • Yes, spring-context-3.0.5.RELEASE – Jumper Jun 19 '13 at 11:43
  • what are you mean by "both development and production environments"? I just added it to my project/web-inf/lib – Jumper Jun 19 '13 at 11:44
  • ok I understood I added it to classpath, but now I have error.Build path is incomplete. Cannot find class file for com.mycompany.controller.controller.AddCityController – Jumper Jun 19 '13 at 11:49
  • That is outside the scope of this question. Please fix your IDE problems unrelated to your question and report back after that. – Bart Jun 19 '13 at 11:51
0

I think this problem related to the servlet mapping configuration. change @RequestMapping:

@RequestMapping("/welcome.html")

and servlet mapping should be like this:

 <servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <url-pattern>*.html</url-pattern>
    </servlet-mapping>

    <servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <url-pattern>/welcome.html</url-pattern>
    </servlet-mapping>

link will be localhost/MyCountryProject/welcome.html

hurshid
  • 66
  • 1
  • 2
  • 5