1

I have tried the solutions which were discussed in other discussions but nothing worked for me. Any help is appreciated

Here is my Web.xml file.

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  <display-name>spring-mvc-crud-demo</display-name>
  <absolute-ordering />

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

  <servlet>
    <servlet-name>dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>/WEB-INF/spring-mvc-crud-demo-servlet.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>

  <servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>
</web-app>

Here is my spring-mvc-crud-demo.xml file

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

    <!-- Add support for component scanning -->
    <context:component-scan base-package="com.luv2code.springdemo" />

    <!-- Add support for conversion, formatting and validation support -->
    <mvc:annotation-driven/>

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

    <!-- Step 1: Define Database DataSource / connection pool -->
    <bean id="myDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
          destroy-method="close">
        <property name="driverClass" value="com.mysql.cj.jdbc.Driver" />
        <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/web_customer_tracker?useSSL=false&amp;serverTimezone=UTC" />
        <property name="user" value="springstudent" />
        <property name="password" value="springstudent" /> 

        <!-- these are connection pool properties for C3P0 -->
        <property name="initialPoolSize" value="5"/>
        <property name="minPoolSize" value="5" />
        <property name="maxPoolSize" value="20" />
        <property name="maxIdleTime" value="30000" />
    </bean>  

    <!-- Step 2: Setup Hibernate session factory -->
    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
        <property name="dataSource" ref="myDataSource" />
        <property name="packagesToScan" value="com.luv2code.springdemo.entity" />
        <property name="hibernateProperties">
           <props>
              <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
              <prop key="hibernate.show_sql">true</prop>
           </props>
        </property>
   </bean>    

    <!-- Step 3: Setup Hibernate transaction manager -->
    <bean id="myTransactionManager"
            class="org.springframework.orm.hibernate5.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory"/>
    </bean>

    <!-- Step 4: Enable configuration of transactional behavior based on annotations -->
    <tx:annotation-driven transaction-manager="myTransactionManager" />

    <!-- Add support for reading web resources: css, images, js, etc ... -->
    <mvc:resources location="/resources/" mapping="/resources/**"></mvc:resources>



</beans>

This is my controller class

package com.springdemo.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.springdemo.dao.CustomerDAO;
import com.springdemo.entity.Customer;

@Controller
@RequestMapping("/customer")
public class CustomerController {

    // need to inject the customer dao
    @Autowired
    private CustomerDAO customerDAO;

    @RequestMapping("/list")
    public String listCustomers(Model theModel) {

        // get customers from the dao
        List<Customer> theCustomers = customerDAO.getCustomers();

        // add the customers to the model
        theModel.addAttribute("customers", theCustomers);

        return "list-customers";
    }

}



However on going to http://localhost:8080/web-customer-tracker/

it returns a 404 error.


May 13, 2019 1:37:43 AM org.springframework.web.servlet.handler.AbstractUrlHandlerMapping registerHandler
INFO: Mapped URL path [/resources/**] onto handler 'org.springframework.web.servlet.resource.ResourceHttpRequestHandler#0'
May 13, 2019 1:37:43 AM org.springframework.web.servlet.FrameworkServlet initServletBean
INFO: FrameworkServlet 'dispatcher': initialization completed in 11889 ms
May 13, 2019 1:37:43 AM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["http-nio-8080"]
May 13, 2019 1:37:43 AM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["ajp-nio-8009"]
May 13, 2019 1:37:43 AM org.apache.catalina.startup.Catalina start
INFO: Server startup in [14,797] milliseconds
May 13, 2019 1:37:48 AM org.springframework.web.servlet.DispatcherServlet noHandlerFound
WARNING: No mapping found for HTTP request with URI [/web-customer-tracker/] in DispatcherServlet with name 'dispatcher'

Here is my project structure

Project structure

Ashish Shah
  • 1,047
  • 7
  • 17
  • Share the pom.xml. You need add 'web-customer-tracker' as your final package name which will be deployed in server. – Oomph Fortuity May 13 '19 at 06:10
  • Hi Oomph this application is build using spring and hibernate, maven is not used – Ashish Shah May 13 '19 at 06:11
  • then How are you downloading/managing the dependancies if you are not using maven? – Oomph Fortuity May 13 '19 at 06:13
  • I am using Spring annotation to manage dependencies – Ashish Shah May 13 '19 at 06:15
  • the how are you creating your final deployable package? – Oomph Fortuity May 13 '19 at 06:16
  • Sorry I did not get your question. I am basically building web application which includes all CRUD features. Browser -> Controller -> Services->Data Access Object-> Database is the flow of the application. Was this helpful? – Ashish Shah May 13 '19 at 06:19
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/193233/discussion-between-oomph-fortuity-and-ashish-shah). – Oomph Fortuity May 13 '19 at 06:27
  • If this is a new application, avoid both the legacy Servlet configuration and JSP if at all possible. Spring Boot makes all of your configuration here unnecessary, and Thymeleaf is much easier to test. – chrylis -cautiouslyoptimistic- May 13 '19 at 06:53
  • I already build the application using Spring, just one step away from working project. So trying to solve this warning. – Ashish Shah May 13 '19 at 06:57
  • Try the URL: http://localhost:8080/web-customer-tracker/customer/list Your controller is mapped to that URL, you have nothing mapped to the root url /. Or it should be even : http://localhost:8080/customer/list because in the web.xml you map the root url to your only servlet – Veselin Davidov May 13 '19 at 07:12
  • I tried changing the URL, still getting same warnings – Ashish Shah May 13 '19 at 07:16
  • you are scanning `com.luv2code.springdemo` package, however your controller is in `com.springdemo.controller` – eparvan May 13 '19 at 11:49

2 Answers2

0

You don't have the end-point of http://localhost:8080/web-customer-tracker If you want to call the Customer List end-point you should call http://localhost:8080/customer/list

If you still want to use the context-path 'web-customer-tracker', you need to add the following configuration into application.properties

server.servlet.context-path=/web-customer-tracker

Then you can call customer list using http://localhost:8080/web-customer-tracker/customer/list

Mark
  • 216
  • 1
  • 2
  • 9
0

configure that url in servlet-mapping as

<servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>/web-customer-tracker*</url-pattern>
  </servlet-mapping>

Then you can call customer list using http://localhost:8080/web-customer-tracker/customer/list

MangduYogii
  • 935
  • 10
  • 24