gpt4 book ai didi

java - Spring 4.1 MVC 未将我的 Ajax Json 调用分派(dispatch)到我的 Controller 方法,出现 404 Not Found 错误

转载 作者:行者123 更新时间:2023-12-01 12:20:07 27 4
gpt4 key购买 nike

我有一个 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 为:

<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 类为:

@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 中的请求 header :

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

firebug 中的响应 header :

内容语言 en 内容长度 1042 内容类型 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 方法。

我相信配置文件中遗漏了一些配置,我没有从 google 找到任何 Spring 4.1 Ajax/Json 调用配置的内容。

谁有这方面的经验,请指教!!

最佳答案

您缺少 Dispatcher Servlet,它负责将请求映射到 Controller 。它在您的 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/>

关于java - Spring 4.1 MVC 未将我的 Ajax Json 调用分派(dispatch)到我的 Controller 方法,出现 404 Not Found 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26726621/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com