0

Im new to MVC. Ive been able to do the tutorial of a couple examples with no problem. Im going to send this project .war file to a potential employer. And he's impatiently waiting.

Im getting a 404 error when trying to run http://localhost:8080/ProductStore/index

My project dir structure is as follows:

ProductStore src/java/main com.productstore.dao ProductsDoa.java com.productstore.controller Productscontroler.java com.productstore.domain Products.java com.productstore.service ProductsService.java
WEB-INF jsp
index.jsp spring-servlet.xml web.xml

Im at a complete loss.

My config files are as follows: web.xml

<?xml version="1.0" encoding="UTF-8"?>
            <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xmlns="http://java.sun.com/xml/ns/javaee"
             xmlns:web="http://java.sun.com/xml/ns/javaee/web- app_2_5.xsd"
             xsi:schemaLocation="http://java.sun.com/xml/ns/javaee;             http://java.sun.com/xml/ns/javaee/web-app.xsd"
             id="WebApp_ID" version="2.5">
             <display-name>Spring3-Hibernate</display-name>
             <welcome-file-list>
                 <welcome-file>/WEB-INF/jsp/index.jsp</welcome-file>
             </welcome-file-list>
             <servlet>
             <servlet-name>spring</servlet-name>
             <servlet-class>
                 org.springframework.web.servlet.DispatcherServlet
             </servlet-class>
             <init-param>
               <param-name>contextConfigLocation</param-name>
              <param-value>/WEB-INF/spring-servlet.xml</param-value>
             </init-param>
             <load-on-startup>1</load-on-startup>
         </servlet>
         <servlet-mapping>
             <servlet-name>spring</servlet-name>
             <url-pattern>/</url-pattern>
         </servlet-mapping>
     </web-app>

spring-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: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:mvc="http://www.springframework.org/schema/mvc"
    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">

    <mvc:annotation-driven />
    <context:annotation-config />
    <context:component-scan base-package="com.productstore" />

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

    <bean id="messageSource"
        class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
        <property name="basename" value="classpath:messages" />
        <property name="defaultEncoding" value="UTF-8" />
    </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>classpath: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>

My controller class is as follows:

 package com.productstore.controller;

    package com.productstore.controller;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.productstore.domain.Products;
import com.productstore.service.ProductsService;

@Controller 
public class ProductsController {

     @Autowired
        private ProductsService productsService;


     @RequestMapping(value = "/index", method = RequestMethod.GET)
     public String listProducts (Map<String, Object> map) {

         System.out.println("index");

            map.put("products", new Products());
            map.put("productsList", productsService.listProducts());

            return "index";
     }     

     @RequestMapping(value = "/add", method = RequestMethod.POST)
        public String addProducts(@ModelAttribute("products")
        Products products, BindingResult result) {

            productsService.addProducts(products);

            return "redirect:/index";
        }

        @RequestMapping("/delete/{Id}")
        public String deleteProducts(@PathVariable("Id")
        Integer Id) {

            productsService.removeProducts(Id);

            return "redirect:/index";
        }
}       
Alabama Mothman
  • 87
  • 1
  • 3
  • 8
  • Always use the URL with all in lowercase, from your URL, what is `ProductStore`? your app directory deployed on Tomcat or has been defined in your web.xml file how servlet-mapping? Post your `web.xml` source code – Manuel Jordan May 25 '15 at 17:31
  • Your app starts up without any problem? check the .log files too. – Manuel Jordan May 25 '15 at 17:32
  • Hi, Im having problems with the web site. It's difficult to use. Im trying to post my web.xml – Alabama Mothman May 26 '15 at 18:13
  • I finally got the web.xml posted. ProductStore is the project name and com.productstore is just the package I have it in. I dont know how to get to the logs. But Im pretty sure it's not running at all. – Alabama Mothman May 26 '15 at 18:24
  • checkout this [SO](http://stackoverflow.com/questions/28611216/spring-mvc-spring-uses-sometimes-requestmappinghandlermapping-and-sometimes-si/28619571#28619571) – iamiddy May 26 '15 at 18:48

2 Answers2

0

If your application starts without errors the problem could be in your servlet mapping.

You map it as:

<servlet-mapping>
    <servlet-name>spring</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

which means mapping to root context. Try to change it to following

<servlet-mapping>
    <servlet-name>spring</servlet-name>
    <url-pattern>/*</url-pattern>
</servlet-mapping>
Stan
  • 1,410
  • 10
  • 14
  • Thanks Stan. I think I found my problem. Thank You very much for your reply. My problem appears to be a class not found. – Alabama Mothman May 27 '15 at 15:19
  • Please excuse my post, I find this website extremely difficult to use java.lang.ClassNotFoundException: org.springframework.web.method.annotation.ExceptionMethodMapping. But there are no jars that contain this class. Ive about had it with spring. It's a very sloppy framework with incompatible code all over the place. JSF is far superior. Spring MVC is not even MVC, I still have to combine logic in my jsp pages. I cant for the life of me see why people use it. If you know how to get around this error Id be much appreciative. – Alabama Mothman May 27 '15 at 15:28
  • If you are using Maven could you please post content of your pom.xml to see dependencies? – Stan May 27 '15 at 15:44
0

Just insure whether you have entry of component scan in servlet-context.xml file.

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

let me know if you still face the issue.

Avyaan
  • 1,285
  • 6
  • 20
  • 47