0
votes

i spend a few hours fighting with my problem, but with no big result, maybe someone will help me :)

My project look like this:

enter image description here

I want to use a library to draw a chart from this tutorial: FusionCharts

I got error message this:

09-Jan-2018 21:56:22.304 WARNING [http-nio-8080-exec-1] org.springframework.web.servlet.PageNotFound.noHandlerFound No mapping found for HTTP request with URI [/] in DispatcherServlet with name 'spring' 09-Jan-2018 21:56:22.323 WARNING [http-nio-8080-exec-3] org.springframework.web.servlet.PageNotFound.noHandlerFound No mapping found for HTTP request with URI [/] in DispatcherServlet with name 'spring' 09-Jan-2018 21:56:22.465 WARNING [http-nio-8080-exec-5] org.springframework.web.servlet.PageNotFound.noHandlerFound No mapping found for HTTP request with URI [/] in DispatcherServlet with name 'spring'

Files FusionCharts, fusioncharts.charts.js and fusioncharts.js are from library.

Home Controller Code:

package com.charts.controller;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

public class HomeController implements Controller {
    @Override
    public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        return new ModelAndView("index");
    }

    public String chartmaker(){
        FusionCharts lineChart = new FusionCharts (
                "line",// chartType
                "chart1",// chartId
                "600","350",// chartWidth, chartHeight
                "chart",// chartContainer
                "jsonurl",// dataFormat
                "data.json"
        );
        return lineChart.render();
    }

}

index.jsp:

<%@ page import="com.charts.controller.HomeController" %>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@page import="com.charts.controller.FusionCharts" %>
<!DOCTYPE html>
<html>
<head>
    <title>FusionCharts || www.fusioncharts.com</title>
    <script src="../fusioncharts.js"></script>
    <script src="../fusioncharts.charts.js"></script>
    <script src="../fusioncharts.theme.fint.js"></script>
</head>
    <body>
    <div id="chart"></div>

    <%
        HomeController a = new HomeController();
        out.println(a.chartmaker());
    %>

    </body>
</html>

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

    <bean id="HandlerMapping" class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping" />

    <bean id="/welcome.html" class="com.charts.controller.HomeController" />

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

</beans>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">

    <servlet>
        <servlet-name>spring</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

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

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>WEB-INF/spring-servlet.xml</param-value>
    </context-param>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

</web-app>

When i run a project, i got this message: enter image description here

When i go to the http://localhost:8080/welcome.html i got a clear screen and this library doesn't draw any chart or something like that.

My configuration files are properly like web.xml or spring-servlet.xml ? I will be grateful for any advices

Regards, Adrian

1
You shouldn't instantiate and/or call methods on your Controller class from your JSP. Your Controller handler methods should return a view name which corresponds to a view (viewname.jsp). It might help to search for some Spring MVC tutorials and work through a getting started app first, as your current usage seems a bit confused. - Kevin Hooke
You might also want to look at creating a new Spring MVC app with Spring Boot, as the configuration is vastly simplified (no need for xml config anymore) - Kevin Hooke
In Controller i return a view index.jsp like that: @RequestMapping("/index") public ModelAndView hello() { return new ModelAndView("index"); }, I did it a properly? - user7061656

1 Answers

0
votes

I suggest you annotate your HomeController class with @Controller, and inside a method with any name (ex. public String showIndex() { return "index"; } ). Annotate that method with @GetMapping("/") should work.