Spring 4.1 MVC没有调度我的Ajax Json调用我的控制器方法,发生了404 Not Found错误

时间:2014-11-04 02:09:49

标签: java ajax json spring spring-mvc

我有一个Spring MVC应用程序在Spring框架3.2上运行,现在,我将它转移到Spring MVC 4.1。一切都很好,除了jQuery 1.7.2 Ajax / Json调用得到“404 Not Found”错误页面。

配置为:web.xml

<listener>
    <listener-class>
        org.springframework.web.context.ContextLoaderListener
    </listener-class>
</listener>
<listener>
    <listener-class>
        org.springframework.web.context.request.RequestContextListener
    </listener-class>
</listener>
<filter>
    <description>generated-persistence-filter</description>
    <filter-name>postgresql41Filter</filter-name>
    <filter-class>
        org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter
    </filter-class>
    <init-param>
        <param-name>entityManagerFactoryBeanName</param-name>
        <param-value>postgresql41</param-value>
    </init-param>
</filter>
<filter>
    <description>generated-sitemesh-filter</description>
    <filter-name>Sitemesh Filter</filter-name>
    <filter-class>
        com.opensymphony.module.sitemesh.filter.PageFilter
    </filter-class>
</filter>

web-context.xml as:

<bean id="beanNameViewResolver" class="org.springframework.web.servlet.view.BeanNameViewResolver" />

<bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver">
    <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
    <property name="prefix" value="/WEB-INF/pages/" />
</bean>

<bean id="iPhoneUserAgentViewResolver" class="org.skyway.spring.util.viewresolution.UserAgentViewResolver">
    <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
    <property name="agentSubstring" value="iPhone" />
    <property name="prefix" value="/WEB-INF/iphone/" />
    <property name="order" value="0" />
</bean>

控制器类为:

@Controller("FirmController")
@ResponseBody
public class FirmController {
/*Added by CheckABNfromABR*/
@RequestMapping(value="/checkAbnbyAbr", method=RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
public @ResponseBody String checkAbnbyAbr(@RequestParam("abn") String abn, HttpServletRequest request, HttpServletResponse response, Locale locale, Model model) throws IOException{
    /*ObjectMapper mapper = new ObjectMapper();*/
    /*AbnInfo requesInfo = mapper.readValue(json, AbnInfo.class);*/
    AbnInfo abnInfo = new AbnInfo();
    abnInfo.setAbn(abn);

    String viewName = "Success";
    if (viewName.equalsIgnoreCase("Success"))
        abnInfo.setValidation("Success");
    else
        abnInfo.setValidation("Error");
    return toJson(abnInfo);
}
}

javascript代码为:

function checkAjaxCall(inabn){
/*var name = $('#name').val(); 
var location = $('#location').val(); */
/*var json = {"name" : name,"location" : location};  */
/*var json = {"abn" : inabn};*/
jQuery.ajax({
    url: "checkAbnbyAbr",
    type: 'GET',
    data: "abn="+inabn,
    dataType: "json",
    /*cache:    false,*/
    async:  false,
    beforeSend: function(xhr) {
        xhr.setRequestHeader("Accept", "application/json");  
        xhr.setRequestHeader("Content-Type", "application/json");
    },
    success:function(response){
        /*alert("Validation: "+response.validation+"   Name: "+response.name+"  Location: "+response.location);*/
        console.log("Validation: "+response.validation+"|");
    },
    error:function(jqXhr, textStatus, errorThrown){
        alert(textStatus);
    }
});
return true;
};

在firebug中请求标头:

Accept  application/json
Accept-Encoding gzip, deflate
Accept-Language en-US,en;q=0.5
Content-Type    application/json
Cookie  JSESSIONID=55AD33FFBF4524926A53C660FA734F01
Host    localhost:8080
Referer http://localhost:8080/OZSSCMEMAC/newFirm
User-Agent  Mozilla/5.0 (Macintosh; Intel Mac OS X 10.9; rv:33.0) Gecko/20100101 Firefox/33.0
X-Requested-With    XMLHttpRequest

响应萤火虫中的标题:

Content-Language zh    内容长度1042    Content-Type text / html; charset = utf-8    日期星期二,2014年11月4日00:14:43 GMT    服务器Apache-Coyote / 1.1

我设置log4j.properties以支持调试状态下的spring应用程序,我得到了一些服务器信息:

2014-11-04 10:14:43,524 DEBUG [http-nio-8080-exec-34] OpenEntityManagerInViewFilter - Opening JPA EntityManager in OpenEntityManagerInViewFilter
2014-11-04 10:14:43,524 DEBUG [http-nio-8080-exec-34] OpenEntityManagerInViewFilter - Opening JPA EntityManager in OpenEntityManagerInViewFilter
2014-11-04 10:14:43,524 DEBUG [http-nio-8080-exec-34] OpenEntityManagerInViewFilter - Closing JPA EntityManager in OpenEntityManagerInViewFilter
2014-11-04 10:14:43,524 DEBUG [http-nio-8080-exec-34] OpenEntityManagerInViewFilter - Closing JPA EntityManager in OpenEntityManagerInViewFilter
2014-11-04 10:14:43,524 DEBUG [http-nio-8080-exec-34] EntityManagerFactoryUtils - Closing JPA EntityManager
2014-11-04 10:14:43,524 DEBUG [http-nio-8080-exec-34] EntityManagerFactoryUtils - Closing JPA EntityManager

似乎Ajax / Json请求是由OpenEntityManagerInViewFilter处理的,并且处理程序没有找到正确的Controller方法。

我相信在配置文件中有一些配置错过,我没有发现任何来自谷歌的Spring 4.1 Ajax / Json调用配置。

任何人都有这方面的经验,请指教!!

1 个答案:

答案 0 :(得分:1)

您缺少Dispatcher Servlet,它将请求映射到控制器。它在您的web.xml中定义如下:

<servlet>
        <servlet-name>action</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>debug</param-name>
            <param-value>true</param-value>
        </init-param>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>
                /WEB-INF/action-servlet.xml
            </param-value>
        </init-param>
        <load-on-startup>2</load-on-startup>
    </servlet>

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

在您的action-servlet.xml中,您需要指定:

  <tx:annotation-driven/>
    <mvc:annotation-driven/>
    <context:component-scan base-package="your.package"/>
    <context:annotation-config/>