0

I am trying Spring MVC 4 with datatables for publishing data on frontend.

My project Name is NewEDXUI.

Scenario :- index.jsp will get data from Controller/Rest Controller to publish data from database in Table format.

Issues i am facing - i have created web.xml and servlet.xml

1> in my jsp , if i am giving path of Js files from resource folder - then while loading it is looking for http://localhost:8080/NewEDXUI/Views/resources/js/client.js if instead of "resources/js/client.js" i use "../resources/js/client.js" then client.js file load properly.

2> Jsp page has table with id ="tableClient" and it should get data from client.js var tableClient , which is invoking getAllPartners service define in controller package MainController.java .. but when it is invoking i am getting GET http://localhost:8080/NewEDXUI/Views/getAllPartners?_=1497263560865 404 (Not Found)

Please provide some guidance how to resolve these issues and complete my first Spring project.

Project Structure

enter image description here

Web.xml

<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
                         http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0" metadata-complete="true">
<display-name>Archetype Created Web Application</display-name>
<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>/</url-pattern>
</servlet-mapping>
<welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
</welcome-file-list>

springmvc-servlet.xml

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

<context:annotation-config />   
<tx:annotation-driven /> 
<mvc:annotation-driven />

<bean id="viewResolver"
    class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
    <property name="prefix" value="/Views/" />
    <property name="suffix" value=".jsp" />
</bean>

<mvc:resources mapping="/resources/**" location="/resources/" />


<context:property-placeholder location="classpath:hibernate.properties" />

<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="${database.driverClass}" />
    <property name="url" value="${database.url}" />
    <property name="username" value="${database.username}" />
    <property name="password" value="${database.password}" />
</bean>

<bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">${hibernate.dialect}</prop>
            <prop key="hibernate.format_sql">${hibernate.format_sql}</prop>
            <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
        </props>
    </property>
    <property name="packagesToScan">
        <list>
            <value>com.edx.model</value>
        </list>
    </property>
</bean>

<bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory" />
</bean>

<context:component-scan base-package="com.edx.controller" />
<context:component-scan base-package="com.edx.dao" />
<context:component-scan base-package="com.edx.service" />
<context:component-scan base-package="com.edx.util" />

Client.js

$(document).ready(
    function() {

        var tableClient = $('#tableClient').DataTable(
                {
                    "autoWidth" : false,
                    "columnDefs" : [{
                        "targets" : [ 0 ],
                        "visible" : false,
                        "searchable" : false
                    }],
                    "ajax" : {
                        "url" : "/NewEDXUI/Views/getAllPartners",
                        "type" : "GET",
                        "success" : function(data) {
                            $.each(data, function(ind, obj) {
                                tableClient.row.add(
                                        [ obj.partner_code,
                                        obj.partner_name,
                                        obj.status_code,
                                        obj.default_group_code,
                                        obj.partner_type,
                                        obj.network_type ]).draw();
                            });
                        }
                    },
                })

        $(window).on('load',function() {

        });

        $("#buttonSearch").click(function() {
            tableClient.clear().draw();
            tableClient.ajax.reload();
        });

    });

MainController.java

    package com.edx.controller;

    import java.util.List;

    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    import org.springframework.web.bind.annotation.RestController;

    import com.edx.model.Partner;
    import com.edx.service.PartnerService;

    @RestController
    public class MainController {

        @Autowired
        private PartnerService partnerService;

        @RequestMapping(value="/getAllPartners", method=RequestMethod.GET, produces="application/json")
        public List<Partner> getAllPartners(HttpServletRequest req,HttpServletResponse resp){
            System.out.println("Inside Controller");
            List<Partner> partnerlist = partnerService.getAllPartners();    
            return partnerlist;
        }

    }
niting
  • 55
  • 4
  • 12

1 Answers1

0

why you are using Spring 4 witn xml file configuration? for best practices please to use the annotation Config on Spring 4.

You are also wrong on you request, please to remove Views:

http://localhost:8080/NewEDXUI/getAllPartners