-1

I have NetBeans Spring MVC project without any pom.xml file. I included jackson-databind-2.7.0-rc1.jar, jackson-core-2.7.0-rc1.jar and jackson-annotation-2.7.0-rc1.jar. But I got the following error pressing the button "Click me": HTTP Status 404 - Not Found. How can I avoid this error?

Here are my files

applicationContext.xml:

a<?xml version='1.0' encoding='UTF-8' ?>
<!-- was: <?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:p="http://www.springframework.org/schema/p"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">

</beans>

dispatcher-servlet.xml:

<?xml version='1.0' encoding='UTF-8' ?>
<!-- was: <?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:p="http://www.springframework.org/schema/p"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
       http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

    <mvc:annotation-driven/>
    <context:component-scan base-package="controller" />

    <bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"/>

    <bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
        <property name="mappings">
            <props>
                <prop key="index.htm">indexController</prop>
            </props>
        </property>
    </bean>

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

    <!--
    The index controller.
    -->
    <bean name="indexController"
          class="org.springframework.web.servlet.mvc.ParameterizableViewController"
          p:viewName="index" />


    <bean id="viewResolver2" 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> 


     <mvc:resources mapping="/resources/**" location="/resources/"  
    cache-period="31556926"/>
</beans>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" 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">
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/applicationContext.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>2</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>*.htm</url-pattern>
    </servlet-mapping>
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
    <welcome-file-list>
        <welcome-file>redirect.jsp</welcome-file>
    </welcome-file-list>
</web-app>

index.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"  %>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Welcome to Spring Web MVC project</title>
        <script src="<c:url value="/resources/jsgrid/external/jquery/jquery-1.8.3.js" />"></script>
    </head>

    <body>

        <script language="javascript">

                function onClick()
                {
                     $.ajax({ 
                        type: "GET",
                        dataType: "jsonp",
                        url: "<c:url value="/ls-test.htm" />",
                        success: function(data){        
                          alert(data);
                        },
                        error: function (data, textStatus, xhr) {
                            $('html').html("" + data.responseText);
                        }
                     });
                }
            </script>

        <button onclick="onClick()" >Click me</button>

    </body>
</html>

example.java

package controller;

import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
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 org.springframework.web.portlet.ModelAndView;

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author 
*/
@Controller
public class Example {

    class Test
    {
        final int id;
        final String fio;

        public Test(int id, String fio) {
            this.id = id;
            this.fio = fio;
        }

        public int getId() {
            return id;
        }

        public String getFio() {
            return fio;
        }
    }

    @RequestMapping(value = "/ls-test", method = RequestMethod.GET,
            produces = {MediaType.APPLICATION_JSON_VALUE})
    public ResponseEntity <Test> test()
    {
        Test t = new Test(1, "123");

        return new ResponseEntity<Test>(t, HttpStatus.OK);
    }

    @RequestMapping(value = "/index", method = RequestMethod.GET)
    public ModelAndView loadIndex()
    {
        ModelAndView mv = new ModelAndView();
        mv.setViewName("index");
        return mv;
    }

}

I have the same error when I chante method test() by the following code:

@RequestMapping(value = "/ls-test", method = RequestMethod.GET,
        produces = {MediaType.APPLICATION_JSON_VALUE})
public @ResponseBody Test test()
{
    Test t = new Test(1, "123");

    return t;
}
I Ivanov
  • 23
  • 3

1 Answers1

2

Ok. Let' make this simple. No need to configure multiple controller handlers and multiple view resolvers. Make the changes below.

1. Change your web.xml dispatcher servlet mappings from *.html to /

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

2. In dispatcher-servlet.xml, replace your coniguration with this

3. In your ajax call, use url: "./ls-test", in the onclick() function

Omkar Puttagunta
  • 4,036
  • 3
  • 22
  • 35
  • When I changed URL I've got HTTP Status 500 - Internal Server Error. In GlassFish Server log: javax.servlet.ServletException: No adapter for handler [controller.Example@fc2307]: The DispatcherServlet configuration needs to include a HandlerAdapter that supports this handler – I Ivanov Dec 02 '15 at 06:44
  • ok...see my edited answer and make the necessary changes. This should work. Let me know – Omkar Puttagunta Dec 02 '15 at 07:54
  • Thanks a lot! As described here (http://stackoverflow.com/questions/21235472/http-status-406-spring-mvc-4-0-jquery-json), Spring can resolve data type by *.htm extension. I added servlet-mapping for *.json urls, and now everything works fine/ – I Ivanov Dec 02 '15 at 14:37
  • Thanks for letting me know! :) – Omkar Puttagunta Dec 02 '15 at 14:46