仍在尝试使用 Spring 框架,因此我为我的 Shiny 新事物提前道歉。
我正在尝试让 Ajax 工作,但结果不太理想。
这是 Controller 代码,这只是我想要开始工作的代码,因此它可能看起来微不足道:
@RequestMapping("/rest/login")
public @ResponseBody CallManager logIn() throws ServletException {
callManager.removeStationId();
return callManager;
}
这是相关的web.xml
<servlet>
<servlet-name>myservlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>myservlet</servlet-name>
<url-pattern>*.htm</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>myservlet</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
这是 myservlet-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: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-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<import resource="database-c3p0.xml" />
<context:annotation-config/>
<mvc:annotation-driven />
<!-- the application context definition for the springapp DispatcherServlet -->
<bean id="productManager" class="org.dat.service.ProductManagerImpl">
</bean>
<bean id="priceIncrease" class="org.dat.service.PriceIncrease">
</bean>
<bean id="stationID" class="org.dat.domain.StationId">
<property name="datStation" value=""/>
</bean>
<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basename" value="messages"/>
</bean>
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView">
</property>
<property name="prefix" value="/WEB-INF/jsp/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
这是我收到的错误:
org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/myservlet/rest/login] in DispatcherServlet with name 'myservlet'
非常感谢您为 Ajax 正常运行提供的任何帮助。如果您需要查看其他内容,请告诉我。谢谢。
最佳答案
您的映射与请求不匹配。
您的 DispatcherServlet
映射是 /rest/*
,因此对 /myservlet/rest/login
的调用将由 DispatcherServlet 正确处理,但是除此之外它不会找到匹配项,原因是 DispatcherServlet 会从映射匹配的其余部分中删除其映射。因此,在这种情况下,它将查找 /login
的匹配项,但在任何 Controller 中都找不到它。
更改为 @RequestMapping("/login")
应该可以,或者另一种方法是将 DispatcherServlet 的映射更改为 /
关于java - 无法让 Ajax 工作。这与附加的配置有关吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16820621/