HTTP状态404 - 在Spring 4应用程序中找不到

时间:2015-12-01 11:42:48

标签: java json spring spring-mvc jackson

我有没有任何pom.xml文件的NetBeans Spring MVC项目。我包括 jackson-databind-2.7.0-rc1.jar jackson-core-2.7.0-rc1.jar jackson-annotation-2.7.0 -rc1.jar 即可。但按下“单击我”按钮时出现以下错误: HTTP状态404 - 未找到。如何避免此错误?

这是我的文件

的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>

调度-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;
    }

}

当我通过以下代码进行chante方法测试()时,我遇到了同样的错误:

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

    return t;
}

1 个答案:

答案 0 :(得分:2)

确定。让&#39;让这很简单。无需配置多个控制器处理程序和多个视图解析程序。进行以下更改。

1.将您的web.xml调度程序servlet映射从*.html更改为/

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

2.dispatcher-servlet.xml中,将您的配置替换为this

3.在您的ajax调用中,使用url: "./ls-test",函数中的onclick()