当前位置: 首页 > 知识库问答 >
问题:

登录后Spring应用程序AJAX调用返回404 not found

罗鸿福
2023-03-14

我有一个spring应用程序,登录后的ajax调用不起作用。

我在CHrome开发工具代码片段中实现了ajax功能,如下所示。

var today = new Date();
var month=today.getMonth();
var name="testuser";

$(document).ready(function(){
  $.ajax({
   type: "GET",
   url: "ajax/getStmt",
   data: "name="+name+"&month="+month,
   success: function(msg){
     alert( "Data Saved: " + msg );
   },
   error: function(msg){
       alert("there was an error \n"+msg);
   }
 }); 
 });

控制器:

package com.cardholder.controller.ajax;
@Controller
public class StatementAjax {
@Autowired
public UserDao userDao;

//initialise logger, dao
@RequestMapping(value = "/ajax/getMonths", method = RequestMethod.GET)
public String getMonths(){
    //get user from session
    //get user token from session

    /*UserStatement availableStmts = new UserStatement();
    availableStmts.setAvailableStatements(userDao.getUserStatementMonths(user.getUser_token()));*/
    return "months";
}
@RequestMapping(value = "/ajax/getStmt", method = RequestMethod.GET)
public String getStatement(@RequestParam(required = true) String name, @RequestParam(required = true) String month){
    System.out.println(name+", "+month);
    //get user from session
    //get user token from session
    /*UserStatement availableStmts = new UserStatement();
    availableStmts.setAvailableStatements(userDao.getUserStatementMonths(user.getUser_token()));
    ArrayList<UserStatement> s = (ArrayList<UserStatement>) userDao.getUserSelectedStatement(user.getUser_token(), 07, 2015);*/

    return "statements";
}

}

SpringXML。

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:p="http://www.springframework.org/schema/p"
xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:security="http://www.springframework.org/schema/security"
xsi:schemaLocation="http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
http://www.springframework.org/schema/context  
http://www.springframework.org/schema/context/spring-context-4.1.xsd 
 http://www.springframework.org/schema/jdbc 
 http://www.springframework.org/schema/jdbc/spring-jdbc-4.1.xsd
http://www.springframework.org/schema/security
http://www.springframework.org/schema/security/spring-security-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.1.xsd">
<context:annotation-config />
<context:component-scan base-package="com.cardholder,com.cardholder.orm,com.cardholder.controller.ajax" />
<tx:annotation-driven transaction-manager="transactionManager"></tx:annotation-driven>
<bean id="secureServiceClient" class="com.cardholder.service.SecureServiceClient">
     <property name="serviceUrl" value="http://xxxx:8080/xApi/api/"></property>
     <property name="serviceUserName" value="xxxx"></property>
    <property name="servicePassword" value="xxxx"></property> 
</bean>
<bean id="httpHeders" class="org.springframework.http.HttpHeaders">
</bean>
<bean id="restTemplate" class="org.springframework.web.client.RestTemplate">

<property name="errorHandler">
    <bean class= "com.cardholder.service.error.ServiceClientErrorHandler" />
</property>
</bean>

<bean id="resourceLoader" class="org.springframework.core.io.DefaultResourceLoader"/>
<bean id="pdfRenderer" class="com.cardholder.controller.menunav.util.Util"/>        
<bean id="serviceClientInterceptor" class="com.cardholder.service.interceptors.ServiceClientInterceptor">
    <property name="serviceUserName" value="xxx"></property>
    <property name="servicePassword" value="xxx"></property> 
</bean>
<bean id="encoder"
    class="org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder">
    <constructor-arg name="strength" value="16" />
</bean>
<bean id="idGenerator" class="org.apache.catalina.util.SessionIdGenerator"></bean>
<bean id="sessionValidator" class="com.cardholder.session.SessionValidator"></bean>
<bean class="org.springframework.orm.hibernate4.HibernateTransactionManager" id="transactionManager">
    <property name="sessionFactory" ref="sessionFactory" />
</bean>
<bean id="sessionFactory"
    class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    <property name="dataSource" ref="dataSource"></property>
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
            <prop key="hibernate.show_sql">true</prop>
        </props>
    </property>
    <property name="packagesToScan" value="com.cardholder"></property>
</bean>
<bean id="dataSource"
    class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="com.mysql.jdbc.Driver" />
    <property name="url" value="jdbc:mysql://xxxx:3306/portal" />
    <property name="username" value="xxxx" />
    <property name="password" value="xxxx" />
</bean>

下面是devtools截图的链接。

http://i.stack.imgur.com/EXry7.png

据我所知,即使请求试图访问的URL是正确的,请求也会进入应用程序上下文。

我在dispatcher servlet的do dispatch方法中有一个调试点,它应该是在任何拦截器、控制器等之前到达的第一个位置。我没有看到任何线程暂停在那里。

编辑:添加网页。xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns="http://java.sun.com/xml/ns/javaee" 
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" 
id="WebApp_ID" version="3.0"
metadata-complete="true">
 <display-name>NCHP</display-name>

  <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>1</load-on-startup> 
   </servlet>

   <servlet-mapping>  
     <servlet-name>dispatcher</servlet-name> 
     <url-pattern>*.*</url-pattern>  
     <url-pattern>*.htm</url-pattern>  
     <url-pattern>*.do</url-pattern>
  </servlet-mapping>

   <listener>
      <listener-class>
         org.springframework.web.context.ContextLoaderListener
      </listener-class>
   </listener>
 <filter>  
    <filter-name>encodingFilter</filter-name>  
    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>  
    <init-param>  
       <param-name>encoding</param-name>  
       <param-value>UTF-8</param-value>  
    </init-param>  
    <init-param>  
       <param-name>forceEncoding</param-name>  
       <param-value>true</param-value>  
    </init-param>  
</filter>  
<filter-mapping>  
    <filter-name>encodingFilter</filter-name>  
    <url-pattern>/*</url-pattern>  
</filter-mapping>
   <welcome-file-list>  
     <welcome-file>land.htm</welcome-file> 
 </welcome-file-list> 
</web-app>

共有1个答案

江建明
2023-03-14

感谢您发布网站。xml

<servlet-mapping>  
     <servlet-name>dispatcher</servlet-name> 
     <url-pattern>*.*</url-pattern>  
     <url-pattern>*.htm</url-pattern>  
     <url-pattern>*.do</url-pattern>
  </servlet-mapping>

基于上述配置,只有在传入URL具有一些路径信息时,才会咨询Spring的调度程序。e、 g.登录。htm什么的。xx。但是,如果url没有路径信息,例如/getStmt,则调度程序将不会收到请求。

您可以将ajaxendpoint更改为/getStmt之类的内容。ajax。或者在web xml中添加/作为url模式(根据项目中其他资源的使用方式,这可能会产生其他影响)。

如果您目前通过xxxx.htm约定来提供页面,我建议使用xxxx.ajax约定来处理ajax请求。

您可能还想看看这个答案:没有找到URI为Spring MVC的HTTP请求的映射

希望这有帮助。

 类似资料:
  • 问题内容: 对于典型的最典型的面向Internet的网站,当您通过关闭选项卡( 无需注销 )登录并离开该网站(然后 注销 ),然后再次访问时,可能不需要您重新指定凭据或登录,您可以直接登录。 这一切在后端如何发生?如何在我的JSF 2.1应用程序中启用这种机制? 在Tomcat7服务器上使用JSF 2.1 问题答案: 这基本上是通过一个长寿的cookie完成的。JSF API不提供此功能,因为它只

  • 当使用signInWithEmailAndPassword()API在firebase中进行身份验证时,我无法获得有效的“当前”用户。 我使用用户名和密码登录应用程序 我一旦登录,用户就会被带到主页 我试图获取用户的UID,但是没有返回有效的用户。调用发生在异步函数中,如下所示: 当前用户为空。 知道是什么引起的吗?

  • 我正在考虑将应用程序迁移到Cloud Foundry,因为我厌倦了独自管理我的服务器。在我当前的应用程序中,我使用Spring Security和会话来处理我的用户登录。然而,我对如何更改我的代码一无所知,因此Cloud Foundry的多个实例支持我的用户以某种无状态的方式登录(但使用令牌)。我已经研究了UAA,但这似乎是针对云铸造用户的,而不是我的应用程序的用户。 类似OAUTH2的东西似乎是

  • 我正在学习Spring Boot。我创建了一个具有Spring Boot安全性的示例Spring Boot Web应用程序,它运行良好。 现在我想在同一个Web应用程序中添加Rest Webservice功能。 问题:当我尝试访问任何API时,我得到的响应是登录。jsp 要求:我希望不需要JSON响应身份验证和授权 要实现这一目标,我需要做哪些更改 Web安全配置:

  • 我是Spring Boot的新手,目前被卡住了。我跟着这个(https://github.com/AppDirect/service-integration-sdk/wiki)教程,因为我想实现一个将自身集成到AppDirect中的应用程序。在日志中,我可以看到endpoint被创建和映射: 但是,当我尝试使用浏览器或Http-请求器访问endpoint(http://localhost:8080

  • 预先列入登记表工作完善。注册后,它甚至显示我已登录。但当我尝试使用我在注册表中输入的相同凭证登录时。它不允许我登录。我认为问题出在中间件上,因为当我输入错误的用户名/密码时,会显示错误,即无效凭据。但不是有效的,它只是重定向到登录页面没有任何错误。我使用的是laravel 5.0,所有文件都是由laravel提供的